Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168717
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.

%auto var('x,y') #This line just makes sure that x and y are variables
(x, y)

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.

diff(x^2+3*x+4)
2*x + 3
derivative(x^2+3*x+4)
2*x + 3

Second derivatives are taken by simply typing diff(f,2).

diff(x^2+sin(x)+7*sqrt(x),2)
-7/4/x^(3/2) - sin(x) + 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.

f=sin(x)*x f.diff()
x*cos(x) + sin(x)

Using this notation, you have to use .subs() to evaluate the derivative at points. 

f.diff().subs(x=3)
sin(3) + 3*cos(3)

You can use function notation f(x)f(x) instead.  Then f.diff() gives a function, so you can type f.diff()(3) to evaluate the derivative at 3.

f(x)=sin(x)*x f.diff()
x |--> x*cos(x) + sin(x)
f.diff()(3)
sin(3) + 3*cos(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.

f(x)=x^3-3*x Df=f.diff() Df(x)
3*x^2 - 3
Df(3)
24

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.

xrange=(x,-2,2) p=plot(f(x),xrange) p+=plot(Df(x),xrange,color='red') p.show()

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.

D2f=Df.diff() D2f(x)
6*x
D2f=f.diff(2) D2f(x)
6*x

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.

p+=plot(D2f(x),xrange,color='green') p.show()

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.

var('h') #without this line, you can't use h as a variable f(x)=x^3 f(x).show()
x^{3}
diffquot=(f(x+h)-f(x))/h diffquot.show()
\frac{{({(h + x)}^{3} - x^{3})}}{h}
diffquot.full_simplify().show()
h^{2} + 3 \, h x + 3 \, x^{2}
limit(diffquot,h=0).show()
3 \, x^{2}

Tangent Lines

The derivative gives the slope of a function at every point.  To find an equation of the tangent line at x=cx=c, we compute the derivative and evaluate it at cc to obtain the slope.  The line y=f(c)y=f'(c) has the right slope, so we move it over cc units to the right and up f(c)f(c) units to obtain a line through (c,f(c))(c,f(c)).  The resulting equation is yf(c)=f(c)(xc).y-f(c) = f'(c)(x-c). 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 x=cx=c.

f(x)=sqrt(x) c=4 Df=f.diff() Df(x).show()
\frac{1}{2} \, \frac{1}{\sqrt{x}}
(y-f(c)==Df(c)*(x-c)).show()
y - 2 = \frac{1}{4} \, x - 1
xrange=(x,0,9) p=plot(f(x),xrange) p+=plot(f(c)+Df(c)*(x-c),xrange,color='red') p.show()

Position, Velocity, Acceleration, and Jerk

The velocity of an object is the rate at which its position changes, or v=dsdtv=\dfrac{ds}{dt}. Accerlation is the rate at which velocity changes, or a=dvdta=\dfrac{dv}{dt}. The jerk is the rate of change of acceleration, j=dadtj=\dfrac{da}{dt}.  These quantities are all related to each other by derivatives. The code below requires you to enter the position of an object, a time t=ct=c at which to compute these values, and then it computes all the quantities and plots them all on the same set of axes.

position(t)=-16*t^2+32*t+100 velocity=derivative(position) acceleration=derivative(velocity) jerk=derivative(acceleration) pretty_print([position(t),velocity(t),acceleration(t),jerk(t)]) pretty_print([position(c),velocity(c),acceleration(c),jerk(c)])
\newcommand{\Bold}[1]{\mathbf{#1}}\left[-16 \, t^{2} + 32 \, t + 100, -32 \, t + 32, -32, 0\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[-28, -96, -32, 0\right]
trange=(t,0,5) p=plot(position(t),trange) p+=plot(velocity(t),trange,color='red') p+=plot(acceleration(t),trange,color='green') p+=plot(jerk(t),trange,color='purple') p.show()

Symbolic Differentiation

You can use sage to work with unknown functions. The notation D[0](f)(x)D[0](f)(x) means to take the derivative of f(x)f(x) with respect to the 0th variable (which is xx).

var('x,n') diff(x^n,x).show() #the power rule
n x^{{(n - 1)}}
f(x)=function('f',x) #this tells sage that f and g are functions of x g(x)=function('g',x) diff(f(x)*g(x),x).show() #the product rule
D[0]\left(f\right)\left(x\right) g\left(x\right) + f\left(x\right) D[0]\left(g\right)\left(x\right)
diff(f(x)/g(x),x).show() #the quotient rule
\frac{D[0]\left(f\right)\left(x\right)}{g\left(x\right)} - \frac{f\left(x\right) D[0]\left(g\right)\left(x\right)}{g\left(x\right)^{2}}
diff(f(g(x)),x).show() #the chain rule
D[0]\left(f\right)\left(g\left(x\right)\right) D[0]\left(g\right)\left(x\right)
diff(f(x)^n,x).show() #the general power rule
n f\left(x\right)^{{(n - 1)}} D[0]\left(f\right)\left(x\right)
diff(sin(f(x))).show() #a trig derivative, don't forget the derivative of the inside
D[0]\left(f\right)\left(x\right) \cos\left(f\left(x\right)\right)

Exponential Functions

The code below draws graphs of 2h1h\dfrac{2^h-1}{h}, 3h1h\dfrac{3^h-1}{h}, and eh1h\dfrac{e^h-1}{h} for values near h=0h=0. Notice that the graph for ee passes through the point (0,1), so we should have limh0eh1h=1\displaystyle \lim_{h\to 0} \frac{e^h-1}{h}=1.  This allows us to show that the derivative of exe^x is itself, exe^x.

var('h') diff_quot=(2^h-1)/h p=diff_quot.plot(-1,1) p.show()
diff_quot=(3^h-1)/h p+=diff_quot.plot(-1,1,color='red') p.show()
diff_quot=(exp(h)-1)/h p+=diff_quot.plot(-1,1,color='purple') p.show()
exp(1).n()
2.71828182845905
diff(exp(x))
e^x

To take derivatives of exponential functions other than exe^x, you have to multiply by the natural logarithm of the base.  So

  • (2x)=2xln2(2^x)'=2^x\ln 2
  • (3x)=3xln3(3^x)'=3^x\ln 3
  • (7x)=7xln7(7^x)'=7^x\ln 7

In Sage (and mathematica), the natural logarithm is the default logarithm, so typing log(2) means ln2=loge2\ln 2= \log_e 2.

diff(2^x)
2^x*log(2)

Trigonometric Derivatives

The example below shows how to expand the difference quotient for sinx\sin x.  Then it graphs the functions cos(h)1h\dfrac{cos(h)-1}{h} and sin(h)h\dfrac{\sin(h)}{h} near 0 to show that the limits are 0 and 1 respectively.  This allows us to conclude that the derivative of sinx\sin x is cosx\cos x

var('x,h') diff_quot=(sin(x+h)-sin(x))/h diff_quot.trig_expand()
(sin(h)*cos(x) + sin(x)*cos(h) - sin(x))/h
diff_quot.trig_expand().full_simplify()
((cos(h) - 1)*sin(x) + sin(h)*cos(x))/h
plot((cos(h)-1)/h,-2,2)
plot((sin(h))/h,-2,2)
diff(sin(x))
cos(x)
diff(cos(x))
-sin(x)

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 tanx\tan x, secx\sec x, cscx\csc x, or cotx\cot x. If you instead rewrite each of these in terms of sines and cosines, you may get closer to what you wanted.

For (tanx)(\tan x)', remember that tan2x+1=sec2x\tan^2 x+1 = \sec^2x.

print diff(tan(x)) print diff(tan(x)).full_simplify() print diff(sin(x)/cos(x))
tan(x)^2 + 1 cos(x)^(-2) sin(x)^2/cos(x)^2 + 1
print diff(sec(x)) print diff(1/cos(x))
D[0](sec)(x) sin(x)/cos(x)^2
print diff(csc(x)) print diff(1/sin(x))
D[0](csc)(x) -cos(x)/sin(x)^2
print diff(cot(x)) print diff(1/tan(x)) print diff(1/tan(x)).full_simplify()
D[0](cot)(x) -(tan(x)^2 + 1)/tan(x)^2 -1/sin(x)^2