# Rate of Growth, Optimal Timing, PV

load(draw)$  set_draw_defaults(line_width=2, grid = [2,2], point_type = filled_circle,
          head_type = 'nofilled, head_angle = 20, head_length = 0.5,
             background_color = light_gray,  draw_realpart=false)$     
 fpprintprec : 5$  ratprint : false$

load ("Econ1.mac");

## Preface

In Dowling09.wxmx  we discuss some of the topics and work some of the problems in 
Ch. 9, "Exponential and Logarithmic Functions", from the text: Introduction to 
Mathematical Economics, 3rd ed., Edward T. Dowling, Schaum's Outline Series, 
McGraw-Hill, 2012.

This text is a bargain, with many complete problems worked out in detail. You should 
compare Dowling's solutions, worked out "by hand", with what we do using Maxima here.

A code file Econ1.mac is available in the same section (of Economic Analysis with Maxima),
which defines some Maxima functions used in this worksheet. Use load ("Econ1.mac");

This worksheet is one of a number of wxMaxima files available in the section
    Economic Analysis with Maxima
on my CSULB webpage.

Edwin L. (Ted) Woollett
https://home.csulb.edu/~woollett/
May 19, 2022

## References

Chiang & Wainwright, Fundamental Methods of Mathematical Economics, 4th ed., 2005,
Ch. 10.

Online Maxima html manual:
https://maxima.sourceforge.io/docs/manual/
                                           maxima_singlepage.html#Function-and-Variable-Index

## Economic Problems, Maximization wrt One Variable

### Prob. 9.20, Maximize Total Revenue TR

Given the (inverse) demand function
   P = 8.25*exp (-0.02*Q), for what value of Q is the total revenue TR = P*Q
maximized? (P = price per unit, Q = number of units per unit time.)

Look for critical points at which total revenue satisfies d(TR)/dQ = 0.

P : 8.25*exp (- 0.02*Q);
TR : P*Q;
solns : solve (diff(TR,Q), Q);

Maxima's solve function has found one critical point, Q = 50 - call this value Qs.

Qs : at (Q, solns);

Price P and  Total revenue TR at the critical value Q = 50:

[Ps, TRex] : at ([P, TR],solns);

Check the sign of the second derivative of TR wrt Q at the critical point.

at ( diff(TR, Q, 2), solns);

The second derivative of TR wrt Q is less than zero at the critical value of Q, implying a
relative maximum. A simple plot confirms the conclusion.

wxdraw2d (xlabel = "Q", yrange = [0,160], title = "TR vs Q", 
    key_pos = bottom_right, key = "TR", explicit (TR, Q, 0, 100), 
    color = black, line_width = 1, key = "Q = 50",
    parametric (Qs, yy, yy, 0, TRex))$

## Economics Rate of Growth Problems

The growth (rate) G of a function y = f(t) is defined as

     G = f'(t)/f(t) = y'/y = d( ln(y) )/dt ~ (Δy/y)/Δt

The growth rate G is then the fractional increase in y, Δy/y, per unit time period.
For example if Δy/y = 0.01 and Δt = 1 day, the growth rate would
be 1% per day. Growth rates are usually described in terms of the language
percent per unit time period.

### Example 9: Growth Rate Using Two Methods 

Find the growth rate of V = P exp( r*t), where P = constant using both methods.
Method 1 is V'/V and method 2 is d(ln(V))/dt.

kill(P)$
V : P*exp (r*t);
G1 : diff (V, t)/ V;
G2 : diff (log (V), t);

We get the same answer with both methods.

### Prob 9.23 Ag Sector Revenue  R; Growth Rate of a Product

Suppose the price P of agricultural goods is going up by 4% per year, the quantity Q 
by 2% per year. What is the annual rate of growth of revenue R = P*Q from the 
agricultural sector?

If we calculate the growth rate of the product P*Q via d(ln(P*Q))/dt we will get just the
sum of the growth rates of P and Q. Here we show this symbolically:

depends ([P, Q], t);

diff (log (P*Q), t), expand;

We can now remove the dependency set above:

remove ([P, Q], dependency);

diff (log (P*Q), t), expand;

