Differentiation and its uses.

In the previous chapter, we looked at some tangent line problems which we solved by finding the  limit of a ratio.    For the function   f(x) = x^2+10 , we found that the slope of the tangent line to the graph of f at a point [b,f(b)] is  2b.   The defines a function, called the derivative of f,   diff(f(x),x) = 2*x .  It turns out that the derivative function also measures the rate of change of f.  The notion of rate of change has application to almost any area of knowledge.  In this chapter,  we  begin to make serious use of differentiation in solving  problems which involve rate of change.

 Defining Derivatives

  The Maple word    limit   can be used to calculate derivatives from the definition.  Say    f(x) = x^2+x-3   and you want to compute f ' by the definition.  So enter

>     f := x -> x^2 + x -3;

f := proc (x) options operator, arrow; x^2+x-3 end proc

>    fp := limit((f(x+h)-f(x))/h,h=0);

fp := 2*x+1

You would get the same thing by entering

>    fp := diff(f(x),x);

fp := 2*x+1

>   

The main Maple words here are diff      and  limit but in practice you will use diff  much more.  Derivatives of functions defined by expressions, such as

>     y := x^2 + x;

y := x^2+x

would be computed with, as follows:

>    yp := diff(y,x);

yp := 2*x+1

Now evaluate the derivative of y at    x = 3   using subs ,     

>    subs(x=3,yp);

7

to get the derivative at    x = 3  .  Another way to do this is with    unapply     , a Maple word to change an expression into a function.

>     yp:=unapply(yp,x);

yp := proc (x) options operator, arrow; 2*x+1 end proc

>    yp(3);

7

Yet another way is to use the D operator ,    which takes a function and returns its derivative as a function.

>    f := x -> x^2 + x;

f := proc (x) options operator, arrow; x^2+x end proc

>    df := D(f);

df := proc (x) options operator, arrow; 2*x+1 end proc

>    df(3);

7

The student package

Maple contains a package of words, called the student package ,    that pertain to the study of calculus.  This is a good spot to examine some of them.   In order to load the words for the student package, enter  

>    with(student);

