Derivatives

In this notebook we introduce the Mathematica commands f '[ ], D[ ], Table[ ], Simplify[ ], Manipulate[ ], Solve[ ], NSolve[ ], and ContourPlot[ ].

Some examples are done below.  Read the examples carefully to understand the commands.  Position your cursor anywhere on the first input line (the indented lines are input lines), click the mouse, and press the Enter key on the far right of the computer by the numeric keyboard or Shift+Enter (Shift+Return on a Macintosh) on the alphabetical keyboard to execute the commands.  Study the result and, after you understand what happened, move your cursor to the next input line and enter it. Continue until you reach the end of the notebook.

Examples

Derivatives

If we want to find the derivative of a function  f(x) we can use  f '(x)  = (lim_(h -> 0)) ((f(x + h) - f(x))/h)  but it is much simpler to define the function and then use either  f '[ ] or D[ ].  If we want to find the derivative of  f(x) = 4 (x^5)-3(x^4)-7(x^2)+6 we can start by defining the function  f(x)  so that we can refer to it again without having to retype the entire formula.  We will use formal Mathematica to define the function, we need square brackets [ ]  and the underscore symbol after the  x  when defining the function.  Every time you ask Mathematica to carry out an operation, the Wolfram Predictive Interface gives you a Suggestions Bar that is relevant to the current output.  If the Suggestions Bar seems to have disappeared completely, click on the output cell and it will reappear.  Notice that if you are using formal Mathematica, once you start typing a command it gives you a list of commands from which you can select one.

f[x_]=4(x^5) - 3(x^4)-7(x^2) + 6

Once we enter the function, we can select "x derivative" from the Suggestions Bar to get the first derivative or we can  ask Mathematica to find the derivative with the following line.  If you select "x derivative" from the Suggestions Bar, Mathematica designates the derivative of 4 (x^5)-3(x^4)-7(x^2)+6 as (∂_x)(4 (x^5)-3(x^4)-7(x^2)+6); this notation is one that you will use more in Calculus III when you are looking at functions of two or more variables.

f'[x]

We can also use the D[ ] notation asking Mathematica to differentiate the function with respect to  x.  Again, this notation will be used more when we get to Calc III and we are looking at functions of two or more variables.

D[f[x],x]

Mathematica also now allows us to use "Free-form input".

If you click on the "+" in the upper right hand corner of the box around the "Free-form input" command, you can then click on "Step-by-step solution" and Mathematica will explain how it gets the answer.  It will also show you the graph of f ' (x) and a lot of other things related to f ' (x).  Mathematica is not restricted to differentiating polynomials as you will see in the next examples.  If you use  f(x)  to define a new function, you must first clear  f  of any previous definition.  This is accomplished by entering the following where Clear starts with a capital letter and we use square brackets around  f.

Clear[f]

Tangent is a function, so it must start with a capital letter in formal Mathematica and whatever we are taking the tangent of is enclosed in square brackets.

f[x_]=Tan[(x^2)+1]

f'[x]

Notice that if you click on the "+" in the upper right hand corner of the box around the "Free-form input" command, you can then click on "Step-by-step solution" and Mathematica will explain how it used the Chain Rule to get the derivative.

Clear[f]
f[x_]=(Sqrt[4(x^3) - 3x])

D[f[x],x]

Clear[f]
f[x_]=((x^(2 ))/((x^3) -1))

f'[x]

Sometimes the answer that we get is rather "messy" and we might like to have Mathematica simplify it.  We can use the "simplify" on the Suggestions Bar  or we can use the command  Simplify[ %] where "%"  stands for the last output.  The command is in formal Mathematica but can be used after a "Free-form input" as well.

Simplify[%]

Simplify[%]

Higher Order Derivatives