With revenue R = P*Q, growth rate of revenue GR = GP + GQ = growth rate of price
plus the growth rate of quantity = 4%/yr + 2%/yr = 6%/yr. 

In general, the growth rate of a function which is a product of components is the sum of 
the growth rates of the components. G(A*B*C) = G(A) + G(B) + G(C), etc.

### Prob 9.24: Input Costs C = P*Q

A firm experiences a 10% per year increase in the use of inputs Q at a time when the 
input costs (input price per unit) P are rising by 3% per year. What is the rate of increase 
in total input costs C =  P*Q?

As we learned in Prob. 9.23, we add up the percent increases per year for the 
components of an arithmetic product to get 13% per year increase in total costs. 
GC = 13%/yr.

### Prob 9.25:  Rate of Growth of Per Capita Employment 

Employment opportunities E are increasing by 4% per year and population P by 2.5%.
What is the rate of growth of per capita employment PCE?

Per capita employment PCE = E/P = number of people employed divided by the total
population of people.

[E,P];

We can show the growth rate of PCE = E/P equals the growth rate of E minus the 
growth rate of P.

depends ([E, P], t);

diff (log (E/P), t), expand;

which completes the proof.

remove ([E, P], dependency);

diff (log (E/P), t), expand;

In general, the % growth rate of (A/B) is the % growth rate of A minus the % growth 
rate of B.

Hence the growth rate of per capita employment is 4%/yr - 2.5%/yr = 1.5%/yr.

These rules can obviously be generalized to the growth rate of A*B/(C*D), for example.

[A,B,C,D];

depends ([A,B,C,D], t);

diff (log (A*B/(C*D)), t), expand;

remove ([A,B,C,D], dependency);

### Prob 9.26: Rate of Growth of Per Capita Income

National income Y is increasing by 1.5% per year and population P by 2.5% per year.
What is the rate of growth of per capital income PCY = Y/P?

Per capita income is falling by 1% per year.

### Prob 9.27  Export Earnings E; Growth Rate of a Sum

A country exports two goods, copper c, and bananas b, where earnings in millions of 
dollars are: c(t0) = 4, b(t0) = 1.  If c grows by 10% per year and b grows by 20% per
year what is the rate of growth of export earnings E = c + b?

By definition GE = d( ln(E) )/dt = (dE/dt)/E.

depends ([c, b], t);

diff (log (c + b), t), expand;

remove ([c, b], dependency);

The first term can be written as [c/(c+b)]*(1/c)*dc/dt = [c/(c+b)]*d(ln(c))/dt. 
The second terms can likewise be written as [b/(c+b)]*d(ln(b))/dt.
In summary:

GE = d ln(E)/dt = [b/(b+c)]*Gb + [c/(b+c)]* Gc

In words, the growth rate of a function involving the *sum* of other functions is the 
sum of the weighted average of the growth rate of the other functions.

Evaluating at t0, we get GE = [1/(1+4)] * Gb + [4/(1+4)]*Gc, or
  GE = (1/5)*Gb + (4/5)*Gc = (1/5)*20% + (4/5)*10% = 4% + 8% = 12%

Export earnings E grow at 12% per year.

### Prob. 9.28, Growth Rate of a Sum

A company derives 70 percent of its revenue from bathing suits, 20 percent from bathing 
caps,  and 10 percent from bathing slippers. 

If revenues from bathing suits increase by 15 percent,  from caps by 5 percent, and 
from slippers by 4 percent, what is the rate of growth of total  revenue? 

From our work with Prob. 9.27:

Gr = 0.7*0.15 + 0.2*0.05 + 0.1*0.04

Gr : 0.7*0.15 + 0.2*0.05 + 0.1*0.04;

Growth rate of total revenue is 11.9%.

### Prob 9.30 Growth Rate of Profits

Find the rate of growth of profits at t = 8, if π(t) = 250,000 exp (1.2*t^(1/3)).

Pr : 2.5e5*exp(1.2*t^(1/3));

G : diff (log(Pr),t);

at (G, t  = 8);

The rate of growth of profits at t = 8 is thus 10%.

## Optimal Timing Problems

