library(calculus)
library(Ryacas)
Find the equation of the regression line for the given points. Round any final values to the nearest hundredth, if necessary. ( 5.6, 8.8 ), ( 6.3, 12.4 ), ( 7, 14.8 ), ( 7.7, 18.2 ), ( 8.4, 20.8 )
# x and y points
x <- c(5.6, 6.3, 7, 7.7, 8.4)
y <- c(8.8, 12.4, 14.8, 18.2, 20.8)
# model the linear regression
model <- lm(y~x)
# summary of linear regression
summary(model)
##
## Call:
## lm(formula = y ~ x)
##
## Residuals:
## 1 2 3 4 5
## -0.24 0.38 -0.20 0.22 -0.16
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -14.8000 1.0365 -14.28 0.000744 ***
## x 4.2571 0.1466 29.04 8.97e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.3246 on 3 degrees of freedom
## Multiple R-squared: 0.9965, Adjusted R-squared: 0.9953
## F-statistic: 843.1 on 1 and 3 DF, p-value: 8.971e-05
# coefficient
coefficient <- round(coef(model), 2)
# equation of linear regression
cat(paste("Equation of the regression line: y =", coefficient[2], "x +", coefficient[1]), '\n')
## Equation of the regression line: y = 4.26 x + -14.8
Find all local maxima, local minima, and saddle points for the function given below. Write your answer(s) in the form (x, y, z). Separate multiple points with a comma. \(f(x, y) = 24x - 6xy^2 - 8y^3\)
# define function
f <- expression(24*x - 6*x*y**2 - 8*y**3)
# define partial derivative
fx <- (D(f, 'x'))
fy <- (D(f, 'y'))
print(fx)
## 24 - 6 * y^2
print(fy)
## -(6 * x * (2 * y) + 8 * (3 * y^2))
Solve \(fx = 0\); \[
\begin{aligned}
fx = 24 - 6y^2 &= 0 \\
6y^2 &= 24 \\
y^2 &= 4 \\
y &= \pm 2
\end{aligned}
\] Substituting back into \(f(x,y)\),
when \(y=+2\), \(x=-4\), therefore a critical point is \((-4, 2)\)
when \(y=-2\), \(x=+4\), therefore a critical point is \((4, -2)\)
Solve \(fy = 0\); \[
\begin{aligned}
fy = -12y(x + 2y) &= 0 \\
y = 0 \: \text{or} \: y &= \frac{x}{2} \\
\end{aligned}
\] Substituting back into \(f(x,y)\),
when \(y=0\), \(x=0\), therefore a critical point is \((0, 0)\)
\(y=-\frac{x}{2}\), leads to the same
critical points we found when \(fx =
0\)
Therefore our critical points are $(-4, 2), (4, -2), (0, 0))
Now find out if those points are local maxima or minima or saddle points,
# Calculate second partial derivatives
fxx <- (D(fx, "x"))
fxy <- (D(fx, "y"))
fyy <- (D(fy, "y"))
# Use to find Hessian determinant
f_discriminant <- function(x, y) {
eval(fxx) * eval(fyy) - eval(fxy)^2
}
# Evaluate Hessian determinant and second derivatives at critical points
critical_points <- list(c(-4, 2), c(4, -2), c(0, 0))
for (point in critical_points) {
x <- point[1]
y <- point[2]
discriminant_value <- f_discriminant(x, y)
second_xx <- eval(fxx, list(x = x, y = y))
if (discriminant_value > 0) {
if (second_xx > 0) {
print(paste("Local minimum at:", paste(point, collapse = ", ")))
} else {
print(paste("Local maximum at:", paste(point, collapse = ", ")))
}
} else if (discriminant_value < 0) {
print(paste("Saddle point at:", paste(point, collapse = ", ")))
} else {
print(paste("Inconclusive at:", paste(point, collapse = ", ")))
}
}
## [1] "Saddle point at: -4, 2"
## [1] "Saddle point at: 4, -2"
## [1] "Inconclusive at: 0, 0"
A grocery store sells two brands of a product, the “house” brand and a “name” brand. The manager estimates that if she sells the “house” brand for x dollars and the “name” brand for y dollars, she will be able to sell \(81 - 21x + 17y\) units of the “house” brand and \(40 + 11x - 23y\) units of the “name” brand.
Step 1. Find the revenue function \(R (x, y)\) \[ \begin{aligned} \text{House brand quantity,}\: Q_h &= 81 - 21x + 17y \\ \text{Name brand quantity,}\: Q_n &= 40 + 11x - 23y \\ \\ \text{Revenue,}\: R(x,y) &= xQ_h + yQ_y\text{, where }x\rightarrow\text{House brand price},\: y\rightarrow\text{Name brand price} \\ &= x(81 - 21x + 17y) + y(40 + 11x - 23y) \\ &= -21x^2 + 28xy + 81x - 23y^2 + 40y \end{aligned} \]
Step 2. What is the revenue if she sells the “house” brand for $2.30 and the “name” brand for $4.10?
Rxy <- function(x,y)
-21*x**2 + 28*x*y + 81*x - 23*y**2 + 40*y
cat(paste('The revenue is: $', round(Rxy(2.3, 4.1), 2), '\n'))
## The revenue is: $ 116.62
A company has a plant in Los Angeles and a plant in Denver. The firm is committed to produce a total of 96 units of a product each week. The total weekly cost is given by \(C(x, y) = \frac{1}{6}x^2 + \frac{1}{6}y^2 + 7x + 25y + 700\), where x is the number of units produced in Los Angeles and y is the number of units produced in Denver. How many units should be produced in each plant to minimize the total weekly cost?
# define cost function
cost_function <- function(x,y)
return((1/6)*x**2 + (1/6)*y**2 + 7*x + 25*y + 700)
# define constraint
constraint <- function(y){
x <- 96-y
return(cost_function(x,y))
}
# find the minimum
optimal <- optimize(constraint, interval = c(0, 96))
# Denver optimal unit
D_optimal <- round(optimal$minimum, 2)
# Los Angeles optimal unit
LA_optimal <- 96 - D_optimal
cat(paste('Optimal number of units produced in Denver:', D_optimal), '\n')
## Optimal number of units produced in Denver: 21
cat(paste('Optimal number of units produced in Los Angeles:', LA_optimal), '\n')
## Optimal number of units produced in Los Angeles: 75
Evaluate the double integral on the given region.\(\iint\limits_R(e^{8x+3y})dA;R:2\leq{x}\leq{4}\:and\:2\leq{y}\leq{4}\) Write your answer in exact form without decimals
# define function
f <- expression(exp(8*x + 3*y))
# evaluate double integral
i2 <- integral(f, bounds = list(x = c(2, 4), y = c(2, 4)))
cat(paste('The exact form without decimals:', format(i2$value, scientific = FALSE)))
## The exact form without decimals: 535108549221226560