Linear Algebra

Exercise WILA.C10

In Example TMP the first table lists the cost (per kilogram) to manufacture each of the three varieties of trail mix (bulk, standard, fancy). For example, it costs $3.69 to make one kilogram of the bulk variety. Re-compute each of these three costs and notice that the computations are linear in character.

(trailmix <- matrix(c(7,6,2,6,4,5,2,5,8),nrow=3,ncol=3))
##      [,1] [,2] [,3]
## [1,]    7    6    2
## [2,]    6    4    5
## [3,]    2    5    8
(trailmix_batch <- trailmix/15)
##           [,1]      [,2]      [,3]
## [1,] 0.4666667 0.4000000 0.1333333
## [2,] 0.4000000 0.2666667 0.3333333
## [3,] 0.1333333 0.3333333 0.5333333
(cost <-matrix(c(2.55,4.65,4.80),nrow=3,ncol=1))
##      [,1]
## [1,] 2.55
## [2,] 4.65
## [3,] 4.80
trailmix_cost <- trailmix_batch %*% cost
result <- data.frame(trailmix_cost)
result <- cbind(c("bulk", "standard", "fancy"),result)

Exercise WILA.M70

In Example TMP two different prices were considered for marketing standard mix with the revised recipes (one-third peanuts in each recipe). Selling standard mix at $5.50 resulted in selling the minimum amount of the fancy mix and no bulk mix. At $5.25 it was best for profits to sell the maximum amount of fancy mix and then sell no standard mix. Determine a selling price for standard mix that allows for maximum profits while still selling some of each type of mix.

# Let x as the profit of selling a standard trail mix
# The total profit of selling all mix a day will be
# (4f-3300)*(4.99-3.7) + (-5f+4800)*x + f*(6.5-4.45)
# so we have function for profit
z <- function(f,x) {
    profit <- 7.21*f+4800*x-5*f*x-4257
    return(z)
}

# get deriviative on f and x respectively 
dzdf <- D(expression(7.21*f+4800*x-5*f*x-4257), 'f') 
print(paste("dzdf=",dzdf))
## [1] "dzdf= -"     "dzdf= 7.21"  "dzdf= 5 * x"
dzdx <- D(expression(7.21*f+4800*x-5*f*x-4257), 'x')
print(paste("dzdx=",dzdx))
## [1] "dzdx= -"     "dzdx= 4800"  "dzdx= 5 * f"
# find the root for equation dzdf=0 
df <- function(x) {}
body(df) <- dzdf
xroot <- uniroot(df,c(0,1000))$root

# find the root for equation dzdx=0
dx <- function(f) {}
body(dx) <- dzdx
froot <- uniroot(dx,c(0,1000))$root

# calculate the second deriviatives on f,x, denoting as A, B, and C
(A <- D(D(expression(7.21*f+4800*x-5*f*x-4257), 'f'), 'f'))
## [1] 0
(B <- D(D(expression(7.21*f+4800*x-5*f*x-4257), 'f'), 'x'))
## -5
(C <- D(D(expression(7.21*f+4800*x-5*f*x-4257), 'x'), 'x'))
## [1] 0
# evaluate whether there is extrema
B<-eval(B)
test <- A*C-(B)^2

if (test > 0){
  maxima <- 7.21*froot+4800*xroot-5*froot*xroot-4257
}

if (test < 0){
 print("There is no maxima")
}
## [1] "There is no maxima"