[D, Diff, Doubleint, Int, Limit, Lineint, Product, Sum, Tripleint, changevar, completesquare, distance, equate, integrand, intercept, intparts, leftbox, leftsum, makeproc, middlebox, middlesum, midpoin...
[D, Diff, Doubleint, Int, Limit, Lineint, Product, Sum, Tripleint, changevar, completesquare, distance, equate, integrand, intercept, intparts, leftbox, leftsum, makeproc, middlebox, middlesum, midpoin...
[D, Diff, Doubleint, Int, Limit, Lineint, Product, Sum, Tripleint, changevar, completesquare, distance, equate, integrand, intercept, intparts, leftbox, leftsum, makeproc, middlebox, middlesum, midpoin...

One interesting word is    showtangent     .  Give it a function and a point where the derivative of the function is defined.  The word showtangent will return a picture of the function and the tangent line to the function at the given point.  So to see the graph of    y = x^3+x-1   and its tangent line at   x = 4.5 , enter

>    showtangent(x^3+x-1,x=4.5,color=black);

[Maple Plot]

>   

Problems

Exercise:   Use each word   unapply  and   D  to define the derivative of %?    Plot both f and its derivative over the interval [-1..1]

>    f := x->x^4 - 2*x^3 + x -3 ;

f := proc (x) options operator, arrow; x^4-2*x^3+x-3 end proc

>    df := D(f);

 

df := proc (x) options operator, arrow; 4*x^3-6*x^2+1 end proc

>    plot({f,df},-2..2);

[Maple Plot]

Exercise:  Use  'for x from -1 by .25 to 2 do'  to tabulate the values of  f and its derivative at increment of .25 over the interval from -1 to 2.  

>   

>   

 

3.  Plot f and its derivative over this interval.  Then use   showtangent  to plot f and the tangent line to the graph at    x = -5 .

>   

>   

 

4.  Where does f cross the x-axis?  Find the zeros of f '(x).  That is, find the extreme values of f.


>   

>   

Newton's Method.

 You have used   fsolve  to solve an equation   f(x) = 0   for x.   How does that work?  It possibly might use Newton's method, which is a very fast method for solving such equations when f is differentiable.   The idea is simple.  By graphing or guessing, you come up with a first estimate of the solution, call it x0.  Now the point (x0,f(x0)) is probably not on the x-axis.  Iif it were then f(x0) is 0 and x0 is a solution. So we go along the tangent line to the graph of f until we come to the x-axis, at the point (x1,0) say.    We find that we can express x1 in terms of x0, f(x0), and f'(x0).  This number x1 is the second approximation to the solution.  

>    restart;

>      eq :=  diff(f(x0),x0) = (f(x0)-0)/(x0-x1);

eq := diff(f(x0),x0) = f(x0)/(x0-x1)

>    expand(solve(eq,{x1}));

{x1 = x0-f(x0)/diff(f(x0),x0)}

>   

This equation is called Newton's method .     It has the property that you can iterate it, that is, after computing x1 you rename it x0 and apply the method again.  You can continue this iteration until sequence of numbers converges, or it becomes clear that it won't converge.   Here is a version of Newton's method which draws a so-called stairstep  picture to go with the calculations  The final approximation is the title.

>    vnewt := proc(f,start,a,b,iterations)     
  local  i, x0, fp, p,pl1,pl2;         
  x0 :=  evalf(start);         
  fp := D(f);                  
  p := [x0,0];                   
  for i from 1 to iterations do        
         p := p, [x0, f(x0)];    
         x0 := x0 - f(x0)/fp(x0);   
        p := p,[x0,0];          
  od;                         
pl1 := plot( [p] ,x=a..b, color=black);
pl2 := plot(f,a..b,color=red) ;
### WARNING: semantics of type `string` have changed
plots[display]([pl1,pl2],title=convert(x0,string));
end:

  Now test   vnewt  to solve     x^2 = 2   by starting starting at 3 and using a plot interval of 0 to 4..   

>    vnewt(x -> x^2 - 2, 10 , 0,  10 ,5);  

[Maple Plot]

>   

Exercises.

Exercise:   Use   vnewt  to solve     cos(x)-x = 0     for  x   for various starting points  from 2 to       3.05.   Are there starting points which don't lead to a solution?  Explain your answer.

>   

>   

Exercise:  Use   vnewt  to solve     x^2+.1e-1 = 0  .  Try start at .2, a at -.3, b at .3.  What    happens?    

>   

>   

Exercise:     Define  a new version of  vnewt, tnewt, which has the same inputs, but returns the list of approximations to the root.  Test your word.

>   

>   

>   

Use of the derivatives in plotting

There is a close connection between the first and second derivative of a (differentiable) function f and what its graph looks like.  The local extreme points of f occur amongst the critical points     (the zeros of f ' ), and the inflection points occur amongst the zeros of f ''.  Also f is increasing (decreasing) on intervals where f ' is positive (negative), and f is concave up (down) on intervals where f '' is positive (negative).   

 

For example, let's look at the function

>    y := (x^5-1)/(6*x^2+1);

y := (x^5-1)/(6*x^2+1)

>   

First plot it over an interval, say from -2  to 2.

>    plot(y,x=-2..2);

[Maple Plot]

>   

Look at the graph carefully and estimate the coordinates of local extrema and inflection points .    Now lets get these points exactly using some calculus.  First compute the derivative of y with respect to x.

>    yp := diff(y,x);

yp := 5*x^4/(6*x^2+1)-12*(x^5-1)/(6*x^2+1)^2*x

>    yp := simplify(yp);

yp := x*(18*x^5+5*x^3+12)/(6*x^2+1)^2

>   

Get the critical points by solving   yp = 0  .  Actually, we get better results by finding when the numerator of yp is 0.

>    cp := fsolve(numer(yp),x);

cp := -.8657650194, 0.

>   

cp is an expression sequence of two critical points, cp[1] and cp[2].  By inspecting the graph, we can see that cp[1] is a local maximum and cp[2] is a local min.   Let's evaluate y at   x = cp[1] , and at    x = cp[2] .

>    subs(x = cp[1],y);

-.2703889018

>    subs(x = cp[2],y);

-1.000000000

>   

Now we know the coordinates of the extreme points on the graph.  Next let's get the coordinates of the inflection points.

>    ypp := diff(yp,x);

ypp := (18*x^5+5*x^3+12)/(6*x^2+1)^2+x*(90*x^4+15*x^2)/(6*x^2+1)^2-24*x^2*(18*x^5+5*x^3+12)/(6*x^2+1)^3

>    simplify(ypp);

4*(54*x^7+27*x^5+5*x^3-54*x^2+3)/(6*x^2+1)^3

>    pip := fsolve(numer(ypp),x);

pip := -.2324164053, .2392938071, .8746375912

>    for i from 1 to 3 do print(pip[i], subs(x=pip[i],y)) od;

-.2324164053, -.7557396749

.2392938071, -.7437022342

.8746375912, -.8732685718e-1

>   

So we see there are three inflection points.

Exercise:  there always an inflection point between two extreme points? Explain your answer.

Exercise:  Is there always an extreme point between two inflection points?   Explain your answer.

Implicit Differentiation

Suppose  you have a curve in the plane which is defined by an equation in x and , say   f(x,y) = 0 .  If [x0,y0] is a point on the curve, that is   f(x0,y0) = 0 ,  then we can ask for an equation for the tangent line to the curve at that point.   One way to obtain the slope of the tangent is as follows:  assume that y is a function of x near [x0,y0].  Then near [x0,y0],  f(x,y(x)) is the constant function 0, so when we differentiate f(x,y(x)) with respect to x the result function of x is the constant function 0.  But also, the function we get will be linear in y'(x).  That means we can solve for y'(x) in terms of x and y(x).  Evaluating this at [x0,y0] gives the slope of the tangent.  This algorithm is referred to as implicit differentiation .     

Exercise:  Plot the equation   x^2*sin(x*y)+y^3-5 = 0  in the rectangle  x=0..4, y= 0..2, and draw a tangent to the graph at the point  [3,y0] where  y0 is between 0 and .6.  

Solution:

First we will draw the curve using plots[implicitplot] .        

>    plots[implicitplot](x^2*sin(x*y)+y^3-5 = 0,x=0..4,y=0..2);

Now we need to find the desired y0.  Use fsolve      for that.

Error, (in plot/iplot2d/expression) bad range arguments x = 0 .. 4, (x^5-1)/(6*x^2+1) = 0 .. 2

>    y0 := fsolve(3^2*sin(3*y)+y^3-5=0,y,0..(.6));

Error, (in fsolve) fsolve cannot solve on (x^5-1)/(6*x^2+1)

Now let's get a formula for y'(x).  First substitute y(x) in for y.

>    g := subs(y=y(x),x^2*sin(x*y)+y^3-5=0) ;

g := x^2*sin(x*(x^5-1)/(6*x^2+1))+(x^5-1)^3/(6*x^2+1)^3-5 = 0

Then differentiate the resulting function of x with respect to x.

>    dg := diff(g,x);

dg := 2*x*sin(x*(x^5-1)/(6*x^2+1))+x^2*cos(x*(x^5-1)/(6*x^2+1))*((x^5-1)/(6*x^2+1)+5*x^5/(6*x^2+1)-12*x^2*(x^5-1)/(6*x^2+1)^2)+15*(x^5-1)^2/(6*x^2+1)^3*x^4-36*(x^5-1)^3/(6*x^2+1)^4*x = 0
dg := 2*x*sin(x*(x^5-1)/(6*x^2+1))+x^2*cos(x*(x^5-1)/(6*x^2+1))*((x^5-1)/(6*x^2+1)+5*x^5/(6*x^2+1)-12*x^2*(x^5-1)/(6*x^2+1)^2)+15*(x^5-1)^2/(6*x^2+1)^3*x^4-36*(x^5-1)^3/(6*x^2+1)^4*x = 0

Notice the left hand side is linear in y'(x), as we asserted it would be.  So solve the equation for y'(x).

>    yp := solve(dg,diff(y(x),x));

yp := NULL

Now turn this back into an expression in x and y.

>    yp := subs(y(x)=y,yp);

yp := (x(x)^5-1)/(6*x(x)^2+1) = (x^5-1)/(6*x^2+1)

Calculate the slope of the tangent at [3,y0].

>    slope :=evalf(subs({x=3,y=y0},yp));

slope := (3(3)^5-1.)/(6.*3(3)^2+1.) = y0

Write the equation for the tangent line in point slope form.

>    tangent := y - y0 = slope*(x-3);

tangent := (x^5-1)/(6*x^2+1)-y0 = (4.400000000*x-13.20000000 = (x-3)*y0)

Plot the curve and tangent separately so that we can color them differently. Then display them.

>    curve :=plots[implicitplot]( x^2*sin(x*y)+y^3-5 = 0 ,x=0..4,y=0..2,thickness=2):

Error, (in plot/iplot2d/expression) bad range arguments x = 0 .. 4, (x^5-1)/(6*x^2+1) = 0 .. 2

>    tanline := plots[implicitplot]( tangent   ,x=0..4,y=0..2,thickness = 2, color=blue):

Error, (in simpl/relopsum) invalid terms in sum

>    plots[display]([curve,tanline]);

Exercise:  Find a point on the curve in the rectangle where the tangent line is vertical.  (Hint: you will need to assume x is a function of y and differentiate with respect to y here.)

Error, (in plots/display) first argument must be a plot structure or list, set or array of plot structures

 Max-min Problems

  A vast number of problems fall into the category of the so-called max-min problems.  These are problems in which some quantity, Q, is to be maximized or minimized as needed.  The quantity Q is a function of one or more other variables which are subject to some constraints.  We attempt to use these constraints to eliminate all but one of the variables in the computation of Q.  If we are successful in doing this, we can then use calculus to locate the maximum or minimum if it exists.

Let's take an example.

A Paper folding problem:

Problem:  A sheet of paper 4 inches wide by 8 inches high is folded so that the bottom right corner of the sheet touches the left hand edge of the sheet.  The tip of the corner is no more than 4 inches above the bottom edge of the paper.  Then the paper is creased (see figure).  Find the length L of the crease, and find how to fold the paper so that L is minimum.  

Solution:

Let h, x, and y be as shown in the diagram below.

>    restart;

>    A1:=plots[textplot]({[2.6,1.9,"L"],[3.7,2,"h"],[.1,1.1,"y"],[.5,.3,"x"]},align=RIGHT):

>    A3:=plot({[[0,0],[0,8],[4,8],[4,4.45],[1.219,0],[0,0]],[[4,4.45],[0, 2.5],[1.219,0]]},color=blue):

>    A4:=plot([[4,4.45],[4,0],[1.219,0]],color=red):

>    A5:=plots[polygonplot]([[4,4.45],[0,4.45],[0,2.5]],style=patch,color=tan):

>    plots[display]([A1,A3,A4,A5],axes=boxed,scaling=constrained);

[Maple Plot]

>   

 We can see several equations relating x, y, h, and L in the diagram.  For example, the small right triangle with legs x and y has a hypotenuse which is  4-x units long.  This gives eq1.

>    eq1 := y^2+x^2=(4-x)^2;

eq1 := y^2+x^2 = (4-x)^2

eq2 comes from the right triangle with hypotenuse L and legs h and 4-x.

>    eq2 := L^2 = (4-x)^2 + h^2;

eq2 := L^2 = (4-x)^2+h^2

Now it is easy to work out that the tan right triangle with hypotenuse h and legs 4 and  h-y is similar to the right triangle with hypotenuse 4-x and corresponding legs y and x.  So we get eq3.

>    eq3 := 4/(h-y)=y/x;

eq3 := 4/(h-y) = y/x

>   

>    h := solve(eq3,h);

h := (4*x+y^2)/y

>    x := solve(eq1,x);

x := -1/8*y^2+2

>    L := unapply(sqrt(op(2,simplify(eq2))),y);

L := proc (y) options operator, arrow; 1/8*((16+y^2)^3/y^2)^(1/2) end proc

>   

So we have L expressed as a function of  one variable y. Examining the behavior of L as y  varies,  

>    plot(L,2..4);

[Maple Plot]

we see that there is a minimum length crease of about    L = 5.2   inches at about   y = 2.8   inches.  We can get a more precise value with fsolve, by locating the x between 2.6 and 3 where the derivative of the crease function is 0.

>    y1 := fsolve(diff(L(y),y),y,2.6..3);

y1 := 2.828427125

>   

Now check the value of x and L for this value of y.

>    minL := L(y1);

minL := 5.196152422

>    minx := subs(y=y1,x);

minx := 1.000000000

>   

Notice the nice integer value of   minx .  This gives rise to a simple construction of the crease of minimum length:  by folding twice along the bottom edge, we can mark the point  1 inch from the left edge of the paper.  Then bring the corner point up to the left edge and crease.

Exercise:   Use Maple to draw the diagram showing the minimum length crease.

>   

Exercise:   Suppose we wanted to minimize  L + y, rather than just L.  Would the  minimum occur at the same place?  Work it out.

>   

>