All published worksheets from http://sagenb.org
Image: ubuntu2004
Derivatives
This worksheet will introduce you to using Sage to compute derivatives. It will also show you how to use the definition of the derviative to compute derivatives, plot functions, their derivatives, and tangent lines, as well as connect position, velocity, acceration and jerk.
Basic Derivative Commands
The simplest way to take derivatives is to use either the diff() or derivative command. Don't forget to always include a * when you want to times things.
Second derivatives are taken by simply typing diff(f,2).
If you are going to be using the same function in multiple places, then you may want to save it as an expression first. Then you can type f.diff() to differentiate it.
Using this notation, you have to use .subs() to evaluate the derivative at points.
You can use function notation instead. Then f.diff() gives a function, so you can type f.diff()(3) to evaluate the derivative at 3.
My preferred method is to name the derivative of f (something like Df). Then I can type Df(3) to evaluate the derivative at 3.
With this notation, it is simple to plot the function and it's derivative on the same axes. Just define p to be a plot, and then use p+= to add more plots to the graph. (Those of you familiar with computer programming should recognize that p+= means let p be itself plus what follows.) Because I use the same xrange in all the graphs below, I start by defining the xrange and then use it in each plot command.
If you want higher order derivatives, you can take the derivative of the derivative, or you can just simply take 2 derivatives. Both Df.diff() and f.diff(2) will give the second derivative. I want to name it something simple like D2f so I can use it again later.
I can now add the second derivative to the graph as well. Notice that the derivative is zero precisely when the function's horizontal tangent line has zero slope. Similarly, the 2nd derivative is zero precisely when the first derivative has horizontal tangent lines.
Using the definition of the derivative
Taking derivatives is built into Sage. Part of you homework will ask you to manually compute derivatives from the definition. The code below computes the difference quotient, simlifies it, and then takes a limit. You can use this to check your work.
Tangent Lines
The derivative gives the slope of a function at every point. To find an equation of the tangent line at , we compute the derivative and evaluate it at to obtain the slope. The line has the right slope, so we move it over units to the right and up units to obtain a line through . The resulting equation is The following code does all of this for you, first computing the derivative, then finding and equation of the line, and then plotting both the function and it's tangent line at .
Position, Velocity, Acceleration, and Jerk
The velocity of an object is the rate at which its position changes, or . Accerlation is the rate at which velocity changes, or . The jerk is the rate of change of acceleration, . These quantities are all related to each other by derivatives. The code below requires you to enter the position of an object, a time at which to compute these values, and then it computes all the quantities and plots them all on the same set of axes.
Symbolic Differentiation
You can use sage to work with unknown functions. The notation means to take the derivative of with respect to the 0th variable (which is ).
Exponential Functions
The code below draws graphs of , , and for values near . Notice that the graph for passes through the point (0,1), so we should have . This allows us to show that the derivative of is itself, .
To take derivatives of exponential functions other than , you have to multiply by the natural logarithm of the base. So
In Sage (and mathematica), the natural logarithm is the default logarithm, so typing log(2) means .
Trigonometric Derivatives
The example below shows how to expand the difference quotient for . Then it graphs the functions and near 0 to show that the limits are 0 and 1 respectively. This allows us to conclude that the derivative of is .
Because there are lots of different ways to represent trigonometric functions (using the identities from trigonometry), Sage may not give you the derivative in the exact same form as you obtain by hand when you use derivatives which involve , , , or . If you instead rewrite each of these in terms of sines and cosines, you may get closer to what you wanted.
For , remember that .