Exponential functions are used to express the value of goods that appreciate or depreciate 
over  time. Such goods include wine, cheese, and land. Since a dollar in the future is worth 
less than a  dollar today, its future value must be discounted to a present value. Investors 
and speculators seek  to maximize the present value of their assets, as is illustrated in 
Example 10 and Problems 9.31  to 9.34.  

See the beginning of the section below with the title: Optimal Timing: Chiang &
Wainwright, Ch. 10, for a careful description of the use of a discount factor in these
problems to take into account the time value of money.

### Example 10: Optimum Time to Sell Stored Cheese

Assume that the value ($ per unit) of cheese that improves with age is given by 
       V(t) = 1400*(1.25)^t^(1/2),
where V(t) more precisely is the $ per unit of cheese received at time t, where t = 0 
is the time aging begins. 

If the cost of capital under continuous compounding is 9 percent a year and there is no 
storage cost for aging the cheese in company caves, how long should the company  
store the cheese before selling?  The company must evidently maximize the present 
value P(t) of the cheese:
     P(t) = V(t) exp( - r*t)
in which r is the discount rate 0.09.

V : 1400*1.25^sqrt(t);
P : V*exp(- 0.09*t);
gradP : diff (P,t);

solve won't be able to solve gradP = 0 for t in this form. The value of t for which ln(P) is 
maximum will also be the value of t for which P is maximum because ln(x) is a monotonic
function of x. Where x increases, so does ln(x). Where x decreases, so does ln(x).

lnP : log(P);

We need to change the value of the flag logexpand to 'all' to get Maxima to automatically
use the replacements 
    ln (a*b) --> ln(a) + lnb),    ln(a^x) --> x*ln(a),  etc.

logexpand : all;

[log(a*b),log (a^x)];

Now we get an expanded form we can work with:

lnP : log(P);

gradlnP : diff (lnP, t);

solns : solve (gradlnP, t), numer;

tcrit : at (t, solns[1]);

The first derivative of ln(P) wrt the time t is zero for t ~ 1.54 years.

Check the sign of the second derivative of ln(P) at this value of t.

grad2lnP : diff(gradlnP, t);

We see that the second derivative of ln(P) wrt t is negative for all positive values of t, 
which is the signal that  we have found a maximum of the function ln(P(t)).

The present value of the cheese is $1,607.67 at t = 1.54 yr.

Pmax : at (P, solns);

At t = 0, P = V(0)= 1400, less than Pmax.

at (P, t = 0);

wxdraw2d (xlabel = "t", ylabel =  "P", yrange = [1350,1620],
    title = "P(t) vs t", explicit (P, t, 0, 3),color = black, line_width = 1,
    key_pos = bottom_right, key = "tcrit = 1.54", parametric(tcrit, yy,yy,0,Pmax))$

Let's also plot ln(P) versus t.

lnPmax : at (lnP, solns), numer;

at (lnP, t = 0),numer;

wxdraw2d (xlabel = "t", yrange = [7.2, 7.4], title = " ln( P ) vs t",
     explicit ( lnP, t, 0, 3), color = black, line_width = 1, key_pos = bottom_right,
 key = "tcrit = 1.54",  parametric(1.537, yy, yy, 0, lnPmax))$

We see that ln (P) has a maximum at the same point that P(t) has a maximum.

### Prob 9.31, Maximize Present Value of Cut Glass

Cut glass currently worth $100 is appreciating in value V(t) according to the formula
  V(t) = 100*exp (√t ) = 100*exp(t^(1/2)).
How long should the cut glass be kept to maximize its present value if under continuous
compounding  (a) r = 8% per year,  (b) r = 12% per year?

Ignore storage costs.

The present value P(t) is V(t)*exp (- r*t). P(t) should be maximized by choosing the time
at which P(t) has a relative maximum.

(a) r = 8% per year = 0.08 per year

V : 100*exp(sqrt(t));
Pa : V*exp (- 0.08*t);
solns : solve (diff (Pa,t)), numer;

tcrit_a : at (t, solns);

Pamax : at (Pa, solns);

at (Pa, t = 0);

The present value reaches $2,276 after 39.06 years with r = 8%/year.

Check second derivative sign.

