# Dowling04:  Derivatives and Economics

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$  

## Preface

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

In Dowling04.wxmx, we use Maxima to discuss concepts based on first and second
derivatives, based mainly on  Ch. 4 of the supplemental text: Introduction to  
Mathematical Economics, 3rd ed, (Schaum's Outline Series), by Edward T. Dowling
 (1992, 2001), McGraw-Hill.
 
This modestly priced text is a bargain with many worked out examples. You should 
compare the  examples worked out "by hand" in this text with what we do using Maxima. 

We have changed some of the symbols used in particular problems.

An approximate pdf translation (using Microsoft print to pdf) is available as Dowling04fit.pdf. 
That pdf file can be searched using Ctrl-F.

Ted Woollett
https://home.csulb.edu/~woollett/
woollett@charter.net
April 25, 2022

## Concavity and Convexity  [4.2]

A function f(x) is CONCAVE at x = a if in some small region close to the point [a, f(a)]
the graph of  the function lies completely below its tangent line. A function is CONVEX  
at x = a if in an area very close  to [a, f(a)] the graph of the function lies completely above
its tangent line. A positive second derivative  at x = a denotes the function is CONVEX
at x = a; a negative second derivative at x = a denotes the  function is CONCAVE at a.
The sign of the first derivative is irrelevant for concavity. 

Here we show a plot of a convex function tangent to the straight line y = x at x = 2. The 
slope of the straight line is +1, and that slope needs to match the local slope of the convex 
curve at x = 2. The example chosen is y = 16 - 11*x + x^3.

y0 : 16 - 11*x + x^3$
wxdraw2d (xlabel = "x", ylabel = "y",  yrange = [0, 4.5], 
    explicit (y0, x, 1, 3), 
    color = black, line_width = 1,explicit (x, x, 1, 3) ,
    label ([" y(x) = 16 - 11*x + x^3", 2, 4.2], [" dy/dx(2) = 1", 2, 3.9],
        [" d^2y/dx^2 (2) = 12 > 0", 2, 3.6] , ["CONVEX", 2, 3.2] ) )$

Here we check the value of the curve and its first and second derivatives at x = 2.

at ([ y0, diff (y0,x), diff (y0, x, 2) ], x = 2);

We define a Maxima function dplot (cval, lyp) in which cval is the chosen value of 'c' in the 
expression y = b + c*x^3 + d*x^4, and lyp is either 1 or -1, giving the slope of the 
straight line which passes through the point (2,2). Inside doplot, the values of b and d are
calculated so that the curve is tangent to our straight line at the point (2,2).

The symbols ys, bs, ... inside the bracket at the start of block are treated as "local
variables", rather than "global variables", and their values are unknown on the global level
after execution of doplot.           

doplot (cval, lyp) := 
block ([ys, bs, ds, x, e1s, e2s, yspp, solns, ysplot, ysppv,yline,ymax],
    if lyp = 1 then (yline: x, ymax : 3.5) 
        else if lyp = -1 then (yline : 4 - x, ymax : 4)
        else return ("the second arg lyp must be +/- 1"),
    /* at this point, bs and ds are unknown  */
    ys : bs + cval*x^3 + ds*x^4,
    [ dys, d2ys ] : [ diff (ys, x), diff (ys, x, 2) ],   
    /* e1s and e2s are equations to be solved for bs and ds */
    [e1s, e2s, yspp] : at ([ys = 2, dys = lyp, d2ys ], x = 2),  
    solns : first ( solve ([e1s,e2s], [bs, ds]) ),
    ysppv : float (at (yspp, solns) ),    
    print ("(2nd derivative of ysplot at x = 2) = ", ysppv),
    if ysppv < 0 then print ("CONCAVE")
       else if ysppv > 0 then print ("CONVEX"),
    ysplot : float (at (ys, solns)), 
    display (ysplot),      
    wxdraw2d (  yrange = [1, ymax], xlabel = " x", ylabel = "y", explicit (ysplot, x, 1, 3), 
    color = black, line_width = 1,explicit ( yline, x, 1, 3) ) )$

Using the Maxima function doplot (cval, lyp), here are two convex cases:

doplot( -5, 1);

doplot (-5, -1);

And here are two concave cases:

doplot( 5, -1);

doplot( 5, 1);

And here is an example of an error return, when a value of lyp
which is not +/-1 is used:

doplot( 5, 2);

## Relative Extrema  [4.3]

A "relative extremum" is a point at which a function is at a "relative maximum or minimum." 
To be  at a relative maximum or minimum at a point a, the function must be at a "relative 
plateau,"  i.e., neither  increasing nor decreasing at a. If the function is neither increasing 
nor decreasing at a, the first  derivative of the function at a must equal zero or be 
undefined. A point in the domain of a function where the derivative equals zero or is 
undefined is called a "critical point" or value. 

For functions f(x) which are differentiable for all values of x in a given region Δx, called
"smooth" or "differentiable" in the given region, one need only consider cases where
df/dx = 0 in looking for critical points.

A relative MINIMUM at x = a then requires f'(a) = 0 and convexity at x = a: (f''(a) > 0).
A relative MAXIMUM at x = a requires f'(a) = 0 and concavity at x = a: (f''(a) < 0).

As an example, consider the function f(x) = 2 x^3 - 30 x^2 + 126 x + 59.
We let f be a Maxima expression (not a Maxima function).

f : 2*x^3 - 30*x^2 + 126*x + 59;
solns : solve (diff(f,x) = 0);

solns[1];

solns[2];

That gives us two critical points. We can then test them for the sign of the second
derivative of f wrt x.

d2f : diff(f,x,2);

[at( d2f, solns[1]), at (d2f, solns[2])];

Thus x = 3 is a relative maximum, x = 7 a relative minimum.
Let's confirm this with a simple plot.

wxdraw2d (xlabel = "x", ylabel = "f", explicit (f, x, 0, 10) )$

A fancier plot:

wxdraw2d (xlabel = "x", ylabel = "f", explicit (f, x, 0, 10) ,
    color = black, line_width = 1, parametric (3, yy, yy, 0, at (f, x = 3)),
    parametric (7, yy, yy, 0, at (f, x = 7)))$

### When solve Doesn't Work, Try find_root

In the example above, we used the Maxima function solve to find 
where f'(x) = 0, in which the function f'(x) was 

diff (f,x);

The Maxima function solve doesn't work with every expression. Note that
solve (expr, var) is equivalent to solve (expr = 0, var).

In Maxima, exp(x) is the same as %e^x.

[exp(2), %e^2];

float(%);

Suppose we use the Maxima function solve to find that value of x such that %e^x = 10.
We first make use of the syntax solve (B, x) which solve interprets as solve (B = 0, x).

solve (exp(x) - 10);

This is consistent with using an explicit equation as the first argument to solve.

solve(exp(x) = 10);

Maxima's log(...) function stands for the natural logarithm, such that e^log(a) = a, and 
with the equation: log (e^a) = a.

[a, exp(log(a)), log (exp (a) ), float (log(10)) ];

Another example in which solve succeeds in returning an explicit solution:

solve(x*exp(x) = 10);

In the following example, no solution is found.

solve(sin(x)*exp(x) = 10);

In the return from solve, the equation is just rearranged, but not solved. The best strategy 
at this point is to transfer all terms of the equation to the left hand side and plot the 
resulting expression, and look for points at which the expression crosses y = 0. If we
let g be assigned the Maxima expression sin(x)*exp(x) - 10, g changes very rapidly with x
and it is easy to miss points where g = 0. Here we look at the interval (x = 5, x = 10).

g : sin(x)*exp(x) - 10$
xmin : 5$
xmax : 10$
wxdraw2d ( xlabel = "x", yrange = [-100, 100],  explicit (g, x, xmin, xmax), 
    color = black, line_width = 1, explicit(0, x, xmin, xmax))$

Here we don't restrict the y range and look at the whole region (x = 0, x = 20).

wxdraw2d ( xlabel = "x",  explicit (sin(x)*exp(x) - 10, x, 0, 20), 
    color = black, line_width = 1, explicit(0, x, 0, 20))$

The expression changes value so rapidly that it is easy to miss points where the expression 
equals zero. If we use find_root(expr, var, vi, vf) the expression must have opposite signs 
at the beginning and end of the given interval [vi, vf] and searches for the first root found, 
starting at the left end vi (assuming vi < vf).

From our first plot we see that the curve crosses y = 0 somewhere between x = 6 and x = 7.

xval : find_root (sin(x)*exp(x) = 10, x, 6, 7);

To see all 16 digits, change the setting of fpprintprec to either 16 or 0:

fpprintprec : 0$
xval;
fpprintprec : 5$

How close to zero is the expression g when x = xval? Remember, Maxima uses the full 16
digit expression behind the scenes. The current value of fpprintprec does not affect the
arithmetic accuracy, just the number of digits printed to the screen.

fpprintprec : 16$
at (g, x = xval);
fpprintprec : 5$

Maxima by default works with 16 digit arithmetic, and every floating point answer is an
approximate answer. 10^(-13) is close enough to zero for me.

## Inflection Points [4.4]

An inflection point is a point on the graph where the function crosses its tangent line 
and changes from concave to convex or vice versa. Inflection points occur only where 
the second derivative equals zero or is undefined. The sign of the first derivative is 
immaterial. For an inflection point to exist at x = a, we need three properties:

1. f'' (a) = 0 or is undefined.  ( f''(x) = diff (f, x, 2) = d²f/dx² )
2. Concavity changes at x = a.  
3. In a plot, the function crosses its tangent line at x = a.

We define the Maxima function inflect (cval, lyp) and construct a curve
                    y = a + b*x + c*x^3 + d*x^4
using cval for 'c' and with lyp = 0 or +/- 1 (only) being the slope of a straight line passing 
through the point (2,2). We calculate the second derivative of y wrt x at x = 1.8 to see if
it is positive or negative, testing the concavity of y for x < 2.

inflect (cval, lyp) := 
block ([ys, as,bs, ds, x, e1s, e2s, e3s, d2ysv, solns, ysplot, yline,ymax],
    if lyp = 1 then (yline: x, ymax : 3.5) 
        else if lyp = -1 then (yline : 4 - x, ymax : 4) 
        else if lyp = 0 then (yline : 2, ymax : 4)
        else return ("the second arg lyp must be 0, or +/- 1"),
    ys : as + bs*x + cval*x^3 + ds*x^4,
    [ dys, d2ys] : [ diff (ys, x), diff (ys, x, 2) ], 
 /*   display (ys, dys, d2ys), */   
    [e1s, e2s, e3s] : at ( [ys = 2, dys = lyp, d2ys = 0], x = 2 ), 
    solns : first (  solve ([e1s,e2s,e3s], [as, bs, ds])),
   /* display (solns), */   
    d2ysv : at (  at (d2ys, solns), x = 1.8),     
    print ("(2nd derivative of ysplot at x = 1.8) = ", d2ysv),
    if d2ysv < 0 then print ("CONCAVE for x < 2 and CONVEX for x > 2")
       else print (" CONVEX  for x < 2 and CONCAVE  for x > 2"),    
    ysplot : at (ys, solns),
    display (ysplot),
    wxdraw2d (  yrange = [1, ymax], xlabel = " x", ylabel = "y", explicit (ysplot, x, 1, 3), 
    color = black, line_width = 1,explicit ( yline, x, 1, 3),
        points ([[2,2]]), parametric (2,yy,yy,1,2) ) )$

Here are some examples:

inflect (5, 0);

inflect (- 5, 0);

inflect (5, 1);

inflect (-5, 1);

## Optimization of  Functions  [4.5]

"Optimization" is the process of finding the relative maximum OR minimum of a function. 
Without  the aid of a plot, this is done with the techniques developed in Sections 4.3 
through 4.4 and outlined  below. Given the usual differentiable function, the first step is
to take the first derivative, set it equal to zero, and solve for the critical point(s). 

This step represents a necessary condition known as the first-order condition. It identifies 
all the points  at which the function is neither increasing nor decreasing, but at a plateau. 
All such points are candidates for a possible relative maximum or minimum. If the second 
derivative is non-zero at the critical points, the work is done.

See the example worked in the section Relative Extrema. In that example the properties 
of the first and second derivatives sufficed.

As another example of "optimization" of a function, consider a case from Prob. 4.9:
    f(x) = - 3 x^4 - 20 x^3 + 144 x^2 + 17

[ f : -3*x^4 - 20*x^3 + 144*x^2 + 17, solve ( diff(f, x) ) ];

d2f : diff(f,x,2);
[ at (d2f, x = - 8), at (d2f, x = 0), at (d2f, x = 3) ];

Thus x = -8 is a relative maximum, x = 0 is a relative minimum, x = 3 is a relative maximum.
Check with a simple plot.

wxdraw2d ( xlabel = "x", ylabel = "f", explicit (f, x, -10, 5) )$

## Successive-Derivative Test for Optimization  [4.6]

1. If the first nonzero value of a higher-order derivative, when evaluated at a critical 
point, is an odd-numbered derivative (third, fifth, etc.), the function is at an inflection point. 
See Problems  4.6(b) and (d) and 4.7(c).  

2. If the first nonzero value of a higher-order derivative, when evaluated at a critical 
point x = a, is  an even-numbered derivative, the function is at a relative extremum at a, 
with a negative value of the derivative indicating that the function is concave and 
at a relative maximum and a positive value signifying the function is convex and at 
a relative minimum. See Problems 4.6(a)  and (c), 4.7(d), and 4.9(c) and (d). 

### Prob. 4.6 (a)

Prob. 4.6(a): f(x) = - (x - 8)^4

[f: - ( x - 8)^4, solve ( diff (f,x) )];

for j:2 thru 5 do
    (djf : diff (f, x, j), 
     print (j, subst (8,x, djf) ) )$

The first non-zero nth derivative (when evaluated at the critical point) here is n = 4,
an even number, indicating a global extremum, and since d^4(f)/dx^4, when 
evaluated at x = 8, in negative, indicating f is locally concave, so x = 8 is a relative maximum.

wxdraw2d ( xlabel = "x",  explicit ( f, x, 4, 12 ))$

The plot shows a very flat looking curve near x = 8.

### Prob. 4.6 (b)

Prob. 4.6 (b):  f(x) = (5 - x)^3

[ f: ( 5 - x)^3, solve ( diff (f,x) )];

for j:2 thru 5 do
    (djf : diff (f, x, j), 
    print (j, subst (5, x, djf) ) )$

The first non-zero derivative (when evaluated at x = 5, the critical point)
is n = 3, an odd number, indicating no relative extremum, but rather an
inflection point at x = 5.

wxdraw2d ( xlabel = "x", explicit ( f, x, 0, 10 ), color = black,
    line_width = 1, explicit (0, x, 0, 10) )$

## Marginal Concepts in Economics [4.7]

Marginal cost (MC) in economics is defined as the change in total cost (TC) incurred 
from the production of an additional unit. Marginal revenue (MR) is defined as the 
change in total revenue (TR) brought about by the sale of an extra unit of a good. Since 
total cost (TC) and total revenue (TR) are both functions of the level of output (Q), 
where Q is the number of units per unit time period (e.g., 10^4 widgits per week)
marginal cost (MC) and marginal revenue (MR) can each be expressed mathematically 
as  derivatives of their respective total functions. Thus, suppose ΔQ is a proposed
increase in units of a good, then the corresponding increase in production cost
would be approximately ΔC = d(TC)/dQ * ΔQ, in which TC is a known function of Q. 
Taking ΔQ = 1, we get the corresponding extra cost MC for one extra unit of the good:
        MC = d(TC)/dQ,      (marginal cost)

A similar argument leads to the definition of marginal revenue:
         MR = d(TR)/dQ.

In the general case, both MC and MR are functions of the level of output Q as well.

Bear in mind that the definition of a "unit" may be 10^6 widgits per week, for example.

Example 1:

[TR : 75*Q - 4*Q^2, MR : diff( TR, Q) ];

[TC : Q^2 + 7*Q + 23, MC : diff (TC, Q) ];

### Example 2

Suppose the price offered per unit is the (inverse) demand function P = 30 - 2 Q. Then 
the total revenue (TR) achieved is (price per unit)*(number of units) = P*Q.

 P : 30 - 2*Q;
TR : expand( P*Q);
MR : diff (TR, Q) ;

Compare marginal revenue MR when Q = 4 and 5:

[ subst (4, Q, MR), subst (5, Q, MR) ];

In this example, the marginal revenue MR (in purple) is less at the higher value of 
level of output Q.

qmax : 10$
wxdraw2d (xlabel = " Level of output Q", ylabel = "P  $ per unit", key_pos = top_left,
    key = "Pd",explicit (P, Q, 0, qmax), color = red, key = "TR", explicit (TR, Q, 0, qmax), 
    color = purple, key = "MR",  explicit (MR, Q, 0, qmax) );

### Dimensions of P, Q, TR and MR

Let [x] stand for the dimensions of x. Then [P] = $/unit and [Q] = units/time-period,
where the unit time-period could be 1 week, for example. Then the units of total 
revenue TR are: [TR] = [P] * [Q] = ($/unit) * (units/time-period) = $/time-period.
Thus on the vertical axis, the numbers could refer to revenue in $/week, for
example, and since [df/dx] = [f]/[x], [MR] = [TR]/[Q] = ($/week)*(1/unit) = $/(week-unit).

## Optimizing Economic Functions [4.8]

The economist can be called upon to help a firm maximize profits and levels of physical 
output and productivity, as well as to minimize costs, levels of pollution, and the use of 
scarce natural resources. This is done with the help of techniques developed earlier and 
illustrated in Example 4 and  Problems 4.17 to 4.23. 

### Example 4

Maximizing profit example. 

Assume revenue (total revenue) R and cost (total cost) C are given functions of level of 
output Q. Then the profit π = R - C. Optimize the profit for the firm. We use Pr to stand
for the profit π.

float (π);

R : 4000*Q - 33*Q^2;
C : 2*Q^3 - 3*Q^2 + 400*Q + 5000;
 Pr : R - C;

Critical values of Q for potential extrema of profit π implied by values of Q such that
dπ/dQ = 0.

solve (diff(Pr, Q));

The negative root Q = - 30 is not physical, since level of production Q is non-negative.
So we have one critical point Q = 20 (units per unit period) and the profit for that level of 
production is

at (Pr, Q = 20);

So π(20) = $39,000.

wxdraw2d( xlabel = "Q", ylabel = "$/unit", yrange = [0, 1.4e5],
    key_pos = top_left, key = "Cost", explicit(C,Q,0,40), 
    color = red, key = "Revenue", explicit(R,Q,0,40),
    color = magenta, key = "Profit", explicit (Pr,Q, 0, 40) , color = black,
    line_width = 1, key = "", parametric (20, yy, yy, 0, at (Pr, Q = 20)))$

## Relating Total, Marginal, & Average

A total product (TP) curve of an input is derived from a production function by allowing 
the amounts of one input (say, capital K ) to vary while holding the other inputs (labor 
and land) constant.  A graph showing the relationship between the total, average, and 
marginal products of a particular input can easily be sketched by using now familiar 
methods, as demonstrated in Example 5. 

### Example 5

### Total Product of Capital TPK

Assume the "total product of capital" TPK is given by TPK = 90 K^2 - K^3, in which TPK is 
the level of unit production, K is level of units of capital used for production (for given level
of use of units of labor and land). Check for minima, maxima, and inflection points of TPK 
as a function of K.

TPK : 90*K^2 - K^3;
dTPK : diff(TPK,K);
d2TPK : diff(TPK,K,2);

factor (d2TPK);

The second derivative of TPK with respect to K is zero for K = 30, indicating an inflection
point.

Find the  critical points in TPK by setting dTPK = 0,

crit_pts : solve (dTPK);
Kpts : map ('rhs, crit_pts);

We can use Kpts, the list [0, 60] of critical points, with a "do loop":

for kval in Kpts do print (kval)$

Look at the signs of the second derivative of TPK at the critical points.

for kval in Kpts do print (kval,", ", at (d2TPK, K = kval))$

For K = 0, TPK'' > 0, TPK is convex, a relative minimum.
For K = 60, TPK'' < 0, TPK is concave, a relative maximum.

TPKmax : at (TPK, K = 60);

We already know K = 30 is an inflection point in TPK. If we did not already know this, we
could use solve or find_root.

solve (d2TPK);

K = 30 is the single inflection point in TPK.

### Average Product of Capital APK

The "average product of capital" APK is defined as APK = TPK/K. We use the simplification 
function ratsimp in defining APK.

APK : TPK/K, ratsimp;
dAPK : diff (APK, K);
d2APK : diff (APK,K,2);

crit_pts : solve (dAPK);
Kpts : map ('rhs, crit_pts);

Look at the sign of APK'' at K = 45.

for kval in Kpts do print (kval, ", ", at (d2APK, K = kval))$

For K = 45, APK'' < 0, the curve is concave, so K = 45 is a relative maximum in the value of
APK.

APKmax : at (APK, K = 45);

### Marginal Product of Capital MPK

The "marginal product of capital" MPK is the first derivative of TPK with respect to K. 
MPK  = TPK' = 3 K (60 - K),    MPK' = TPK'' = 6 (30 - K)

MPK : diff (TPK, K);
dMPK : diff (MPK, K); 
d2MPK : diff (MPK,K, 2);

factor (dMPK);

There is one critical point in the curve MPK, K = 30, where dMPK = 0, and since MPK'' = -6
for all K, the curve MPK is concave and K = 30 is a global maximum.

MPKmax : at (MPK, K = 30);

### Plots

Let's make two plots, one for TPK vs K, and another for APK and MPK vs K.

TPK30 : at (TPK, K = 30);
TPK45 : at (TPK, K = 45);

[TPK30 : subst (30, K, TPK), TPK45 : subst (45, K, TPK)];

lslope : TPK45/45;

yline : lslope*K;

Kmax : TPKmax/lslope, numer;

wxdraw2d (xlabel = "K", ylabel = "TPK",  key_pos = top_left, key = "TPK", 
     explicit (TPK, K, 0, 80), line_width = 1,
   color = brown, key = "", parametric (30, yy, yy, 0, TPK30),
     parametric (45, yy, yy, 0, TPK45),   parametric (60, yy, yy, 0, TPKmax),
color = black, line_type = dashes, explicit (yline, K, 0, Kmax) ), 
  wxplot_size = [880, 380]$

wxdraw2d ( xlabel = "K", ylabel = "APK, MPK", yrange = [-500, MPKmax], 
    color = purple, key = "APK = TPK/K",  explicit (APK, K, 0, 80), 
    color = magenta, key = "MPK",  explicit (MPK, K, 0, 80), 
    line_width = 1, key = "", color = brown, parametric (45, yy, yy, -500, MPKmax),
    parametric (30, yy, yy, -500, MPKmax), parametric (60, yy, yy, -500, MPKmax),
    color = black, explicit (0,K,0,80) ), wxplot_size = [880, 380]$

Note that 

(a) MPK is increasing where TPK is convex (K < 30) and increasing at an increasing rate, 
MPK reaches a maximum where TPK is at an inflection point (K = 30),  and MPK 
is decreasing where TPK is concave (K > 30) and increasing at a decreasing rate.

(b) TPK increases over the whole range where MPK is positive ( K < 60), TPK reaches 
a maximum value where MPK = 0, and TPK is decreasing where MPK is negative.
 
(c) APK reaches a maximum where the slope of a line from the origin to the TPK curve 
is tangent to the TPK curve and where MPK = APK (K = 45).
 
(d) MPK >  APK when APK is increasing,  MPK = APK when APK reaches a maximum, 
and MPK < APK where APK is decreasing.
 
(e) MPK is negative  where TPk is decreasing. 

### Prob. 4.26

Assume a total cost function TC = Q^3 - 18 Q^2 + 750 Q. Plot TC,  the average cost AC, 
and marginal cost MC.

TC : Q^3 - 18*Q^2 + 750*Q;
AC : expand (TC/Q); 
MC : diff (TC, Q);

dTC : diff(TC,Q); 
d2TC : diff(TC,Q,2);

Check the total cost function TC for critical points.

solve(dTC);

There are no real TC critical points since we get complex numbers in the solutions.

Check TC for inflection points.

solve(d2TC);

So Q = 6 is the only TC inflection point (TC'' = 0). The value of TC at the
inflection point is:

at (TC, Q = 6);

What is the sign of TC'' for smaller and larger values of Q?

[at (d2TC, Q = 5),  at (d2TC, Q = 7) ];

In summary we have one TC inflection point at (Q = 6, TC = 4068) with 
TC concave for Q < 6, and TC convex for Q > 6.

Check the average cost function AC for critical points.

dAC : diff(AC,Q);
d2AC : diff(AC,Q,2);

solve (dAC);

The average cost AC has one critical point, Q = 9, and since AC'' = 2 for all Q, the curve
AC is convex everywhere, so the point Q = 9 is a global minimum.

Check marginal cost function MC for critical points.

dMC : diff(MC,Q);
d2MC : diff(MC,Q,2);

factor (dMC);

The marginal cost curve MC has one critical point Q = 6, and since MC'' = 6 everywhere,
the MC curve is convex for all Q, and Q = 6 is a global minimum.

MCmin : at (MC, Q = 6);

wxdraw2d (xlabel = "Q", ylabel = " TC",  key_pos = top_left, 
    key = "total cost TC", explicit (TC, Q, 0, 15), color = brown, 
    line_width = 1, key = "", parametric (6, yy, yy, 0, at (TC, Q = 6) ),
    color = black, line_width = 2, vector ( [6, 7000], [0, -2500] ), 
    label ( ["Inflection Point", 6, 7300], ["Concave", 3, 3000], ["Convex", 9.5, 5500]) )$

wxdraw2d ( xlabel = "Q", ylabel = "  AC, MC", xrange = [0,15], yrange = [600, 900],  
    key_pos = top_left, color = purple, key = "average cost AC", explicit (AC,Q,0,15),
    color = magenta, key = "marginal cost MC",explicit (MC, Q, 0, 15),
    color = brown, line_width = 1, key = "",
    parametric (6, yy, yy, 600, at (MC, Q = 6)),
    parametric (9, yy, yy, 600, at (AC, Q = 9) ))$

Note that 

(1) MC decreases when TC is concave and increasing at  a decreasing rate, MC 
increases when TC is convex and increasing at an increasing rate, and MC is at a 
minimum when TC is at an inflection point and changing concavity.

(2) AC decreases over the  whole region where MC < AC, AC is at a minimum when 
MC = AC, and AC increases when MC > AC. 