x <- c(5.6, 6.3, 7, 7.7, 8.4)
y <- c(8.8, 12.4, 14.8, 18.2, 20.8)
regression_model <- lm(y ~ x)
intercept <- coef(regression_model)[1]
slope <- coef(regression_model)[2]
cat("y =", round(intercept, 2), "+", round(slope, 2), "x\n")
## y = -14.8 + 4.26 x
##2. 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
f <- function(x, y) {
return(24*x - 6*x*y^2 - 8*y^3)
}
df_dx <- function(x, y) {
return(24 - 6*y^2)
}
df_dy <- function(x, y) {
return(-12*x*y - 24*y^2)
}
critical_points <- optim(c(0, 0), fn = function(xy) -f(xy[1], xy[2]),
gr = function(xy) -c(df_dx(xy[1], xy[2]), df_dy(xy[1], xy[2])))$par
cat("Critical Point(s):", critical_points, "\n")
## Critical Point(s): 2.684172e+55 -3.183805e+55
x <- 2.30
y <- 4.10
R <- function(x, y) {
units_house_brand <- 81 - 21*x + 17*y
units_name_brand <- 40 + 11*x - 23*y
return(units_house_brand*x + units_name_brand*y)
}
revenue <- R(x, y)
cat("Revenue $", x, "name brand for $", y, "is $", revenue)
## Revenue $ 2.3 name brand for $ 4.1 is $ 116.62
##4 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) = 1 6 x 2 + 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?
library(optimx)
## Warning: package 'optimx' was built under R version 4.3.2
C <- function(x) {
x_LA <- x[1]
x_Denver <- x[2]
return((1/6)*x_LA^2 + (1/6)*x_Denver^2 + 7*x_LA + 25*x_Denver + 700)
}
result <- optim(c(0, 0), C, control=list(fnscale=1))
x_LA_optimal <- result$par[1]
x_Denver_optimal <- result$par[2]
cat("Optimal units produced in Los Angeles:", x_LA_optimal, "\n")
## Optimal units produced in Los Angeles: -20.99461
cat("Optimal units produced in Denver:", x_Denver_optimal, "\n")
## Optimal units produced in Denver: -75.00166
# Load necessary library
library(pracma)
## Warning: package 'pracma' was built under R version 4.3.1
f <- function(x, y) {
return(exp(8*x + 3*y))
}
x_range <- c(2, 4)
y_range <- c(2, 4)
result <- integral2(f, x_range[1], x_range[2], y_range[1], y_range[2])
result
## $Q
## [1] 5.341559e+17
##
## $error
## [1] 15214781905