d2Pa : diff (Pa,t,2), ratsimp;

at (d2Pa, solns);

The second derivative of P(t) is negative at the critical time 37.06 years, indicating a
relative maximum at t = tcrit.

(b) r = 12% per year = 0.12 per year. V(t) is the same function. P(t) needs to be updated.

Pb : V*exp (- 0.12*t);
solns : solve (diff (Pb,t)), numer;
Pbmax : at (Pb, solns);

tcrit_b : at (t, solns);

The present value reaches $803.12 after 17.36 years with r = 12%/year.

Check the sign of the second derivative of P(t) at the critical time 17.36 yrs.

d2Pb : diff (Pb,t,2), ratsimp;

at (d2Pb, t = tcrit_b);

Again the negative sign indicates a relative maximum.

The higher the discount rate r, the shorter the storage time of the cut glass before sale.

wxdraw2d (xlabel = "t years", xrange = [0,50],yrange = [0,2400], key_pos = top_left,
    title = "Present value of cut glass at time t",
    key = "r = 8%/year", explicit (Pa, t, 0, 50), color = red, 
    key = "r = 12%/year", explicit (Pb, t, 0, 50), color = black, line_width = 1,
   key = "tmax = 39", parametric (tcrit_a, yy, yy,0,Pamax),
    key = "tmax = 17.4", parametric (tcrit_b, yy, yy, 0, Pbmax))$

### Prob 9.32, Maximize Present Value of Land

Land bought for speculation is increasing in value according to the formula
    V(t) = 10000 exp (t^(1/3) ). 
The discount rate under continuous compounding is 9% per year. How many years should
the land be held to maximize the present value?

P : 1e4 * exp(t^(1/3)) * exp (- 0.09*t);
gradP : diff (P, t);
solns : solve (gradP, t);

Setting dP/dt equal to zero leads to t^(2/3) = 1/(3*0.09) = 3.7037. Then raising both
sides to the (3/2) power allows one to get t = 3.7037^(1.5) = 7.12778, which is the
first solution found by solve.

soln : float (solns[1]);

tcrit : at (t, soln);

Pmax : at (P, soln);

Check sign of the second derivative at t = tcrit.

subst (soln, diff (P, t, 2));

Which confirms that the present value of the land reaches a maximum of 
$36,074.50 after 7.13 years.

wxdraw2d (xlabel = "t years", yrange = [0, 4e4], 
    title = "Present Value of Land",
    key_pos = bottom_left, key = "PV vs t",
    explicit (P, t, 0, 10), color = black, line_width = 1, key = "tcrit = 7.13 yr",
    parametric (tcrit, yy, yy, 0, Pmax), key = "P = $36,074",
    points ([ [tcrit, Pmax] ]))$

### Prob 9.32, Maximize Present Value of Art Collection

The art collection of a recently deceased painter has an extimated value
   V(t) = 200,000* 1.25^( t^(2/3) ).
How long should the executor of the estate hold on to the collection before putting it
up for sale if the discount rate under continuous compounding is 6 per cent per year?

V : 2e5*1.25^(t^(2/3));
P : V*exp (- 0.06*t);
solns : solve (diff (P, t));

grind(solns)$

solve returns an "implicit solution", so let's take the natural logarithm of P, call it lnP, 
and find the value of t for which ln(P) has a relative maximum.

logexpand : all;

lnP : log(P);

at (lnP, t = 0.0);

at(P, t = 0.0);

Look for the time tmax for which lnP has a relative maximum.

solns : solve ( diff (lnP, t), t), numer;

tmax : at (t, solns);

[Pmax, lnPmax] : subst (solns, [P, lnP]);

Check the sign of the second derivative of ln(P).

subst (solns, diff (lnP, t, 2));

The negative sign indicates a relative maximum.

Let's make a plot of ln(P), and then P, versus time t in years.

wxdraw2d (xlabel = "t years", yrange = [12.2, 12.7], title = " ln( P ) vs t",
     explicit ( lnP, t, 0, 30),  color = black, line_width = 1, key_pos = bottom_right,
    key = "tmax = 15.24 yr", parametric(tmax, yy, yy, 12.2, lnPmax),
    key = "lnPmax = 12.66", points ( [ [tmax, lnPmax] ] ))$

