Reputation: 9
I am trying to find optimal quantity for which I have to equate differentiation of Total Revenue Equation with Marginal cost. I dont know how to solve for x here. Differentiation works on expression type variable and returns the same, and solve() take numeric equation with only coefficient. I dont want to manually input coefficent.
TR = expression(Quantity * (40- 3*Quantity))
MR = D(TR,"Quantity")
Optimal_Quantity = solve(MR-MC) to get Q
The last line is pseudo code on what I want to achieve, Please help. I can manually enter the values, But wish to make it universal. MC = constant numeric value on RHS
Upvotes: 1
Views: 38
Reputation: 76651
I am not completely understanding but if you want to optimize a function, find that functions derivative then find the derivative's zeros.
TR <- expression(Quantity * (40- 3*Quantity))
MR <- D(TR,"Quantity")
class(MR)
#> [1] "call"
dTR <- function(x, const) {
e <- new.env()
e$Quantity <- x
eval(MR, envir = e) - const
}
MC <- 0
u <- uniroot(dTR, interval = c(-10, 10), const = MC)
u
#> $root
#> [1] 6.666667
#>
#> $f.root
#> [1] 0
#>
#> $iter
#> [1] 1
#>
#> $init.it
#> [1] NA
#>
#> $estim.prec
#> [1] 16.66667
curve(dTR(x, const = MC), from = -10, to = 10)
abline(h = 0)
points(u$root, u$f.root, pch = 16, col = "red")
Created on 2022-11-19 with reprex v2.0.2
To make the function dTR
more general purpose, I have included an argument FUN
. Above it would only evaluate MR
, it can now evaluate any function passed to it.
The code below plots dTR
in a large range of values, from -10 to 100, hoping to catch negative and positive end points. Then, after drawing the horizontal axis, boxes the root between 20 and 30.
dTR <- function(x, FUN, const) {
e <- new.env()
e$Quantity <- x
eval(FUN, envir = e) - const
}
total.revenue <- expression(Quantity * (10- Quantity/5))
marginal.revenue <- D(total.revenue, "Quantity")
marginal.cost <- 1
curve(dTR(x, FUN = marginal.revenue, const = marginal.cost), from = -10, to = 100)
abline(h = 0)
abline(v = c(20, 30), lty = "dashed")
u <- uniroot(dTR, interval = c(20, 30), FUN = marginal.revenue, const = marginal.cost)
u
#> $root
#> [1] 22.5
#>
#> $f.root
#> [1] 0
#>
#> $iter
#> [1] 1
#>
#> $init.it
#> [1] NA
#>
#> $estim.prec
#> [1] 7.5
Created on 2022-11-22 with reprex v2.0.2
Upvotes: 0