How to make Pendulum in Sagemath

This pendulum will teach you:

1. How to solve second degree differential equation?

2. How to apply initial values  to the solved equation?

3. How to animate the things?

4. How to save the produced .gif file with ffmpeg in sage?

5. How to take many points on equation?

Here is the code :

T,t,b,a,g,L,c=var('T,t,b,a,g,L,c')
theta = function('theta', t)
b=g/L
assume(g>0)
assume(L>0)
L=4    ## Length of string
g=9.8 ## accelaration due to gravity
##### Pendulum's Time Period: ############
T=2*pi*(L/g)^(1/2)
print "t= ",T
#### Pendulum's Equation ########
de=diff(theta,t,t)+b*theta==0
### Solving Differential equation with initial conditions(i.e at t=0) theta(0)=2*pi/3, theta'(0)=1 (i.e angular velocity=1)#########
des=desolve_laplace(de,dvar=theta,ivar=t,ics=[0,pi/3,1])
theta(t)=des
print "Solution of pendulum equation:",des
## Fixed point of pendulum i.e. origin ##
c=point((0,0),color='blue', pointsize = 20)
z=animate([c+circle(((2*(1/3)*(pi*L*cos(sqrt(g)*t/sqrt(L)) + 3*L^(3/2)*sin(sqrt(g)*t/sqrt(L))/sqrt(g))/L),-T),0.25,color='red',fill=True)+line([(0,0),((2*(1/3)*(pi*L*cos(sqrt(g)*t/sqrt(L)) + 3*L^(3/2)*sin(sqrt(g)*t/sqrt(L))/sqrt(g))/L),-T)],color='black',thickness=3.0) for t in srange(0,T,T/10)],xmin=-3, xmax=3, ymin=-7,ymax=1,figsize=[10,10])
z.ffmpeg(savefile='pendulum.gif',delay=20) ### saving pendulum.gif file in parent folder of sage folder##
 ### You can see path where file is saved by parameter: show_path='True' in ffmpeg() function ########

Output:

t=  1.27775312999988*pi
Solution of pendulum equation: 1/3*(pi*L*cos(sqrt(g)*t/sqrt(L)) + 3*L^(3/2)*sin(sqrt(g)*t/sqrt(L))/sqrt(g))/L

Advertisements

About Priyanka Kapoor

Simple, Hardworking & friendly.....
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s