Python Part 3 - Calculus II
Instructions
- This part focuses on Calculus 2 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
- Finding area between curves
- Finding volumes of revolution
- Calculating work for springs and pumping liquid from a tank
- Generating and plotting sequences and partial sums of series
- Generating a recursive sequence
- Finding the radius and interval of convergence for a power series
- Plotting a function with partial sums of its power series
Exercises
1.
Python Code:
#Area under y=(4/3)sqrt(9-x^2), above x-axis, and to the left of y=2x
from sympy import *
x=symbols('x') f=4/3*sqrt(9-x**2) g=2*x
matplotlib notebook
plot((f,(x,-3,3)),(g,(x,-3,3)))
c=solve(f-g,x)[0] # this case only has one solution so adding [0] gives that single solution as a number, instead of a list print(c) Area1=integrate(f,(x,-3,0)) Area2=integrate(f-g,(x,0,c)) print(Area1,Area2) print('The area is equal to', (Area1+Area2).evalf())
Open Python Notebook File
2.
Python Code:
#Volume of the ellipsoid formed by rotating y=b/a * sqrt(a^2-x^2) about the x-axis from sympy import *
x=symbols('x') a,b=symbols('a b') f=b/a*sqrt(a**2-x**2)
fplot=f.subs({a:3,b:4}) print(fplot)
matplotlib notebook
plot(fplot,(x,-3,3))
c=solve(f,x) print(c)
Volume=integrate(pi*f**2,(x,c[0],c[1])) print('The volume of the ellipsoid is',Volume.simplify())
Open Python Notebook File
3.
Python Code:
#10 Joules of work to move a spring from a natural length of 10cm to a length of 15cm
#How much work would it take to move the spring from a length of 15cm to a length of 20cm?
from sympy import *
x,k=symbols('x k') F=k*x Work1=integrate(F,(x,0,0.05)) ksol=solve(Work1-10,k)[0] print(ksol) Fnew=F.subs(k,ksol) print(Fnew) Work=integrate(Fnew,(x,.05,.10)) print('The work required is',Work,'J')
Open Python Notebook File
4.
Python Code:
#Work required to pump water from a 0.5m spout at the top of a tank 6m long whose ends are semicircles with radius 2m
from sympy import *
y=symbols('y') dW=9800*6*(2*sqrt(4-y**2))*(y+0.5) W=integrate(dW,(y,0,2)) print('The work required is',W.evalf(),'Joules')
Open Python Notebook File
5.
Python Code: Limit of a_n = n * sin(pi/n) a) List the first 20 terms (numerical estimate) b) Plot the first 50 terms (graphical estimate) c) Find the exact limit
from sympy import *
n=symbols('n',positive=True,integer=True) a=n*sin(pi/n) a1to20=[a.subs(n,i).evalf() for i in range(1,21)] print(a1to20)
matplotlib notebook
import matplotlib.pyplot as plt n1to50=range(1,51) a1to50=[a.subs(n,i) for i in n1to50] plt.plot(n1to50,a1to50,'o')
L=limit(a,n,oo) print('The limit of the sequence is',L)
Open Python Notebook File
6.
Python Code: Given the series sum from 1 to infinity of (-1)^(n+1)/n^2 a) Find and plot the first 20 partial sums b) Find the exact sum of the series
from sympy import * import numpy as np
n=symbols('n',positive=True,integer=True) a=(-1)**(n+1)/n**2 n1to20=range(1,21) a1to20=[a.subs(n,i).evalf() for i in n1to20] print(a1to20) s1to20=np.cumsum(a1to20) print(s1to20)
matplotlib notebook
import matplotlib.pyplot as plt plt.plot(n1to20,s1to20,'o')
from mpmath import nsum,inf S=nsum(lambda n:(-1)**(n+1)/n**2,[1,inf]) print('The sum of the series is',S)
Open Python Notebook File
7.
Python Code:
Generate and print the first 50 Fibonacci numbers
a=[1,1] for n in range(2,50): a.append(a[n-2]+a[n-1]) print('The first 50 Fibonacci numbers are',a)
Open Python Notebook File
8.
Python Code:
Find the radius and interval of convergence of the sum from 0 to infinity of (-1)^n * x^n/( (n+2) * 2^n)
from sympy import *
n=symbols('n',positive=True,integer=True) x=symbols('x') a=(-1)**n*x**n/((n+2)*2**n) RatioTest=abs(a.subs(n,n+1)/a) print(RatioTest.simplify()) L=limit(RatioTest.simplify(),n,oo) print('The limit of the Ratio Test is',L) # likely obvious x is between -2 and 2 reduce_inequalities(L<1)
# Left endpoint aLeft=a.subs(x,-2) print(aLeft.simplify(),'This series diverges by limit comparison with the series 1/n.')
# Right endpoint aRight=a.subs(x,2) print(aRight.simplify(),'This series converges by the Alternating Series Test.')
Open Python Notebook File
9.
Python Code:
Plot f(x)=arctan(x) and the 3rd, 5th, and 10th partial sum of its powers series
sum from 0 to infinity (-1)^n x^(2n+1) / (2n+1)
from sympy import *
n=symbols('n',positive=True,integer=True) x=symbols('x') a=(-1)**n*x**(2*n+1)/(2*n+1) f=atan(x) a1to3=[a.subs(n,i) for i in range(4)] s3=sum(a1to3) print(s3) a1to5=[a.subs(n,i) for i in range(6)] s5=sum(a1to5) a1to10=[a.subs(n,i) for i in range(11)] s10=sum(a1to10)
matplotlib notebook
plotf=plot((f,(x,-1.5,1.5)),(s3,(x,-1.5,1.5)),(s5,(x,-1.5,1.5)),(s10,(x,-1.5,1.5)),ylim=[-1,1]) plotf[1].line_color='r' plotf[2].line_color='y' plotf[3].line_color='k' plotf.show()
Open Python Notebook File
