Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168714
Image: ubuntu2004

Using the Chain Rule

This section introduces you to using Sage to do implicit differentation, plotting curves implicitly, using the solver to solve for symbolic functions, take derivatives of log, exp, and trig functions, and find differentials. 

Implicit Differentiation

If you do not tell Sage explicitly that y is a function of x, then it will assume y is a constant when you take derivatives.  The following code shows you how to tell the computer that y is a function of x. You can then type in any equation involving x and y and then the computer will take the derivative of both sides, and then solve for dy/dx, writing it as D[0](y)(x).

var('x,y') y(x)=function(y,x) eqn= x*y^2+3*sin(y)==0 eqn
x |--> x*y(x)^2 + 3*sin(y(x)) == 0

Now take the derivative of both sides.

diff(eqn)(x)
2*x*D[0](y)(x)*y(x) + y(x)^2 + 3*D[0](y)(x)*cos(y(x)) == 0

Solve for the derivative

solve(diff(eqn), diff(y))
[D[0](y)(x) == -y(x)^2/(2*x*y(x) + 3*cos(y(x)))]

Finally, print the solution in a nice readable way. The [0] at then end of dydx[0] tells the computer to grab the first object in the list returned by the solve function. Notice that the answer is included inside brackets [ ] which is why I have to use [0] to grab the first item in the list.

dydx=solve(diff(eqn), diff(y)) dydx[0].show()
D[0]\left(y\right)\left(x\right) = -\frac{y\left(x\right)^{2}}{{(2 \, x y\left(x\right) + 3 \, \cos\left(y\left(x\right)\right))}}

Plotting Implicit Curves and Tangent Lines

This section shows you the code needed plot curves given implicitly, as well as how to find tangent lines at any point on the curve.

This first example shows you how to use implicit plot to plot any curve given implicitly. Use the option, aspect_ratio=1 if you want the x and y scale to be the same.

var('x,y') eqn=y^3==x^2-3*x*y implicit_plot(eqn,(x,-10,10),(y,-10,10),aspect_ratio=1)

In the code below,  you can type in any curve and a point on the curve.  This code will draw the curve, then use implicit differentation to find the derivative, find the slope at the point, create the tangent line, and then plot both the curve and tangent line on the same axes.

var('x,y') eqn = x^2+y^2==25 #don't forget the double equal == pt=(3,-4) xrange=(x,-10,10) yrange=(y,-10,10) x0=pt[0] #get the first number from pt and call it x0 y0=pt[1] #get the second number from pt and call it y0 p=implicit_plot(eqn,xrange,yrange) p+=point(pt,pointsize=30) p.show(aspect_ratio=1)
var('Y') Y(x)=function(Y,x) print diff(eqn(y=Y(x))) sol=solve(diff(eqn(y=Y(x))), diff(Y));sol
2*Y(x)*D[0](Y)(x) + 2*x == 0 [D[0](Y)(x) == -x/Y(x)]
dydx=(sol[0].rhs()).subs_expr(Y(x)==y) dydx
-x/y
m=dydx(x=x0,y=y0) print m
3/4
eqn_tan_line=y-y0==m*(x-x0);eqn_tan_line.show()
y + 4 = \frac{3}{4} \, x - \frac{9}{4}
p+=implicit_plot(eqn_tan_line,xrange,yrange) p.show(aspect_ratio=1)

Derivatives of Log, Exp, and Trig Functions

Below you will find examples of the syntax needed to enter logarithmic, exponential, and trig functions. 

  • Remember that log(x) in Sage means log base e.
  • To get a logarithm of any other base, use log(x,a).
  • To type e^x, use exp(x). 
  • For arcsin, arccos, and arctan, Sage simplifies the derivative to something you recognize.  For the other three, it leaves the derivative in symbolic form.
diff(log(x))
1/x
diff(log(x,3))
1/(x*log(3))
diff(exp(x))
e^x
diff(2^x)
2^x*log(2)
diff(arcsin(x))
1/sqrt(-x^2 + 1)
diff(arctan(x))
1/(x^2 + 1)
diff(arccos(x))
-1/sqrt(-x^2 + 1)
print diff(arcsec(x)) print diff(arccsc(x)) print diff(arccot(x))
D[0](arcsec)(x) D[0](arccsc)(x) D[0](arccot)(x)

