Python Part 2 - Calculus I

Instructions

  • This page focuses on Calculus I examples, but it is useful for learning Python even if you are not taking Calculus.
  • For each video, you can see the problem being solved, as well as the Python code used.
  • You can also open the link to the Python Notebook file. You can click File>Download to download the notebook file to run on your own computer.
  • Each video has a link to the full video page. These pages include links to related videos covering the same topics.

Learning Objectives

  • Using list comprehension to estimate limits
  • Finding and evaluating derivatives
  • Calculating definite and indefinite integrals
  • Word problems
  • Simplifying difference quotients
  • Plotting functions, derivatives, and tangent lines
  • Patterns in higher derivatives
  • Implicit and Parametric plots and tangent lines
  • Finding absolute maxima and minima
  • Finding intervals of increase and decrease, along with concavity
  • Approximating definite integrals with Riemann Sums

Exercises

1.

Python Code:
from sympy import *
x=symbols('x') f=(sqrt(x)-1)/(x**3-1) xval=[0.9,0.99,0.999,1.1,1.01,1.001,1.0001] yval=[f.subs(x,i) for i in xval] print(yval) print('The limit appears to be about 1.6666')
matplotlib notebook
plot(f,(x,0,2))L=limit(f,x,1)print('The limit is',L)
L=limit(f,x,1,'-') print('The left hand limit is',L)
L=limit(f,x,1,'+') print('The right hand limit is',L)

Open Python Notebook File

2.

Python Code:
from sympy import *
mu,m,g,x,h=symbols('mu m g x h') F=mu*m*g*sqrt(x**2+h**2)/(x+mu*h) dF=F.diff(x)print('The general derivative is',dF) dFat10=F.subs({m:18,h:10,mu:0.55,g:9.8,x:10}) print('When x=10, the rate of change in t is',dFat10.evalf() ,'Newtons/meter')
dFat40=F.subs({m:18,h:10,mu:0.55,g:9.8,x:40}) print('When x=40, the rate of change in t is', dFat40.evalf(), 'Newtons/meter')

Open Python Notebook File

3.

Python Code:
from sympy import *
V,omega,t=symbols('V omega t', positive=True) v=V*cos(omega*t) v_int=(v**2).integrate(t) #You can also use: v_int=integrate(v**2,t) print(v_int)
vdef=integrate(v**2,(t,0,2*pi/omega)) print('The integral from 0 to 2pi/omega is', vdef)
rms=sqrt(omega/(2*pi)*vdef) print('The root mean square value of the voltage is', rms)

Open Python Notebook File

4.

from sympy import *

# Step 1 r,h=symbols('r h') V=pi*r**2*h+2/3*pi*r**3 S=2*pi*r*h+2*pi*r**2
# Step 2 command: solve V-4=0 hval=solve(V-4,h) print('the height solution is',hval)
# Step 3 command: subs Sofr=S.subs(h,hval[0]) print(Sofr)
matplotlib notebook
# Step 4 plot(Sofr,(r,0,10),ylim=[0,100])

Open Python Notebook File

5.

from sympy import *

# Use limit definition to find derivative of f(x)=1/x^3 h,x=symbols('h x') f=1/x**3
#Step 1 command: subs diffqt=(f.subs(x,x+h)-f)/h print('The difference quotient is',diffqt)
#Step 2 command: factor, expand, or simplify print(diffqt.expand().simplify())
#Step 3 command: subs print(diffqt.expand().simplify().subs(h,0))

Open Python Notebook File

6.

Python Code: 
from sympy import *

x=symbols('x')
f=exp((x-75)**2/200)/(10*sqrt(2*pi))
y0=f.subs(x,80)
df=diff(f,x)
m=df.subs(x,80)
tanline=y0+m*(x-80)
print('The equation of the tangent line is y=',tanline.evalf())

matplotlib notebook

plot((f,(x,60,100)),(tanline,(x,60,100)))

Open Python Notebook File

7.

Python Code: 
from sympy import *

x=symbols('x')
y=(6*x**2-4*x**3)/(2*sqrt(x**3*(2-x)))

Python Code: 
matplotlib notebook

plot(y,(x,0,2),ylim=[-10,10])

dy=diff(y,x).simplify()
print(dy)

horiz=nsolve(dy,x,0.75)
print('The graph has a horizontal tangent line at x=',horiz)

