require(grDevices)
library(Deriv)
require(lpSolve)
library(cubature)

Prob1

# Create the predictor and response variable.
x <- c(5.6,6.3,7,7.7,8.4)
y <- c(8.8,12.4,14.8,18.2,22.8)
relation <- lm(y~x)

fit <- lm(y~x)
summary(fit)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##     1     2     3     4     5 
##  0.16  0.38 -0.60 -0.58  0.64 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -18.4000     2.0829  -8.834 0.003057 ** 
## x             4.8286     0.2946  16.389 0.000494 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.6522 on 3 degrees of freedom
## Multiple R-squared:  0.989,  Adjusted R-squared:  0.9853 
## F-statistic: 268.6 on 1 and 3 DF,  p-value: 0.0004943
# Plot the chart.
plot(y,x,col = "blue",main = "Regression Line for given points",
abline(lm(x~y)),cex = 1.3,pch = 16,xlab = "X",ylab = "Y")
lm_coef <- round(coef(fit), 3) # extract coefficients
mtext(bquote(y == .(lm_coef[2])*x + .(lm_coef[1])),
adj=1, padj=4.6) # display equation

Prob2

#First Partial Derivatives w.r.t. x and y
df1<-Deriv(f)
df1
## function (x, y) 
## c(x = 24 - 6 * y^2, y = -(y * (12 * x + 24 * y)))

Critical Points from the first partial Derivatives in the x-y plane are:

- critical pt 1: (4,-2)
- critical pt 2: (4,+2)

Applied 2nd derivative test for the function again

#2ndPartial Derivatives w.r.t. x and y
Deriv(df1)
## function (x, y) 
## {
##     .e1 <- -(12 * y)
##     c(x = c(x = 0, y = .e1), y = c(x = .e1, y = -(12 * x + 48 * 
##         y)))
## }

Finally, the critical Points in the 3-D are:

print(paste0("When x=4 and y=-2, then z is ",f(4,-2)));
## [1] "When x=4 and y=-2, then z is 64"
print(paste0("When x=-4 and y=2, then z is ",f(-4,2)))
## [1] "When x=-4 and y=2, then z is -64"

To determine relative maxima/minimas, apply the Second Derivative Test

- If D>0 and f_xx(x_c,y_c)<0, then f(x,y) has a relative maximum at (x_c,y_c).
- If D>0 and f_xx(x_c,y_c)>0, then f(x,y) has a relative minimum at (x_c,y_c).
- If D<0, then f(x,y) has a saddle point at (x_c,y_c).
- If D=0, the second derivative test is inconclusive.

In our case, we have D = 0*(-12x-48y) - 144\(y^{2}\) < 0

Conclusion:

  1. This means the two critical pts of (4,2,64) and (-4,-2,-64) are saddle points, as shown in Fig1
  2. The maximas and minmas are on the edges of the boundary (whatever the boundaries defined by enduser). In my case, I pick in the x-y plane (-6,6) and (6,6) to be my boundary of anlysis, so from contour plot, the relative min is (6,6) which is the lighest color while the relative max is (-6,-6) which is the darkest shaded color

Prob3

Combining terms:

Revenue (x, y) where x are house brands and y name brans respectively

Total Rev(x,y)= (81 - 21x + 17y)x + (40 + 11x - 23y)y


#Step 1:  Rev function
R<-function(x,y){(81 - 21*x + 17*y)*x + (40 + 11*x - 23*y)*y}
#Step 2. What is the revenue if she sells the "house" brand for $2.30 and the "name" brand for $4.10?
print(paste0("The revenue is $", R(2.3,4.1)))
## [1] "The revenue is $116.62"

Prob4

The total weekly cost is given by C(x, y) = 1/6 x^2 +1/6 y^2 + 7x + 25y + 700

Total produced per week = 96, given

constraint => x+y <= 96

Whereby x is production in LA & y is production in Denver


# Making it into a 1-dimensional problem
y <- 96-x
Cn<-function(x){(1/6)*x^2 +(1/6)*(96-x)^2 + 7*x + 25*(96-x) + 700} #The cost function

r<- optim(c(96),Cn, method = c("Brent"),lower=0,upper =96)

print(paste0("The Min Cost is $", Cn(75)))
## [1] "The Min Cost is $2761"
print(paste0("Production in LA : ", round(r$par,0)))
## [1] "Production in LA : 75"
print(paste0("Production in Denver : ", round(96-r$par,0)))
## [1] "Production in Denver : 21"

Prob5

{ { e }^{ 8x+3y }dA } ; R: 2 <= x <= 4 and 2 <= y <= 4

Using “adaptIntegrate function”

fun <- function(x) exp(8*x+3*y)
#adaptIntegrate(fun, c(2,4), c(2,4), tol=1e-8)

Using apply method;

options(digits = 1)   
fvec = function(x, y) sapply(x, function(x, y) exp(8*x+3*y), y=y)
 
gvec = function(x) sapply(x, function(y) integrate(fvec, lower=2, upper=4,  subdivisions=10000, rel.tol=1.e-08, y=y)$val)

ans<-integrate(gvec, lower=2, upper=4, subdivisions=10000, rel.tol=1.e-08)

ans
## 5e+17 with absolute error < 5930