( 5.6, 8.8 ), ( 6.3, 12.4 ), ( 7, 14.8 ), ( 7.7, 18.2 ), ( 8.4, 20.8 )
My best fit was
\[x^{1.7} - 2x\]
R linear fit is
\[0.234x + 3.48\]
Below Im comparing my equation to R’s Estimate Cefficients and lm() and nls().
a<-matrix(c(5.6,8.8,6.3,12.4,7,14.8,7.7,18.2,8.4,20.8), byrow = TRUE, ncol = 2)
a_df<-as.data.frame(a)
colnames(a_df) <- c("x","y")
s1<-summary(lm(a_df$y~a_df$x))
a2_df<-cbind(a_df, 0, 0)
colnames(a2_df) <- c("x","y", "y_estimate", "y_reg" )
# y_lm is R's
# y_reg is mine
for (i in 1:nrow(a2_df))
{
a2_df[i,3]<-round(s1$coefficients[2,1]*a2_df[i,1] + s1$coefficients[1,1],2)
a2_df[i,4]<-1 + round(a2_df[i,1]^1.7 - a2_df[i,1] * 2,2 )
}
par(mfrow=c(1,2))
plot(a_df$x, a2_df$y, main = "Original. Estimate. lm()",
col = "blue",
xlab ="x",
ylab="y" ,type='b')
lines(a_df$x, a2_df$y_estimate, col="red", lty=1, lwd=1 )
abline(lm(a_df$y~a_df$x), col="green", lty=3, lwd=4 ) # non-linear best fit
legend("topleft", legend = c("Original", "Estimate", "lm()"), col = c("blue", "green", "red"), lty = 1)
r_nls <- nls(a_df$y~a_df$x^power, data = a_df, start = list(power = 1), trace = F)
y_nls<-predict(r_nls,newdata = a_df$x )
plot(a_df$x, a2_df$y, main = "Original. Mine. nls()",
col = "blue",
xlab ="x",
ylab="y" ,type='b')
lines(a_df$x, a2_df$y_reg, col="red")
lines(a_df$x, y_nls, col="green")
legend("topleft", # Add legend to plot
legend = c("Original", "Mine", "nls()"),
col = c("blue", "red", "green"),
lty = 1)Compare lm() to mine…
library(kableExtra)
kable(a2_df , caption="Linear Regressions",row.names = FALSE, booktabs=TRUE, table.attr = "style='width:80%;'") %>%
kable_styling(font_size = 12)| x | y | y_estimate | y_reg |
|---|---|---|---|
| 5.6 | 8.8 | 9.04 | 8.50 |
| 6.3 | 12.4 | 12.02 | 11.25 |
| 7.0 | 14.8 | 15.00 | 14.33 |
| 7.7 | 18.2 | 17.98 | 17.74 |
| 8.4 | 20.8 | 20.96 | 21.46 |
\[f(x,y)=24x-6xy^2-8y^3\]
Create the function. Calculate the partial derivatives (set x and y to constants)
library(Deriv)
f1<-function(x,y) {
24*x -6 * x *y^2 + -8*y^3
}
f1_prime<-Deriv(f1) # not clear if you can parse this into seperate functions so i did it manually below
fx<-function(y) 24 - 6 * y^2
fy<-function(x,y) -12 * x * y - 24 * y^2The derivatives are :
\[f_x\prime(x,y) \ = \ 24-6y^2\]
\[f_y\prime(x,y) \ = \ -24y^2 -12xy\]
Its clear that the roots pf \(f_x\prime()\) would be y=2 and y=-2
uniroot(fx,c(0,10))$root
polyroot(c(24,0,-6))## [1] 1.99998
## [1] 2+0i -2+0i
Its also clear that (0,0) would be a root of \(f_y\prime()\) It seems like the roots \(f_y\prime()\) are infinite. Im guessing we would ignore infinite roots.
library(rootSolve)
multiroot(fy, c(0, 0), parms = c(0,0))## $root
## [1] 0 0
##
## $f.root
## [1] 0 0
##
## $iter
## [1] 1
##
## $estim.precis
## [1] 0
Points are (0,2) and (0,-2), Now multiply the 2 second derivatives and plug in those points.
\[f_x\prime\prime(x,y) \ = \ 0\]
\[f_y\prime\prime(x,y) \ = \ -48y -12x\]
Since one of our second derivatives equals zero ( which is a given since the maximum degree of x was 1), our test is inconclusive.
library(animation)
x = seq(-10, 10)
y = seq(-10, 10)
z <- outer(x, y, f1)
saveGIF({
ani.options(interval = 0.2, nmax = 50)
for (i in 1:180) {
theta<-10*i
persp(x,y, z, theta = theta, phi = 0, expand = 0.5, col = "lightblue", main="24x-6xy^2-8y^3",axes=T, ticktype="detailed")
ani.pause()
Sys.sleep(1)
} }, movie.name ="persp.gif",
ani.width = 600, ani.height = 600)Step 1. Find the revenue function R ( x, y )
\[H \ = \ 81-21x+17y\]
\[N \ = \ 40+-11x-23y\]
# returns units sold
fx_units<-function(x,y) 81-21*x+17*y
fy_units<-function(x,y) 40 + 11*x -23*y
# ((81-21*x+17*y)*x) + ((40 + 11*x -23*y)*y)
fxfy_rev<-function(x,y) 81*x-21*x^2 + 28*x*y + 40 * y - 23*y^2
Deriv(fxfy_rev)## function (x, y)
## c(x = 28 * y + 81 - 42 * x, y = 28 * x + 40 - 46 * y)
\[Total Revenue = \frac{\partial(rev)}{\partial(x)} \ = \ 28y + 81 - 42x \ = 0\]
\[\frac{\partial(rev)}{\partial(x)} \ = \ 28x + 40 - 46y \ = 0\]
Isolate y
\[y =\frac{14x+20}{23}\]
Plug in expression to y and solve for x.
\[\ 28\frac{14x+20}{23} + 81 - 42x \ = 0\]
x=4.22 y=3.44
Now invoke our function to get total revenue.
fxfy_rev(4.22,3.44)## [1] 239.7412
Step 2. What is the revenue if she sells the “house” brand for $2.30 and the “name” brand for $4.10?
fxfy_rev(2.30,4.10)## [1] 116.62
\[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?
Note we can already isolate x and y.
\[x=96-y\]
The derivative roots are less than zero.
fxy<-function(x,y) x^2/6 + y^2/6 + 7*x + 25*y + 700
fxy_prime<-Deriv(fxy)
fxy_prime## function (x, y)
## c(x = 0.333333333333333 * x + 7, y = 0.333333333333333 * y +
## 25)
\[f_x\prime(x,y) \ = \ \frac{x}{3} - 7 = 0\]
\[f_y\prime(x,y) \ = \ \frac{y}{3} - 25 = 0\]
Lowest cost : Los Angeles (x) 21. Denver (y) 75.
fxy(21,75)## [1] 3733
\[\int_2^4 \int_2^4 ( e^{8x+3y}) \ dy \ dx\]
Evaluating dy dx…
\[\int_2^4 ( e^{8x+3y}) \ dy \ = \ \frac{e^{8x+3y}}{3} \ = \ \frac{1}{3} \int_2^4 ( e^{8x+3y}) \]
\[ = \frac{1}{3} \ e^{8x+12} \ - \ e^{8x+6} \]
\[ \frac{1}{3} \int_2^4 e^{8x+12} \ - \ e^{8x+6} \ = \frac{1}{24} \int_2^4 e^{8x+12} - e^{8x+6}\]
\[ \frac{1}{24} * (e^{44} \ - e^{38}) - \ (e^{28} \ - e^{22}) \]
e<-exp(1)
1/24*((e^44-e^38)-(e^28-e^22))## [1] 5.341559e+17