Open Python Notebook File

8.

Python Code: 
from sympy import *
# List Comprehension x=symbols('x') f=1/x # 1st 6 derivatives IN ONE LINE OF CODE! fderivs=[diff(f,x,i) for i in range(1,7)] #range(a,b)-> list of integers from a INCLUSIVE to b EXCLUSIVE print(fderivs)
# exponent is 1 bigger than the derivative we took: x^(n+1) # signs alternate (-1)^n or (-1)^(n+1). n=1 is negative, so (-1)^n #factorial numbers in the numerator: n!  print('The formula for the nth derivative of f is (-1)^n*n!/x^(n+1)')

Open Python Notebook File

9.

Python Code: 
from sympy import *

x,y=symbols('x y')
eqn=x**3+y**3-3*x*y

matplotlib notebook

peqn=plot_implicit(eqn,(x,-3,3),(y,-3,3))

dydx=idiff(eqn,y,x)
print('dy/dx=',dydx)
m=dydx.subs({x:2/3,y:4/3})
print('The slope of the tangent line is',m)
tanline=4/3+m*(x-2/3)
print('The equation of the tangent line is y=',tanline)

peqn.extend(plot(tanline,(x,-3,3)))
peqn.show()

Open Python Notebook File

10.

Python Code:
from sympy import *
x,t=symbols('x t') xt=t**3-3*t**2 yt=t**3-3*t
matplotlib notebook
pcurve=plot_parametric(xt,yt,(t,-2,3))
dydx=diff(yt,t)/diff(xt,t) m=dydx.subs(t,-2) x0=xt.subs(t,-2) y0=yt.subs(t,-2) tanline=y0+m*(x-x0) print('The equation of the tangent line is y=',tanline)
pcurve.extend(plot(tanline,(x,-20,0))) pcurve.show()

Open Python Notebook File

11.

Python Code:
from sympy import *
x,t=symbols('x t') xt=cos(t)**3 yt=sin(t)**3 eq1=xt-1/27 # To get exact answers, use the Rational command: eq1=xt-Rational(1,27)  eq2=yt-16*sqrt(2)/27 # To get exact answers, use the Rational command: eq2=yt-Rational(16,27)*sqrt(2) tval=solve([eq1,eq2],t) print(tval)
dydx=diff(yt,t)/diff(xt,t) print('dy/dx=',dydx) m=dydx.subs(t,tval[0][0]) print(m) tanline=16*sqrt(2)/27+m*(x-1/27) print('The equation of the tangent line is y=',tanline.evalf())
matplotlib notebook
pcurve=plot_parametric(xt,yt,(t,0,pi/2)) pcurve.extend(plot(tanline,(x,0,1))) pcurve.show()

Open Python Notebook File

12.

Python Code:
from sympy import *
T=symbols('T') D=.0694*T**2-10.505*T+399.05 dD=diff(D,T) critvals=solve(dD,T) print(critvals)
candidates=[53,critvals[0],107] yvals=[D.subs(T,i) for i in candidates] print(yvals)
print('The minimum distance is', yvals[1],'degrees when T is about 75.68 degrees.')

Open Python Notebook File

13.

Python Code: 
#Analyze the graph of y=2x^7-21x^6-36x^5+3x^4-38x^3+24x^2
from sympy import *
x=symbols('x') y=2*x**7-21*x**6-36*x**5+3*x**4-38*x**3+24*x**2
matplotlib notebook
plot(y,(x,-10,10),ylim=[-10,10])
dy=diff(y,x) # To find concavity, repeat this process but use dy=diff(y,x,2) to get the second derivative cvals=solve(dy,x) cvalsfloat=[i.evalf() for i in cvals] print(cvalsfloat)
matplotlib notebook
plot(dy,(x,-2,11),ylim=[-10,10])
print('The function is increasing from(-∞,-1.625),(0,0.35),(10,253,∞)') print('and decreasing from (-1.625,0) and (0.35,10.253)')

Open Python Notebook File

14.

Python Code: 
from sympy import * import numpy as np
x=symbols('x') f=exp(-x**2) a=0 b=2 n=50 dx=(b-a)/n xi=np.arange(a,b,dx) yi=[f.subs(x,i) for i in xi] Approx=np.sum(yi)*dx print('The area is approximately', Approx)

Open Python Notebook File