wxdraw2d (xlabel = "t years", yrange = [1e5,3.5e5],  title = "P(t) vs t",
 explicit (P, t, 0, 30), color = black, line_width = 1, key_pos = bottom_right,
  key = "tmax = 15.24 yr",    parametric(tmax, yy,yy,0,Pmax),
    key = "Pmax = $316,000", points ([ [tmax, Pmax] ]))$

## Cobb-Douglas Demand Function [9.8]

A demand function expresses the amount of a good a consumer will purchase as a 
function of  commodity prices and consumer income. A Cobb-Douglas demand function 
is derived by maximizing a Cobb-Douglas utility function subject to the consumer’s income. 
Given the utility function function u = x^a y^b, a measure of the desireability of consuming
the amount x of good 1 together with the amount y of good 2, and given the price px per
unit of good 1 and price per unit py of good 2, and given the consumer's budget constraint
px*x + py*y = M, in which M is the money available to the consumer, find the values 
(x,y) for which the utility it maximized. 

We have discussed the analysis of maximization of  a function of two variables subject
to an "equality constraint" in Dowling05.wxmx, and also the use of our Maxima function
optimum (f, varList, constraints), defined in Econ1.mac. L is the Lagrangian function and
λ is the Lagrangian multiplier.

u: x^a*y^b;
g : px*x + py*y;
L : u + λ*(M - g);

gradL : jacobian ([L],[x, y, λ])[1];

solns : solve (gradL, [x,y,λ]);

soln : solns[1];

Let cp be the list of replacement values [x = xs, y = ys] defining a critical point.

cp : rest (soln, -1);

If we assume this critical point corresponds to a relative maximum in the value of
u (x,y), we can call that value umax:

umax : at (u, cp);

For a "strict" Cobb-Douglas function, a + b = 1, and the critical point cp reduces to:

cp_strict : at (cp, b = 1 - a);

### Example 11 , Use of optimum (u, [x,y], M - g)

Given the solution found above for the Cobb-Douglas demand function, use the case in
which a = 0.3, b = 0.7, and the income constraint is M = 200, to find the critical 
point (xs, ys) given prices:
   (A)  px = 5, py = 8
   (B)  px = 6, py = 10

Since evidently a + b = 1, we can use the solutions given by cp_strict.

u_strict : at (u, [a = 0.3, b = 0.7]);

For case (A):

cpA : at (cp_strict, [a = 0.3, M = 200, px = 5, py = 8]);

uA : at (u_strict, cpA);

Econ1.mac defines a Maxima function optimum which can be used to verify the nature of
the critical point found by solve, useful for problems which involve "equality constraints".
For our problem the appropriate syntax is optimum (u, [x,y], 200 - g). The optimum
function examines the "leading principal minors" of the "bordered Hessian matrix" which
Dowling covers in Ch. 12. 

optimum (x^0.3*y^0.7, [x, y], 200 - 5*x - 8*y);

Repeat the above for case B, px = 6, py = 10, we can edit part A input:

cpB : at (cp_strict, [a = 0.3, M = 200, px = 6, py = 10]);

uB : at (u_strict, cpB);

at(uB, [ a = 0.3,b = 0.7]);

optimum (x^0.3*y^0.7, [x, y], 200 - 6*x - 10*y);

## Optimal Timing: Chiang & Wainwright, Ch. 10, Sec 10.6

### A Problem of Wine Storage

We loosely quote Chiang/Wainwright, Sec. 10.6:
"Suppose that a certain wine dealer is in possession of a case of wine which he can
either sell immediately (t = 0) for $K or else store for some length of time t and then
sell at a higher value." The growing  value V(t) of the wine is assumed to be 
described by the model:
                   V(t) = K exp(√t )
so that V(0) = K. 

Since the cost of wine is a "sunk cost", the wine is already paid for by the wine dealer, 
and since we assume there is no extra "storage cost", to maximize profit is to maximize 
the sales value V. Each value of V at a specific point in time represents a dollar sum 
receivable at a different date and, because of the time value of money, is not directly 
comparable with the V value at a different date. The way out of this difficulty is to 
"discount" each V value to its "present-value" equivalent (the value at time t = 0), for 
then all the V values will be on a comparable footing.

