You are on page 1of 6

Indian Institute of Technology Gandhinagar

PH 509: Computational Physics

Assignment - 3

Due date: August 31

Group 9
Name: [Prateek Chauhan, Sachin Kumar, Gourav Kumar]
Roll number: [16510054,16510067,16510028]

1 Problem 1

1.1 Approach
We convert the given second order ODE in two first order equations and then solve it using odeint and
RK4 methods.

1.2 Program
import numpy a s np
import m a t p l o t l i b . p y p l o t a s p l t
from math import s i n , cos , p i
from s c i p y . i n t e g r a t e import o d e i n t

#F unc tion t o compute F(X, t )


def F f u n c ( I n i t , time , b , g , omega ) :
G=np . z e r o s ( 2 )
t h e t a=I n i t [ 0 ]
d t h e t a d t=I n i t [ 1 ]
G[ 0 ] = d t h e t a d t

1
G[1]= bG[0] g s i n ( t h e t a )+omega 2 c o s ( t h e t a ) s i n ( t h e t a )
return G

#I n i t i a l i z a t i o n

Init1 =[1.0 ,0.0]


b=1.0
omega =10.0
g =9.81
parameter =(b , g , omega )
T=2 p i
dt =(0.01/T)
tmin=0
tmax=5T
time=np . a r a n g e ( tmin , tmax , dt )

#S o l v i n g t h e ode u s i n g o d e i n t

S o l=o d e i n t ( F func , I n i t 1 , time , a r g s=parameter )

#P l o t t i n g t h e r e s u l t s
f i g=p l t . f i g u r e ( 1 )

fig . s u p t i t l e ( Phase and Amplitude diagram for $\ omega =10.0 $ and $b =1.0 $ )
plt . subplot (2 ,2 ,1)
plt . p l o t ( time , S o l [ : , 0 ] )
plt . legend ()
plt . x l a b e l ( Time (t) )
plt . y l a b e l ( $ \\ theta$ )
plt . subplot (2 ,2 ,2)
plt . p l o t ( time , S o l [ : , 1 ] )
plt . legend ()
plt . x l a b e l ( Time (t) )
plt . y l a b e l ( $d \\ theta / dt$ )
plt . subplot (2 ,2 ,3)
plt . plot ( Sol [ : , 0 ] , Sol [ : , 1 ] )
plt . legend ()
plt . x l a b e l ( $ \\ theta$ )
plt . y l a b e l ( $d \\ theta / dt$ )
plt . show ( )

#S o l v i n g u s i n g RK4 method

#I n i t i a l c o n d i t i o n s

t h e t a=I n i t 1 [ 0 ]
d t h e t a d t=I n i t 1 [ 1 ]

P= [ ]
Q= [ ]
f o r t 0 in time :
m1 = d t h e t a d t
k1 = b d t h e t a d t g s i n ( t h e t a ) + ( omega 2 ) s i n ( t h e t a ) c o s ( t h e t a )
m2 = d t h e t a d t + ( dt / 2 . 0 ) k1
t 2 = t 0 + ( dt / 2 . 0 )
t h e t a 2 = t h e t a + ( dt / 2 . 0 ) m1
d t h e t a d t 2 = m2
k2 = b d t h e t a d t 2 g s i n ( t h e t a 2 ) + ( omega 2 ) s i n ( t h e t a 2 ) c o s ( t h e t a 2 )
m3 = d t h e t a d t + ( dt / 2 . 0 ) k2
t 3 = t 0 + ( dt / 2 . 0 )

2
t h e t a 3 = t h e t a + ( dt / 2 . 0 ) m2
d t h e t a d t 3 = m3
k3 = b d t h e t a d t 3 g s i n ( t h e t a 3 ) + ( omega 2 ) s i n ( t h e t a 3 ) c o s ( t h e t a 3 )
m4 = d t h e t a d t + dt k3
t 4 = t 0 + dt
t h e t a 4 = t h e t a + dt m3
d t h e t a d t 4 = m4
k4 = b d t h e t a d t 4 g s i n ( t h e t a 4 ) + ( omega 2 ) s i n ( t h e t a 4 ) c o s ( t h e t a 4 )
t 0 = t 0 + dt
t h e t a = t h e t a + ( dt / 6 . 0 ) (m1 + ( 2 . 0 m2) + ( 2 . 0 m3) + m4)
d t h e t a d t = d t h e t a d t + ( dt / 6 . 0 ) ( k1 + ( 2 . 0 k2 ) + ( 2 . 0 k3 ) + k4 )

P . append ( t h e t a )
Q. append ( d t h e t a d t )

#P l o t t i n g r e s u l t s
f i g=p l t . f i g u r e ( 2 )

fig . s u p t i t l e ( Phase and Amplitude diagram for $\ omega =1.0 $ and $b =1.0 $ )
plt . subplot (2 ,2 ,1)
plt . p l o t ( time , P)
plt . legend ()
plt . x l a b e l ( Time (t) )
plt . y l a b e l ( $ \\ theta$ )
plt . subplot (2 ,2 ,2)
plt . p l o t ( time ,Q)
plt . legend ()
plt . x l a b e l ( Time (t) )
plt . y l a b e l ( $d \\ theta / dt$ )
plt . subplot (2 ,2 ,3)
plt . p l o t (P ,Q)
plt . legend ()
plt . x l a b e l ( $ \\ theta$ )
plt . y l a b e l ( $d \\ theta / dt$ )
plt . show ( )

1.3 Results
We can easily see that plots are showing damped oscillations. As b, the damping coefficient increases
the amplitude of , and . /dt vanishes more rapidly.,
Also increasing omega does not affect the time in which the amplitude is vanishing but no. of
oscillations in that time period increases.
For different set of values of and b different plots are shown below.

3
4
5
6

You might also like