# MAPLE ASSIGNMENT 3
# DIFFERENCE QUOTIENT
# The concept of difference quotient will be illustrated with the sine
# curve.
> plot(sin(x),x=0..Pi);
# The first aim is to plot a secant line joining two points on this
# curve.  You may if you wish substitute your own choices for x1 =.5 and
# x2 =2 in the next formula.which is the formula for a straight line
# passing through (x1,sin(x1)) and having slope
# (sin(x2)-sin(x1))/(x2-x1)
> f:=x->sin(.5)+(sin(2)-sin(.5))*(x-.5)/(2-.5);
# To check that this is going to work
> evalf(sin(.5)-f(.5));
> evalf(sin(2)-f(2));
> plot(f(x),x=0..Pi);
# The next command plots both the sine curve and the secant line
# together.
> plot([sin(x),f(x)],x=0..Pi);
# Next plot two more secant lines reducing the distance between the two
# points on the sine curve.  
> f1:=x->sin(.5)+(sin(1.25)-sin(.5))*(x-.5)/(1.25-.5);
> f2:=x->sin(.5)+(sin(.8)-sin(.5))*(x-.5)/(.8-.5);
> plot([sin(x),f(x),f1(x),f2(x)],x=0..Pi,y=0..1.5);
# Maple will also do animations as an alternative to the traditional
# kinds of figures seen in books.
# After executing the animate command, click on th plot to get the
# animate menu, select play in the animate menu to start the animation
# and try out other options on the animate menu or try clicking buttons
# on the toolbar soch as the bottom last button which causes the
# animation to repeat continuously.
> with(plots):
> animate(sin(.5)+(sin(.5+1.5/t)-sin(.5))*(x-.5)*t/1.5,x=0..Pi,t=1..8);

# It is possible to get the sine curve back into the picture by doing a
# composite plot. Notice the colons at the end of the next two commands
# so that no output is displayed.
> a:=animate(sin(.5)+(sin(.5+1.5/t)-sin(.5))*(x-.5)*t/1.5,x=0..Pi,t=1..8
> ):
> b:=plot(sin(x),x=0..Pi):
> display([a,b]);

# Derivatives can be done in Maple either symbolically or with the
# answer given as a function.
> diff(x^n,x);
# If you do not like the way the answer looks you can try
> simplify(");
> diff(sin(x),x);
# Try differentiating something of your own choice.
# To turn the answer into a function which you can use in further
# calculations you can use the unapply command and assign the answer a
# name.
> dsin:=unapply(",x);
# To check that a function has been created
> dsin(Pi/2);
# 
> plot(dsin(x),x=0..Pi);
# The next function will be the tangent line to the sine curve at x1
# =.5.
> tl:=x->sin(.5)+dsin(.5)*(x-.5);
# Finally you can add the tangent line to the plot with the secant
# lines.
> plot([sin(x),f(x),f1(x),f2(x),tl(x)],x=0..Pi,y=0..1.5);
# Algebraically, you should be able to get the derivative of the sine
# curve at x1 as a limit of the 
# difference quotient but this does not seem to work unless x1 is an
# integer.
> limit((sin(.5+h)-sin(.5))/h,h=0);
# Alternatively, you can calculate a number of values of the difference
# quotient as h gets smaller and by inspection verify that the
# difference quotient is approaching a limit. Here h = 1/(10n), n an
# integer.
> seq((sin(.5+(1/(10*n)))-sin(.5))*(10*n),n=1..50);
# The limit is
> cos(.5);
# RULES OF DIFFERENTIATION
# The next commands illustrate the product rule,  the quotient rule and
# give you a couple of opportunities to check that you can use the chain
# rule correctly.
> diff(x^3*ln(x+1),x);
> diff(x^3,x)*ln(x+1)+x^3*diff(ln(x+1),x);
> diff(x^3/ln(x+1),x);
> (diff(x^3,x)*ln(x+1)-x^3*diff(ln(x+1),x))/ln(x+1)^2;
> simplify("-"");
> diff(ln(sin(x)),x);
> diff(cos(ln(x)),x);
# Now using the cost function from the previous assignment, the relation
# between average and marginal 
# cost will be illustrated.
> C:=x->x^3-4*x^2+6*x;
> diff(C(x),x);
> MC:=unapply(",x);
> C(x)/x;
> simplify(");
> AC:=unapply(",x);
> plot(MC(x),x=0..3,y=0..6);
> plot(AC(x),x=0..3,y=0..6);
> plot([MC(x),AC(x)],x=0..3,y=0..6);
# Did you click on the plot to get the coordinates of the minimum AC
# point?  The range for y is 
# specified above as otherwise Maple would put the bottom of the y-range
# very close to the minimum 
# MC value. 
# The next commands show you two ways to get MAC the derivative of AC.
> diff(AC(x),x);
> MAC:=unapply(",x);
> (MC(x)-AC(x))/x;
> simplify(");
# You can see that you get the same answer in both cases. 
# You can also see at what value of x AC is at its minimum.  You can
# also find this point
# numerically, The command fsolve 
# gives only the real solutions of the equation.
> fsolve(MAC(x)=0,x);
> [AC("),MC(")];
> solve(AC(x)=MC(x),x);
# 
#  Go back and add a fixed cost term to C and AC (not too large) and
# reexecute the commands above.   You may want to change the upper
# bounds for x and y to get a
# better looking plot. It may be necessary to change the last command to
# fsolve to eliminate complex
# roots.
> 