We assume the interest rate (on the continuous compounding basis) is at the level of r.
Then with r = decimal interest rate, we can write the present value as
                    A(t) = V(t) exp (- r t)
where A(t) is the present-value of V."

The curve A(t) reaches a local maximum when dA/dt = 0 and d²A/dt² < 0 so
the slope is locally a decreasing function of t. We define A as a Maxima expression 
depending on the symbol t.

V : K*exp(sqrt(t));

The discounted present value of the case of wine A(t) as a Maxima expression:

A : V*exp(-r*t);

We can write A = K exp(f(t)), where f(t) = √t - r t.
dA/dt = K d( exp(f) )/dt = K exp(f) df/dt = A df/dt = A ( (1/(2*sqrt(t)) - r ),
since the derivative of an exponential is the exponential times the derivative of
its argument.

Let dA stand for dA/dt.

dA : diff(A,t);

dA/A;

We see that dA, standing for dA/dt, is equal to A(t) * (1/(2*sqrt(t)) - r), and since A(t) # 0, 
the condition dAdt = 0 reduces to dA/A = 0.

assume (r > 0)$
solns : solve (dA/A,t);

If we tell Maxima to consider t > 0 (as well as r > 0), we can get a solution directly from
Maxima's solve function applied to dA:

assume (t > 0)$
solve (dA, t);

Let d2A stand for the second derivative of A wrt time t.

d2A : diff (A, t, 2);

at (d2A, solns);

Since K > 0, we thus see that d²A/dt² < 0, the local slope dA/dt is a decreasing
function of time, which passes from a positive slope to a negative slope as t increases
through the special time ts = 1/ (4*r^2), which thus  corresponds to a local maximum 
in A(t).

As a numerical example, if the interest rate on money is 10% per year, then the 
decimal rate is r = 0.1, r^2 is 1/100, ts = 100/4 = 25 yr.

subst(0.1, r, solns);

For use in our plot, define Amax as the value of A(t)/K at the critical time t = 25.

Amax : at (A/K, [t = 25, r = 0.1] );

Let's make a plot of A/K as a function of time for this example using r = 0.1 /year.

wxdraw2d ( title = " A(t)/K for r = 0.1/yr",  xlabel = "t years", yrange = [0, 14],
    key_pos = bottom_right, key = "A/K", explicit (subst (0.1, r, A/K), t, 0,  50),
     color = black, line_width = 1, key = "t = 25 yr ", parametric (25, yy, yy, 0, Amax),
    key = "A/K = 12.18", points([ [25, Amax] ]))$

Since exp(0) = 1, A(0) = K, A(0)/K = 1.

Let's next make of plot of interest rate per year versus time t which will allow us to find a 
graphical solution of the optimal time to sell from the equation dA/dt = 0.

solve (dA/A, r) attempts to find the solution(s) to the equation dA/A = 0 for r in terms of 
any other variables. Since dA stands for dA/dt and A is positive, we are finding the 
value(s) of r for which dA/dt = 0.

soln : solve (dA/A, r);

Rate : at (r, soln);

wxdraw2d ( title = " Rate = 0.5/sqrt(t) ",  xrange = [0, 50], yrange = [0, 0.5],
    xlabel = "t", ylabel = "decimal Rate/year",  key = "Rate", 
    explicit (Rate, t, 0.5, 50), color = black, line_width = 1,
    key = "r = 0.1/yr", explicit (0.1, t, 0.5, 50), color = red, key = "t = 25 yr",
    parametric (25,yy, yy, 0, 0.1))$

We see from this latter plot that the horizontal line, rate = 0.1/yr, intersects the curve of 
r(t) = Rate at only one value of time, t = 25 yr. This latter plot is a "graphical solution" to 
this problem of optimal time to sell for a given time value of money. The greater the 
prevailing interest rate on money, the smaller the time one should wait to sell.

### Present Value of a Stream or Flow of Future Values

In the case of a *single* future value V received in the future at time t, the 
present value PV (t=0) is given by
    PV = V*(1+ i)^(-t)    [ discrete case ]
    PV = V*exp(-r*t)      [continuous case]

If instead of a single future value we have a stream or flow of future values - a series of 
revenues receivable at various times or of cost outlays at various times, we simply add up 
the present values of each future value.

For example, suppose there will be end of year revenues R1 at the end of year 1, R2 at 
the end of year 2, and R3 at the end of year 3. The present (t=0) values of these three 
future values are, respectively
    R1*(1 + i)^(-1),  R2*(1 + i)^(-2), and R3*(1 + i)^(-3),
with the total present value written as a sum:
   PV = ∑ R[t]*(1 + i)^(- t)
in which t takes on the values t = 1, 2, 3.

We next suppose we have a continuous cash flow, a continuous revenue stream at the 
rate of R(t) dollars per year. This means that at the instant t = t1 the rate of flow is R(t1) 
dollars per year, and at t = t2 the rate of flow is R(t2) dollars per year. In the small time 
interval (t, t + dt), the amount received is R(t)*dt (with the variable t the time in years). 

An  example of a small value of dt is 
          dt = 3.17*10^(-14) years. = 1 microsecond.

If we then add up the small (present) values R(t)*exp(-r*t)*dt from  t = 0 to t = 3 years, 
we get
    PV = integrate (R(t)*exp(-r*t), t, 0, 3) = ∫R(t)*exp(-t*t)*dt, with the limits of integration 
t = 0 to t = 3.

In the special case that R is a constant, independent of time t, we can simplify this as 
PV = R*integrate (exp(-r*t), t, 0, 3)

We then just have to evaluate the definite integral:

integrate (exp (-r*t), t, 0, 3), ratsimp;

If we replace 3 by tf:

integrate (exp (-r*t), t, 0, tf), ratsimp;

grind(%)$

### Including the Cost of Storage

Chiang & Wainwright continue the wine in Ch. 14, Sec. 14.5, from which we loosely quote:

"Let the purchase cost of the case of wine be the amount C, incurred at the present time 
t = 0. Its future sale value has the present value V(t) exp(-r t). Whereas the future sale of 
the case of wine is a one-time event, the present value of the storage cost, based on a 
rate of s dollars per year will be an accumulation of a "stream" of costs out to a time t, 
given by the integral over [t' = 0 to t' = t]  ∫dt' s*exp(- r*t') = integrate (s*exp(- r*tp), tp, 0, t), 
where tp stands for a dummy variable of integration t'