The Mathematica operator D[f[x], {x, n}] can be used to find the (n^th) derivative of  f(x) with respect to x.  For example, if we wish to find the third derivative of  f(x) =  (x^6) /( 4(x^3)+ (x^2) +5x - 1) we start by first clearing f(x) and then defining the function, we can use three "primes", or  D[f[x], {x, 3}] or "Free-form input" or once we enter the function, we can select "x derivative" from the Suggestions Bar to get the first derivative, then select "x derivative" again to get the second derivative and repeat to get the third derivative.  In this example, you have to pull down "more" from the Suggestions Bar to find "x derivative".  You may want to simplify your final answer in this example.

Simplify[%]

Simplify[Table[D[f[x]], {x, k}, {k, 0, n}]//TableForm] gives us a table listing the function followed by its first  "n"  derivatives.  The following table shows the function  f(x) =  (x^6) /( 4(x^3)+ (x^2) +5x - 1) along with its first 5 derivatives, each on a separate line, and each simplified.

Simplify[Table[D[f[x],{x,k}],{k,0,5}]//TableForm]

String Art

Some interesting pictures can be drawn by plotting a large number of tangent lines to a given curve.  If we want to find the tangent line to a curve at the point (a, f(a)) we use the formula for the equation of a line:
 		y - f(a) = f '(a) (x - a)  or  y = f(a) + f '(a)(x - a).
The following example looks at the function  f(x) = (x^2) over the interval -3 ≤ x ≤ 3, draws the tangent lines to this curve at the points  (a, f(a))  where a = - 3,  a = - 2.9,  a = - 2.8,  a = - 2.7, ... ,  a = 2.7,  a = 2.9,  a = 2.9,  a = 3  and shows these tangent lines with a range of  - 9 ≤ y ≤ 9.  Try entering the following lines.

Clear[f]
f[x_]=(x^2)

Plot[Evaluate[Table[f[a]+f'[a](x-a),{a,-3,3,.1}]],{x,-3,3},PlotRange->{-9,9}]

The next graph gives the same picture but the  x  and  y  axes are adjusted so the units on the two axes are the same.

Plot[Evaluate[Table[f[a]+f'[a](x-a),{a,-3,3,.1}]],{x,-3,3},PlotRange->{-9,9},AspectRatio->Automatic]

Try changing the points at which the tangent lines are drawn.  The statement {a, -3, 3, .1} tells Mathematica to draw the tangent lines starting at a = -3 and to increase the value of  "a"  by increments of 0.1 until finally reaching a = 3.  See what happens if you try {a, -2, 2, .1} or {a, -3, 3, .05}.

In the next example, we look at the circle  (x^2)+ (y^2) = 4  along with the tangent lines to the circle.  The circle is not a function but if we solve for  y  we have  y = +  (Sqrt[4 - (x^2)]).   Since  y  is only defined if  -2 ≤ x ≤ 2 and  dy/dx  is undefined if x = + 2 we can expect to get some error messages when we enter the following lines.

f1[x_]=(Sqrt[4-(x^2)])
f2[x_]=-(Sqrt[4-(x^2)])
Plot[Evaluate[Table[{f1[a]+f1'[a](x-a),f2[a] + f2'[a] (x - a)},{a,-2,2,.1}]],{x,-3,3},
		PlotRange->{-4,4},AspectRatio->Automatic]

Graphing f '(x) from the graph of f(x)

The following example uses Manipulate[ ] and Plot[ ].  It will graph the function in blue, the tangent line in purple, and the derivative in gray. After you enter the command, you will see a bar that is labeled "k" on the left, You can grab the "slider" and as you move it, the tangent line to f(x) at x = a will be drawn and the  slope of the tangent line will then be used to graph the derivative.  Or, at the right end of the bar there is a "+" sign.  Click on that "+" sign (not the "+" sign in the upper right hand corner); by clicking on the arrow head, you will see the same thing happening.  You can pause, slow down, speed up, or reverse the animation by clicking on the various "buttons".

Clear[f]
f[x_]=(x^3) - 2x;
	Manipulate[Plot[{f[x],f[k]+f'[k] (x-k),If[x<k,f'[x],None]},{x,-3,3},
		PlotRange->{-25,25}],{k,-3,3}]

Notice that whenever the original function is increasing the graph of  f '(x)  is above the x-axis and whenever the original function is decreasing the graph of  f '(x)  is below the x-axis.  The Plot[{f1, f2, f3}, {x, a, b}, PlotRange->{c, d}] command is formal Mathematica for graphing three functions.  In this example, f1 = f(x), f2 = the tangent line to f(x) at the point where x = k, and  f3 = f '(x), with domain -3 < x < 3 and range -25 < y < 25.  Notice both words Plot and Range start with capital letters even though PlotRange is one word.  Try changing the function and the intervals for  x  and  y; the values for k should be the same as the values for x.

Graphing f ''(x) from the graph of f(x)

The next example graphs  f '' (x)  from the graph of f(x).   You can animate the graphs just as you did in the previous example.  You should notice that whenever the original function is above the tangent line, its concavity is positive and hence the graph of  f ''(x) is above the x-axis.  Similarly, when the original function is below the tangent line, the concavity is negative and the graph of  f ''(x) is below the x-axis.

Clear[f]
f[x_]=(x^4)+(x^3)-4 (x^2)-4 x;
	Manipulate[Plot[{f[x],f[k]+f'[k] (x-k),If[x<k,f''[x],None]},{x,-3,3},
		PlotRange->{-25,25}],{k,-3,3}]

Try changing the function and the intervals for x and y; the values for k should be the same as the values for x.  This time,  the Plot[{f1, f2, f3}, {x, a, b}, PlotRange->{c, d}] command again uses  f1 = f(x) and f2 = the tangent line to f(x) at x = k, but  f3 = f "(x).

Implicit Differentiation

Mathematica can graph equations that are not functions.  The equation ((x^2)+ (((y^2)))^2)  =  x + 4xy - y  is not written in the form y equals a function of  x.  We can, however, plot this equation and try to find where the tangent line is either horizontal or vertical.  In the following line, you MUST leave a space between x and y in the term  "4xy"; otherwise, Mathematica thinks "xy" is one variable

The formal Mathematica command uses ContourPlot[ ] where both Contour and Plot begin with capital letters although ContourPlot is written as one word.  If we start by using the  ContourPlot[ ]  command, we can specify the values that we want to use for both the domain and range as in the following line.  Notice that we used a double equal sign and there MUST be a space between the x and the y in term "4xy".

ContourPlot[((((x^2)+(y^2)))^2)==x+4x y - y, {x, -2,2},{y,-2,2}]

You can use the  Graphics menu to help estimate the points where the tangent line is horizontal or vertical.  Pull down the Graphics menu to Drawing Tools, you see what appears to be a dotted cross.   First click on the graph, then click on the dotted cross and as you move the cursor over the graph, you will see a read-out of the approximate coordinates of the cursor. From the graph it appears that this equation has horizontal tangent lines  near the points  ( 0.7, 1) and (-0.5,-1.3) and vertical tangent lines near the points (1.2,  0.5) and (-1.1,  -0.7).   You have to be careful if you use the "Free-form input" line below.

The output you see, 4x((x^2) + (y^2)) = 1 + 4y, is NOT the derivative.  If you click on the "+" in the upper right hand corner of the box around the command, under "Result" you see that 
 ((∂y(x))/(∂x))  = (( - 4(x^3) - 4x (y^2) + 4y + 1)/(4(x^2)y - 4x + 4(y^3)+ 1)).

If we specify that y is to be considered a function of  x  by replacing every  y  with  y[x]  we can use formal Mathematica to find the derivative  by using the following commands.

D[((((x^2)+((y[x])^2)))^2)==x +4x y[x] - y[x],x]

The next command solves for y '(x) and replaces all the y[x] terms with y.  Remember "%" always stands for the previous output.  Selecting "solve ode" from the Suggestions Bar does not help us solve for y'(x),

Solve[%,y'[x]]/.y[x]->y

The tangent line to the equation is horizontal when the numerator of y '(x)=0; that is, when 1-4 (x^3)+4 y-4x (y^2) = 0.  Those points on the graph that have a horizontal tangent line must simultaneously satisfy the equation of the curve and  the equation y '(x) = 0.  We can use the following command.  Note the double equal sign that is used in NSolve[ ] and we enclose the original equation and the equation where the numerator of y '(x) equals zero in braces.

NSolve[{((((x^2)+(y^2)))^2)==x +4x y - y, 1-4 (x^3)+4 y-4x  (y^2)==0},{x,y}]

Most of these points involve complex numbers but  (-0.540154, -1.24544)  and  (0.76477, 1.06545) are real points.

Now, the tangent line to the equation is vertical when  y '(x) is undefined, that is the denominator of y '(x) = 0  or when  1-4 x+4(x^2) y +4 (y^3)=0 .  Those points on the graph that have a vertical tangent line must simultaneously satisfy the equation of the curve and make the denominator of y '(x) equal zero.  We can use the following command where we enclose the original equation and the equation where the denominator of y '(x) equals zero in braces.

NSolve[{((((x^2)+(y^2)))^2)==x +4x y - y,1-4 x+4  (x^2) y+4 (y^3)==0},{x,y}]

Once again, most of these points involve complex numbers but  (-1.06545, -0.76477)  and  (1.24544, 0.540154) are real points.   The next line graphs the original function again and uses Prolog  to show the four points we found and to control the size of the points.

ContourPlot[((((x^2)+(y^2)))^2)==x+4x y - y, {x, -2,2},{y,-2,2},
Prolog->{PointSize[.02],Point[{-1.06545,-0.76477}],Point[{1.24544,0.540154}],
	Point[{-0.540154,-1.24544}],Point[{0.76477,1.06545}]}]

Related Rates

Related rates problems are really just an application of implicit differentiation.  We have one or more quantities that are a function of time and we wish to find the rate of change with respect to time.  For example, suppose we are told a particle moves along the curve  (y^2) = 1 + (x^3) and when it reaches the point (2, 3) the x-coordinate is increasing at the rate of  4cm/sec.  We can determine the rate of change in the y-coordinate by using implicit differentiation and we can determine in which direction the particle is moving.

Let's start by looking at the graph of this curve along with the point (2, 3).

ContourPlot[(y^2)==1+(x^3), {x,-2,3},{y,-4,8},Prolog->{PointSize[.02],Point[{2,3}]}]

Since  x  and  y  are both changing with respect to time we will replace  x  with  x[t]  and  y  with  y[t] and then differentiate with respect to time by entering the next line in formal Mathematica.  "Free-form" input" isn’t helpful with this problem.

D[((y[t])^2)==1+((x[t])^3),t]

To solve for dy/dt we can use the Solve[ ] command.

Solve[%,y'[t]]

Since we were told that dx/dt is 4 cm/sec at the point (2, 3) we can use the following line to find dy/dt at the point (2,3).

%/.{x'[t]->4,x[t]->2,y[t]->3}

This tells us that dy/dt = 8 cm/sec.  Since we have dx/dt > 0 and dy/dt > 0 we see that the particle is moving to the right and upward.

If you want to type new command lines, move your cursor on the page until it becomes horizontal and click the mouse.  If you want to use formal Mathematica, simply begin typing.  Notice that if you are using formal Mathematica, once you start typing a command it gives you a list of commands from which you can select one.  If you want to use  "Free-form input",  press the equal sign and then begin typing.

Exercises - Your report should contain a cover page with the name of your instructor, your name and your partner's name, along with the title of the lab.  Your printouts should include the Mathematica commands, not just the outputs, for your graphs and any work that you used to arrive at the solutions to the problems.  You can write explanations by hand on the printed pages.  Include the numbers of the exercises!  No credit will be given without a write-up which shows all your work and gives clear explanations.

1.  Let  f(x) = ((((x  + 1))^2)/((((x^2) - 4))^3)).   Use Mathematica to find:
     a)  f '(x) 
     b)  the fifth derivative in a simplified format.

2.  Try your hand at String Art for each of the following and obtain a printout using:
     a)  f(x) = x +  sin (x) where - 6 ≤ x ≤ 6,  - 6 ≤ a ≤ 6, “a” changes by increments of 0.1 and  -9 ≤ y ≤ 9.  
     b)  f(x) = (Sqrt[4 - (x^2) -2]) and  g(x) = -  (Sqrt[4 - (x^2) +2])where  - 4≤ x ≤ 4,  - 4 ≤ a ≤ 4,   “a”   changes by increments of 0.1, and   - 4 ≤ y ≤ 4 (use these two functions simultaneously, obtaining one printout).

3.  Let f(x) = ((x^2)-4)/((x^2)+1).  
     a)  Use the appropriate Manipulate[ ]  command from the example in this lab to graph f '(x)  from the graph of  f(x) using  - 3 < x < 3 and a range of -5 < y < 5.  Obtain a printout that shows f(x) and f '(x) along with the Mathematica commands, you can use the Play/Pause function to stop the graphing.  Clearly label which graph is f(x) and which is f '(x).
     b)  Use the appropriate Manipulate[ ]  command from the example in this lab to graph f ''(x)  from the graph of  f(x) using - 3 < x < 3  and a range of -10 < y < 10.  Obtain a printout that shows f(x) and f ''(x) along with the Mathematica commands, you can use the Play/Pause function to stop the graphing.  Clearly label which graph is f(x) and which is f ''(x).   If you save your work before printing out this problem, make sure you click Enable Dynamics when you reopen your notebook, or renter the commands to see the graphs.

4. Consider the curve with equation  ((((x^2) + 2(y^2)))^2) ==x - 3xy + y.  You might want to use ContourPlot[ ] to see what the graph looks like and don't forget to leave a space between  x  and  y  in  "xy".
     a)  Find the x and y coordinates, accurate to 6 significant figures,  of all points where the tangent line is vertical and clearly write these on your printout.  
     b)  Obtain a printout of this curve for  - 1 ≤ x ≤ 1 and  - 1 ≤ y ≤ 1  and highlight the points where the tangent line is vertical using Prolog as shown in this lab.

5.  Consider the curve  (x^4) - 4(x^3) + (y^4) - 8(y^2)= 16.  You might want to use ContourPlot[ ] to see what the graph looks like. 
     a)   Find the x and y coordinates, accurate to 6 significant figures,  of all points where the tangent line is horizontal and clearly write these on your printout.
      b)  Obtain a printout of this curve for  - 3 ≤ x ≤ 5 and  - 5≤ y ≤ 5  and highlight the points where the tangent line is horizontal using Prolog as shown in this lab.

6.  A particle is moving along the curve  (x^3) + x - (y^3) - y = 8 in such a way that the x-coordinate of the particle's position is changing at a constant rate x '(t) = 1.  
     a)  How fast is the y-coordinate of the particle's position changing when the particle is at (2, 1)?
     b)  Obtain a printout of this curve, use Prolog to indicate the point (2, 1) and draw on the printout the direction in which the particle is moving at that point.

7.   A container in the shape of an inverted cone has height  16 cm and radius  5 cm at the top.  It is partially filled with a liquid that oozes through the sides at a rate proportional to the area of the container that is in contact with the liquid.  (The surface area of a cone is πRL, where R is the radius and L is the slant height.)  If we pour the liquid into the container at a rate of  2 (cm^3)/min, then the height of the liquid decreases at a rate of  0.3 cm/min when the height is  10 cm.  If our goal is to keep the liquid at a constant height of  10 cm, at what rate should we pour the liquid into the container?