# MAPLE ASSIGNMENT 10
# GRAPHS OF 2 VARIABLE FUNCTIONS
# The following example of a 3d plot has nothing to do with economics
# but may be attractive. Please manipulate the plot in any way that you
# like.
> plot3d(sin(x)*cos(y),x=-Pi..Pi,y=-Pi..Pi);
# Now try plotting the almost production function I have been using as
# an example in class. A good choice of style here may be "patch and
# contour". Putting in normal axes may also help.
> plot3d(x^.25+x*y+y^.25,x=0..50,y=0..50);
#  The domain over which the surface is plotted can be changed from a
# rectangle by making the limits for y functions
# of x. Here the domain is reduced to{(x,y)/0<=x,0<=y,x+y<=50} 
> plot3d(x^.25+x*y+y^.25,x=0..50,y=0..50-x);
# PARTIAL DERIVATIVES
> f:=sin(x)*cos(y);
> g:=x^.25+x*y+y^.25;
# The partial derivatives of multiple variable functions can be
# obtaiined using the diff command.
> fx:=diff(f,x);
> fy:=diff(f,y);
> gx:=diff(g,x);
> gy:=diff(g,y);
# Second partial derivatives can also be computed.
> gxx:=diff(g,x,x);
> gxy:=diff(g,x,y);
> gyx:=diff(g,y,x);
> gyy:=diff(g,y,y);
# Note that gxx and gyy are always negative. This is the law of
# diminishing marginal products. Notice also that gxy = gyx.
# Now you can plot the graphs of the marginal product functions. Change
# the appearance of the plot in any way that like.  Can you see the law
# of diminishing marginal products in these graphs?
> plot3d(gx,x=0..1,y=0..1);
# Diminishing MP's are indicated if when you point the cursor at the
# y-axes and then move the cursor from there parallel to the x-axis you
# cross ever lower and lower contour lines. Notice that I had to reduce
# the domain for the plot
# substantially in order to look at this property as  gx(x,y) quickly
# becomes a constant = y as a function of x
# alone. The next command plots gy.  Does this plot also show DMP?
> plot3d(gy,x=0..1,y=0..1);
# Partial derivatives of the first and second orders can also be
# calculated all at once.
> with(linalg):
> grad(f,[x,y]);
> grad(g,[x,y]);
> hessian(f,[x,y]);
> hessian(g,[x,y]);
#  The variables in these commands do not have to be named x & y. It is
# also possible to do a subset of the
# variables the function is defined over.
> grad(K^(1/2)*L^(1/3)*R^(1/6),[K,L,R]);
> grad(K^(1/2)*L^(1/3)*R^(1/6),[K,R]);
> hessian(K^(1/2)*L^(1/3)*R^(1/6),[K,L,R]);
> hessian(K^(1/2)*L^(1/3)*R^(1/6),[K,R]);
# SOLVING NONLINEAR EQUATIONS. 
#  If an explicit solution is possible, Maple may find it for you. Maple
# automatically solves expression = 0 if the expression is not equated
# to something. 
> solve(x^2-y^2,y);
> solve(x^3-y^3,y);
> solve(x^4-2*x*y+y^4,y);
# In the first example, the two solutions are found.  In the second two
# of the solutions involve complex numbers so we can ignore them (I is
# the square root of -1). In the third, Maple  is saying that there is
# no explicit solution but because this is a polynomial equation , we
# should try a numerical method to find solutions 
# LEVEL CURVES & IMPLICIT FUNCTIONS
# The next command uses the fsolve command to solve the equation
# x^4-2xy+y^4=0. The solution f(x) can be defined in a manner similar to
# a function.
> f:=x->fsolve(x^4-2*x*y+y^4,y);
> f(.5);
# Note that the full solution of the equation is not a function because
# f(x) has more than one value in some cases.  A
# fourth degree equation has either 4 or 2  or 0 real solutions.  The
# next command produces a list of solutions of the equation for values
# of x from -2 to 2 in steps of 0.5.
> seq([i/2,f(i/2)],i=-4..4);
# Note that there are two values of f(x) for -1 =<x <0 & 0<x=<1, a
# repeated root at x=0 and otherwise no solutions for f(x).
# The next command plots the level curve defined by x^4-2xy+y^4=0.
> with(plots):
> implicitplot(x^4-2*x*y+y^4,x=-2..2,y=-2..2);
# The plot does not look that great because Maple calculates only a few
# solutions to the equation and then joins them with straight lines. 
# The next command improves the plot by insisting that more points be
# plotted.
> implicitplot(x^4-2*x*y+y^4,x=-2..2,y=-2..2,numpoints=1600);
# You can see that the whole curve is not a function but can be broken
# into sections which are functions. This is what Implicit Function
# Theorem says must happen. By carefully restricting the range over
# which x and y are plotted, some of these sections can be plotted.
> implicitplot(x^4-2*x*y+y^4,x=.5..1.1,y=.9..1.1);
# You can edit the ranges of x and y in this command to get other
# sections of the level curve which are implicit functions. Go back to
# the plot of the whole level curve and pick appropriate ranges by
# clicking on points in that plot to get their coordinates.
# Maple also allows for implicit differentiation.
> dydx:=implicitdiff(x^4-2*x*y+y^4,y,x);
> dydx11:=subs({x=1,y=1},dydx);
# Note that (1,1) is a point on this curve as you can see by
# substituting it into the equation defining the curve. Maple does not
# appear to be able to differentiate directly an implicit  function
# defined by the fsolve command so implicitdiff is the way to get the
# slope of the tangent line to this function.
# The level curves or contour lines of a production function are also
# known as isoquants. For the example of the almost production function
# used in class (and in Maple Assignment 2), they are illustrated by the
# following commands. 
> contourplot(x^.25+x*y+y^.25,x=0..5,y=0..5);
> implicitplot({x^.25+x*y+y^.25=2,x^.25+x*y+y^.25=4,x^.25+x*y+y^.25=6},x
> =0..5,y=0..5);
# Using implicitplot gives more control over the levels plotted.  You
# can change the levels to be plotted by  editing and reexecuting the
# above command. You can also add more
# levels if you wish. Next the tangent line to an isoquant is plotted
# along with the isoquant. The equation for the tangent line at
# (xo,yo) is fx(xo,yo)(x-xo)+fy(xo,yo)(y-yo)=0. The slope of this line
# is -fx(xo,yo)/fy(xo,yo) and is the MRTS of the isoquant {(x,y) :f(x,y)
# = f(xo,yo)} at (xo,yo).
> f:=x^.25+x*y+y^.25;
> fx:=diff(f,x);
> fy:=diff(f,y);
> q:=subs({x=2,y=3},f);
> g:=subs({x=2,y=3},fx)*(x-2)+subs({x=2,y=3},fy)*(y-3);
> implicitplot({f=q,g=0},x=0..5,y=0..5);
# You can try some other base points than (2,3) if you wish. To rotate
# the tangent line along the level curve, you can do the following.
> y1:=fsolve(subs(x=1.5,f)=q,y);
> g1:=subs({x=1.5,y=y1},fx)*(x-1.5)+subs({x=1.5,y=y1},fy)*(y-y1);
> implicitplot({f=q,g=0,g1=0},x=0..5,y=0..5 );
> 
> 