PVStoragecost : integrate (s*exp(-r*tp), tp, 0, t), ratsimp,factor;

which is mathematically equivalent to (s/r) [1 - exp(-r t)].

Thus the "net present value" of the case of wine is the discounted revenue of the sale at 
time t, minus the present value of the storage cost, minus the purchase cost (purchase 
at time t = 0):
    N(t) = V(t) exp( - r t) - C  - PVStorageCost  
           = V(t) exp (- r t) - C - (s/r) ( 1 - exp (- r t) )
and we want to maximize the net present value of the case of wine, and we hence 
require dN/dt = 0 and d²N/dt² <  0.

To avoid the explicit form of V(t), let's temporarily let U replace V and tell Maxima to 
treat U as some (unspecified) function of t.

depends (U,t);

Let N1 then be the net present value of the case of wine in terms of the symbol U.

N1 : U*exp(-r*t) - C - (s/r)*(1 - exp(-r*t));

To maximize the net present value, we set the first derivative equal to zero and solve for 
the time that occurs. Let dN1 stand for dN1/dt.

dN1 : diff (N1, t),factor;

Since exp (-r*t) # 0 we conclude that the optimum time to sell is given by the solution of 
the equation  dN = dV/dt - r V(t) - s = 0

dN : diff (V,t) - r*V - s;

Let's simplify dN using x = sqrt(t),  and calling the result dN2.

dN2 : subst(x,sqrt(t), dN);

Next multiply dN2 by 2*x, and divide by K, call the result dN3. This  won't change the 
location of the root given by the eqn. dN3 = 0.

dN3 : expand ( 2*x*dN2/K);