Sage will perform logarithmic differentiation automatically when needed.

diff((sin(x))^(cos(x)))
-(log(sin(x))*sin(x) - cos(x)^2/sin(x))*sin(x)^cos(x)
diff(x^x)
(log(x) + 1)*x^x

Related Rates

It is not possible to create a computer program to solve every related rates problem.  Instead, you have to type in the equations you know and then ask Sage to take derivatives and solve for the variables you need in each problem.  Let's use differentials to solve a problem involving a sliding ladder.  Suppose a 10 m ladder is slipping away from a building.  The base is moving at a rate of 2 m per second.  When the base of the ladder is 6 meters from the base of the building, how fast is the top of the ladder sliding down the edge of the building?

If we let our variables be bb for the distance from the base of the building to the base of the ladder, ll be the length of the ladder, and hh be the height from the base of the building to the tip of the ladder, then we know b2+h2=l2b^2+h^2=l^2 and we also know dbdt=2\dfrac{db}{dt}=2 m/s.  We need to find dhdt\dfrac{dh}{dt}.  Notice that ll is always 10 meters long.  Let's now code this into Sage.

var('b,h,l') b(t)=function(b,t) h(t)=function(h,t) l=10 eqn=b^2+h^2==l^2 eqn
t |--> b(t)^2 + h(t)^2 == 100

Let's take the derivative of both sides with respect to tt.

diff(eqn,t)
t |--> 2*b(t)*D[0](b)(t) + 2*h(t)*D[0](h)(t) == 0

Now I need to solve for dhdt\dfrac{dh}{dt}.

sol=solve(diff(eqn,t),diff(h,t)) sol
[D[0](h)(t) == -D[0](b)(t)*b(t)/h(t)]

We have the solution dh/dt=db/dtb/hdh/dt=-db/dt \cdot b/h. We still need to solve for hh, and then plug in these values into the formula we obtained for db/dhdb/dh.

solve(eqn.subs_expr(b(t)==6),h)
[h(t) == -8, h(t) == 8]
(sol[0].rhs()).subs_expr(diff(b,t)==10).subs_expr(b(t)==6,h(t)==8)
-15/2

Differentials

The derivative gives us the slope of a tangent line.  If you increase the xx value of a function from xx to x+dxx+dx, then how much does the yy value increase?  The notation dydx=f(x)\dfrac{dy}{dx}=f'(x) suggests that we can find a small change in yy by multiplying both sides by dxdx to obtain the differential dy=f(x)dx.dy = f'(x)dx. Think of dydy as a small change in yy.  Think of dxdx as a small change in dxdx.  The quantities dydy and dxdx are called differentials. You can use differentials to estimate how much yy will change if xx changes by dxdx. The example below illustates how to do this.  In high dimensional calculus, the differentials become vectors and the derivative becomes a matrix. This differential equation is the key equation needed to extend calculus to higher dimensions.

var('x,dx') f=x^2 dy=diff(f)*dx dy
2*dx*x
xvalue=2 dx=1/2 dy(x=xvalue,dx=dx)
2

The actual vertical change in yy from xx to x+dxx+dx is the height of the green triangle.  The approximate change in yy from xx to x+dxx+dx is the height of the purple triangle. They are not the same, but are very close when dxdx is small.

xrange=(x,xvalue-2*dx,xvalue+2*dx) p=plot(f,xrange) p+=point((xvalue,f(x=xvalue)),pointsize=30) p+=plot(f(x=xvalue)+diff(f)(x=xvalue)*(x-xvalue),xrange,color='red') p+=line([(xvalue,f(x=xvalue)),(xvalue+dx,f(x=xvalue))],rgbcolor='purple') p+=line([(xvalue+dx,f(x=xvalue)),(xvalue+dx,f(x=xvalue)+dy(x=xvalue,dx=dx))],rgbcolor='purple') p+=line([(xvalue,f(x=xvalue)),(xvalue,f(x=xvalue+dx))],rgbcolor='green') p+=line([(xvalue,f(x=xvalue+dx)),(xvalue+dx,f(x=xvalue+dx))],rgbcolor='green') p+=point((xvalue+dx,f(x=xvalue)+dy(x=xvalue,dx=dx)),rgbcolor='purple',pointsize=20) p+=point((xvalue+dx,f(x=xvalue+dx)),rgbcolor='green',pointsize=20) p.show()