# MAPLE ASSIGNMENT 12
# CONSTRAINED OPTIMIZATION
# The firet part of this assignment illustrates the example discussed in
# class. The constant c has been 
# put equal to 10 but you can change that if you like.
> f:=3*x*y+4*y^2;
> plot3d(f,x=-5..5,y=-5..5);
> g:=x^2+y^2;
> L:=f+lambda*(10-g);
> with(linalg):
> gradL:=grad(L,[x,y,lambda]);
> statpt:=solve({gradL[1],gradL[2],gradL[3]},{x,y,lambda});
# These are the 4 stationary points of the problem. We also need the
# value
# of f at each stationary point.
> f1:=subs(statpt[1],L);
> f2:=subs(statpt[2],L);
> f3:=subs(statpt[3],L);
> f4:=subs(statpt[4],L);
> with(plots):
# The next plot shows the level curves of f being tangent to the
# constraint curve at the local maxs and 
# mins of the constrained optimization problem.  If you click on the
# plot and then on the constrained option in the projection menu, the
# circular constraint will be made into an exact circle. 
> implicitplot({f=f1,f=f3,g=10},x=-5..5,y=-5..5);
> bH:=hessian(L,[x,y,lambda]);
# Now check the SOC first at stationary point 1, then 
# edit the commands to check at statpt[3]. Which point is the local max?
# the local min? You already know the answer from calculating the values
# of f at the extreme points above.
> dbH:=det(bH);
> subs(statpt[3],dbH);
# A CONSUMER MAXIMIZING UTILITY
# The first part of this exercise is to show that the U used here is a
# satisfactory utility function.
> U:=(sqrt(x)+sqrt(y))^2;
> plot3d(U(x,y),x=0..5,y=0..5-x);
> with(linalg):
> gradU:=grad(U,[x,y]);
> HU:=hessian(U,[x,y]);
> simplify(HU[1,1]);
> det(HU);
> V:=lambda*U;
> HV:=hessian(V,[x,y,lambda]);
> bHU:=subs(lambda=1,%);
> det(bHU);
# This utility function has positive diminishing marginal utilities and
# is concave but not strictly concave. The indifference curves are
# strictly convex because MUx  > 0 and U is strictly quasiconcave. Now
# you can solve for the consumer's demand functions..
> L:=U+lambda*(m-p*x-q*y);
> gradL:=grad(L,[x,y,lambda]);
> dem:=solve({gradL[1],gradL[2],gradL[3]},{x,y,lambda});
# The solution with lambda>0 is the two demand functions of this
# consumer and
# the solution for the Lagrange multiplier.
# The other solution can easily be seen not to be a solution of the FOC
# so is extraneous (erroneously produced by Maple's solution procedure).
# Now you can check the SOC for
# maximization. However, this is guaranteed to be true because of the
# properties of U.
> bH:=hessian(L,[x,y,lambda]);
> det(%);
# To illustrate the consumer's optimal choices in a plot, we apparently
# need them as functions so this conversion is made below  The correct
# answer is mq/p/(p+q). If you don't get this try the other component of
# dem.
> xd:=subs(dem[2],x);
> xdem:=unapply(xd,p,q,m);
# In the following, the demand for x is plotted against its own price,
# then the demand curve is shifted with income and with changes in the
# price of y (which way?).  Then the demand surface is plotted for a
# fixed income level. Does the demand
# surface conform with your expectations?
> plot(xdem(p,2,15),p=0..5,x=0..50);
> plot([xdem(p,2,15),xdem(p,2,30),xdem(p,2,45)],p=0..5,x=0..50);
> plot({xdem(p,2,15),xdem(p,8,15),xdem(p,26,15)},p=0..5,x=0..50);
> plot3d(xdem(p,q,15),p=0.1..1,q=0.1..1);
# The domain for the last plot was smaller than for the previous plots
# to magnify the impact of changes of p or q on the demand for x.
# COMPARATIVE STATICS
# You may wish to defer this section until this material is covered in
# class..
# The following formula will be derived in class for the partial
# derivative
# of x with respect to m 
> dxdm:=(q*HU[1,2]-p*HU[2,2])/det(bH);
> dxdm1:=subs(dem[2],dxdm);
# To see that this gives the right answer
> diff(xd,m);
> dxdm1-%;
> simplify(%);
# The formula for the slope of the compensated demand function will be
> dxdpcomp:=-lambda*q^2/det(bH);
> dxdpcomp1:=subs(dem[2],dxdpcomp);
# Finally, Slutsky's equation can be verified
> dxdp:=dxdpcomp1-xd*dxdm1;
> diff(xd,p);
> dxdp-%;
> simplify(%);
# If you simplify this further by hand, you will see that it equals 0.
> 