Third step, replace the ratio (s/K) by the parameter a. Then s = a*K. Also replace r by 0.1 
to match the 10%/yr interest rate we used above.  Call the result dN4.

dN4 : at (dN3, [s = a*K, r = 0.1]);

Fourth step, define a Maxima function f(a,x) , using two single quotes (') in front of dN4 
to force evaluation.

f(a,x) := ''dN4;

Look at a = 1 (s = K) and x = 4 (t = 16) as an example. Remember we want to find the 
value of x for which f(a,x) = 0.

f(1,4), numer;

Let's set numer to true to force floating point numbers (the default is false).

numer:true$

A numerical exploration of the values of f(1,x).

xL : makelist  (x, x, 4.1, 4.7, 0.1);

map (lambda ([xx],f(1,xx)), xL);

[f(1,4.5), f(1, 4.6)];

Let's use find_root for a = 1 (s = K) and tell find_root to expect a root in the x 
interval (4.5, 4.6)

xroot : find_root (f(1,x), x, 4.5,4.6);

f(1,xroot);

Here is a simple plot of f(1,x) near the root location

wxdraw2d ( xlabel = "x", yrange = [-1, 1],
    explicit (f(1, x), x, 4.4, 4.6), color = black,
    line_width = 1, explicit (0,x,4.4,4.6),
    key = "x = 4.5002", parametric (xroot,yy,yy,-1,0))$

Recall that x = t^(1/2), so x^2 = t.

xroot^2;

So if s = K, (a = 1), the time when the net present value (including the discounted 
storage costs) is at its maximum is about 20.25 years.

Consider doubling the storage cost parameter s: from s = K to s = 2 K which corresponds 
to a = 2:

xroot2 :  find_root (f(2, x),x, 0, 6);

Thus if a = 2, s = 2*K,  net PV is max for t = 

xroot2^2;

which is about 1/10 year or about one month.

Restore the default value of numer:

numer : false;

### A Problem of Timber Cutting

We again loosely quote Chiang & Wainwright, Sec. 10.6.

What is the best time to cut timber?
"Assume the value of timber already planted on some given land is V(t):
        V(t) = 2^(√t)
in units of $1,000. Assuming a prevailing decimal interest rate r and neglecting the 
maintenance cost during the timber growth process, what is the optimal time to cut the 
timber for sale?

As in the case of wine problem, convert V into its present value A(t),
   A(t) = V(t) exp(- r t)

V : 2^sqrt(t);

A : V*exp (- r*t);

Let dlnA stand for the derivative of ln(A) wrt t:
d(ln(A)/dt = (1/A) dA/dt is the basic rule for the first derivative of a natural logarithm.

dlnA : diff (A,t) / A, ratsimp;

expand (dlnA);

Look for the critical values of t for which dlnA = 0.

assume (r > 0)$
solns : solve (dlnA, t);

tcrit : at (t, solns);

The curve A(t) reaches a local maximum when dA/dt = 0 and d²A/dt² < 0.
Let d2A stand for the second derivative of A wrt time t.

d2A : diff (A, t, 2), ratsimp;

Look at the value of d2A at t = tcrit.

at (d2A, t = tcrit), ratsimp;

We see that the second derivative of A wrt time t is negative at the critical time
t = ( ln(2)/(2*r) )^2, which assures us that we have found the time of maximum present 
value of the timber.

As a numerical example, assume the interest rate is 5%, r = 0.05,

tmax : at (tcrit, r = 0.05), numer;

Amax :   at (A, [r = 0.05, t = tmax]);

Thus with a 5% interest rate, the present value (PV) of the timber is maximized
at t = 48 years at $11,048. The whole project will not be worth while if the planting cost of 
the timber is more than $11,048. (The upkeep cost is assumed to be negligible.)

wxdraw2d ( title = " A(t) for r = 0.05/yr", xlabel = "t", yrange = [0, 12],
    key_pos = top_left,  explicit (subst (0.05, r, A), t, 0,  60), 
    color = black, line_width = 1,   key = "t = 48 yr", 
    parametric (tmax, yy, yy, 0, Amax), key = "Amax = $11,048",
    points ([ [tmax, Amax] ]))$