Indian Institute of Technology Gandhinagar: PH 509: Computational Physics
Indian Institute of Technology Gandhinagar: PH 509: Computational Physics
Assignment - 2
Instructions :
Submit Assignment based on this format
Your File Name for submission should be [N ame] [RollN o] Assignment 2.pdf
Please copy the problem, write your approach, your python code , results (Figures, tables) etc.
1 Problem 1
1. You are given a function f (x) = x3 + 2x2 + 3x + 1. Plot this function using matplotlib when
x [5, 5] with 512 points. Get the input from the user to give a number x0 between [-2,2]. At
the point (x0 , f (x0 ), draw a tangent to this curve. Label the axes and curve. Save the output as a
png file.
1.1 Approach
We have given function of curve ,gradient of f(x) at point x0 will give us slope and using that slope
we will get tangent at (x0 , y0 ).
1.2 Program
from pylab import
x=np . l i n s p a c e ( 5 , 5 , 5 1 2 , e n d p o i n t=True )
y=x3+2x2+3x+1 #g i v e n e q u a t i o n f ( x )
p l o t ( x , y , c o l o r= green , l i n e w i d t h =2 , l i n e s t y l e=- , l a b e l= curve of equation )
x l a b e l ( x , c o l o r= black , f o n t s i z e =14)
y l a b e l ( f(x) , c o l o r= black , f o n t s i z e =14)
t i t l e ( plot of f(x )= $x ^3+2 x ^2+3 x +1 $ and tangent at point )
x t i c k s ([ 4 , 2 ,0 ,2 ,4])
# code f o r t a n g e n t
x 0=input ( give value between -2 to 2 )
m=(3 x 0 2+4 x 0 +3) # d i f f e r e n t i a t i o n of y f o r f i n d i n g s l o p e at x
print m
y 0= x 0 3+2 x 0 2+3 x 0+1 # value of f ( x 0 )
X=np . l i n s p a c e ( 2 , 2 , 5 1 2 , e n d p o i n t=True )
Y=m (Xx 0 ) +y 0 # equation of tangent at ( x 0 , f ( x 0 ) )
p l o t (X, Y, c o l o r=red , l i n e w i d t h =2 , l i n e s t y l e= -- , l a b e l= tangent )
l e g e n d ( l o c= upper right )
g r i d ( True )
s a v e f i g ( tangent of curve . png , d p i =300)
show ( )
1.3 Results
output results
1
2 Problem 2
Pn
1. Suppose f (x, n) = k=1 sin(2(2k1)x)
2k1 . The use will give the value of n. Plot f (x, n) for that n.
See the trend as n increases.
2.1 Approach
we have given f(x) as sum of sin series ,In program code for loop is used for calculating summation of
series.we can see by increasing value n we are getting square wave form.
2.2 Program
#Neha P a t e l 16510047
from p y l ab import
n=input ( give value of n )
x=np . l i n s p a c e ( 1 , 1 , 5 0 0 , e n d p o i n t=True )
print x
s=0
f o r k in range ( 1 , n +1):# l o o p f o r f i n d i n g summation upto n+1
s=s +(np . s i n ( 2 np . p i ( 2 k1)x ) ) / ( 2 k1)
print s
p l o t ( x , s , c o l o r= green , l i n e w i d t h =2 , l i n e s t y l e=- )
x l a b e l ( x , c o l o r= black , f o n t s i z e =14)
y l a b e l ( sum of sin series , c o l o r= black , f o n t s i z e =14)
t i t l e ( plot of f(x )= $ {\ sum_ {k =1}^{ n} sin (2\ pi (2k -1) x )}/({2 k -1}) $ when n =500 )
g r i d ( True )
s a v e f i g ( sin_series . png , d p i =300)
show ( )
2.3 Results
output results
2
3
3 Problem 3
3.1 Approach
In this problem, we have given population growth inform of first order differential equation .which can
be solved by two method analytical( in which ODE solved explicitly by integration )and eulers( in which
is taylor expansion of N(t+t) upto first order. heret should be small so we can ignore higher order
terms).
3.2 Program
Inset program
#Neha P a t e l 16510047
import m a t p l o t l i b . p y p l o t a s p l t
from sympy import
import numpy a s np
N, t=symbols ( N ,t )
a , b=symbols ( a b , i n t e g e r=True )
t= i n t e g r a t e ( 1 / ( a NbN 2 ) ,N) # f o r s o l v i n g e q u a t i o n
print t
#when b=!0
#a n a l y t i c method
a=10
b=3
N 0=2 # i n t i a l v a l u e
d e l t a t =0.01
t=np . a r a n g e ( 0 . 1 , 0 . 5 , d e l t a t )
N=(a N 0 np . exp ( a t ) ) / ( b N 0 np . exp ( a t )b N 0+a ) #e x p r e s s i o n o f N i n terms o f
N 0 and t
#numeric method ( u s i n g e u l e r s method)
def Numeric ( Nt , d e l t a t , dNdt , d2NdT2 ) :
return Nt + dNdt d e l t a t + 0 . 5 d2Ndt2 d e l t a t 2 . 0
num= [ ] # empty l i s t
num . append ( N 0 ) #i n t i a l i z a t i o n
f o r i in range ( len ( t ) 1):
N t=num[ 1]
dNdt=(a N t ) (b N t 2 )
d2Ndt2=(a2b N t ) ( a N tb N t 2 )
#d2Ndt2=0
num . append ( Numeric ( N t , d e l t a t , dNdt , d2Ndt2 ) )
p l t . p l o t ( t , N, c o l o r= green , l i n e w i d t h =1 , l i n e s t y l e= -- , l a b e l= analytic )
p l t . p l o t ( t , num , c o l o r=red , l i n e w i d t h =1 , l i n e s t y l e=- , l a b e l= numeric )
p l t . x l a b e l ( time )
p l t . y l a b e l ( Number of population b =!0 )
p l t . l e g e n d ( l o c= upper right )
4
p l t . g r i d ( True )
p l t . t i t l e ( comparison of analytic and numeric method )
p l t . s a v e f i g ( population_growth1 . png , d p i =300)
p l t . show ( )
#
#when b=0
#a n a l y i t c method
a=10
b=0
N 0=2 # i n t i a l v a l u e
d e l t a t =0.01
t=np . a r a n g e ( 0 . 1 , 0 . 5 , d e l t a t )
N=(N 0 np . exp ( a t ) )
#numeric method ( u s i n g e u l e r s method )
def Numeric ( Nt , d e l t a t , dNdt , d2NdT2 ) :
return Nt + dNdt d e l t a t + 0 . 5 d2Ndt2 d e l t a t 2 . 0
num= [ ]
num . append ( N 0 )
f o r i in range ( len ( t ) 1):
N t=num[ 1]
dNdt=(a N t ) (b N t 2 )
d2Ndt2=(a2b N t ) ( a N tb N t 2 )
#d2Ndt2=0
num . append ( Numeric ( N t , d e l t a t , dNdt , d2Ndt2 ) )
p l t . p l o t ( t , N, c o l o r= green , l i n e w i d t h =1 , l i n e s t y l e= -- , l a b e l= analytic )
p l t . p l o t ( t , num , c o l o r=red , l i n e w i d t h =1 , l i n e s t y l e=- , l a b e l= numeric )
p l t . x l a b e l ( time )
p l t . y l a b e l ( Number of population b =0 )
p l t . l e g e n d ( l o c= upper right )
p l t . g r i d ( True )
p l t . t i t l e ( comparison of analytic and numeric method )
p l t . s a v e f i g ( population growth2 . png , d p i =300)
p l t . show ( )
3.3 Results
output results
5
6