Uploaded by Nandini Sharma

PYTHON PROJECT MAC 5

advertisement
MAC – 5 (LNB)
( Numerical Analysis using PYTHON )
Department – PHYSICS
Sem. – III
Reg. No. – A01-1152 -111- 002 - 2023
Admit Roll. – ***
August 16, 2024
1
Contents
1 Approximation of Simpson 1/3rd Method . . . . . . . . . . . . . . . . . . . . . . . .
3
2 Comparison Between Euler and Modified Euler Method . . . . . . . . . . . . . .
6
3 Forced Oscillator 2nd Order Differential . . . . . . . . . . . . . . . . . . . . . . . . .
8
4 Fourier Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5 Fourier - Gibbs Phenomena . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2
1
Approximation of Simpson 1/3rd Method
#Approximating Simpson Method
import m a t p l o t l i b . p y p l o t a s p l t
import numpy a s np
p l t . s t y l e . use (” c l a s s i c ”)
def f (x ) :
r e t u r n np . exp(−x ∗ ∗ 2 )
#Simpson f o r n no o f terms
def intg (n ) :
x=−6
x f=6
g ,m=0 ,0
h=( xf−x ) / ( n−1)
yy = [ ]
f o r k in range (0 , n ) :
y=f ( x )
yy . append ( y )
x=x+h
f o r j in range (0 , n / / 2 ) :
g=g+4∗yy [ 2 ∗ j +1]
i f 2∗ j +2 < ( n −1):
m=m+2∗yy [ 2 ∗ j +2]
s=h / 3 ∗ ( yy [ 0 ] + g+m+yy [ n −1])
return s
n=3
i n t e g r a l 1 , nn = [ ] , [ ]
r=1e−3
w h i l e abs ( i n t g ( n+2)− i n t g ( n))>= r :
i n t e g r a l 1 . append ( i n t g ( n ) )
nn . append ( n )
n=n+2
#P l o t o f I n t e g r a l vs No o f P o i n t s
f i g , ax=p l t . s u b p l o t s ( 2 , 1 , f i g s i z e = ( 8 , 8 ) , c o n s t r a i n e d l a y o u t=True )
ax [ 0 ] . p l o t ( nn , i n t e g r a l 1 , ’ o − ’)
ax [ 0 ] . a x v l i n e ( x=0)
ax [ 0 ] . a x h l i n e ( y=np . s q r t ( np . p i ) , l i n e s t y l e = ’ : ’ )
ax [ 0 ] . g r i d ( True )
ax [ 0 ] . t e x t ( 1 0 , 3 , f ”max p o i n t s = {max( nn ) } f o r e r r o r l i m i t { r } ” )
ax [ 0 ] . s e t y l a b e l ( ” I n t e g r a l ( n ) $ \\ r i g h t a r r o w $ ” )
3
ax [ 0 ] . s e t x l a b e l ( ” no o f p o i n t s ( n ) $ \\ r i g h t a r r o w $ ” )
ax [ 0 ] . s e t t i t l e ( ” P l o t o f I n t e g r a l vs No o f P o i n t s ” )
#Simpson f o r d i f f e r e n t ( xf−x i )
def intg (x ) :
x i=−x
x f=x
n=1001
g ,m=0 ,0
h=( xf−x i ) / ( n−1)
yy = [ ]
f o r k in range (n ) :
y=f ( x i )
yy . append ( y )
x i=x i+h
f o r j in range (0 , n / / 2 ) :
g=g+4∗yy [ 2 ∗ j +1]
i f 2∗ j +2 < ( n −1):
m=m+2∗yy [ 2 ∗ j +2]
s =(h / 3 ) ∗ ( yy [ 0 ] + g+m+yy [ n −1])
return s
x =0.25
i n t e g r a l 2 , xx = [ ] , [ ]
w h i l e x<=4:
i n t e g r a l 2 . append ( i n t g ( x ) )
xx . append ( 2 ∗ x )
x+=0.25
#P l o t o f I n t e g r a l vs ( Xf−Xi )
ax [ 1 ] . p l o t ( xx , i n t e g r a l 2 , ’ o − ’)
ax [ 1 ] . a x v l i n e ( x=0)
ax [ 1 ] . a x h l i n e ( y=np . s q r t ( np . p i ) , l i n e s t y l e = ’ : ’ )
ax [ 1 ] . t e x t ( 4 , 1 , f ”No o f P o i n t s= {n } ” )
ax [ 1 ] . g r i d ( True )
ax [ 1 ] . s e t y l a b e l ( ” I n t e g r a l ( x ) $ \\ r i g h t a r r o w $ ” )
ax [ 1 ] . s e t x l a b e l ( ” ( Xf−Xi ) $ \\ r i g h t a r r o w $ ” )
ax [ 1 ] . s e t t i t l e ( ” P l o t o f I n t e g r a l vs ( Xf−Xi ) ” )
p l t . show ( )
4
Output:
5
2
Comparison Between Euler and Modified Euler Method
#Comaparison o f E u l e r and M o d i f i e d E u l e r
import m a t p l o t l i b . p y p l o t a s p l t
import numpy a s np
p l t . s t y l e . use (” c l a s s i c ”)
d e f d2y dx2 ( x , y , g ) :
e=np . exp
r e t u r n (−e ( x ) ∗ g)−y +(2∗(x−1)∗ e(−x))−x+1
d e f y1 ( x ) :
r e t u r n x∗np . exp(−x )
n=100 #no o f p o i n t s
x i=x=0
x f=4
y=0
g=1
h=( xf−x i ) / ( n−1)
xx , yy=[ x i ] , [ y1 ( x ) ]
y y e u l e r =[y ]
#E u l e r Method
f o r i in range (n ) :
g=g+h∗ d2y dx2 ( x , y , g )
y=y+h∗ g
x += h
yy . append ( y1 ( x ) )
xx . append ( x )
y y e u l e r . append ( y )
x i=x=0
x f=4
y=0
g=1
h=( xf−x i ) / ( n−1)
#M o d i f i e d E u l e r
y y m o d i f i e d =[y ]
f o r i in range (n ) :
xn=x
yn=y
6
gn=g
g=gn+h∗ d2y dx2 ( x , y , g )
y=yn+h∗ g
x +=h
g=gn +0.5∗h ∗ ( d2y dx2 ( xn , yn , gn ) + d2y dx2 ( x , y , g ) )
y=yn +0.5∗h ∗ ( gn+g )
y y m o d i f i e d . append ( y )
p l t . p l o t ( xx , yy , ’ − ’ , l a b e l =”Real P l o t ” )
p l t . p l o t ( xx , y y e u l e r , ’ o ’ , l a b e l =”E u l e r ” )
p l t . p l o t ( xx , y y m o d i f i e d , ’ ∗ ’ , l a b e l =”M o d i f i e d E u l e r ” )
p l t . y l a b e l ( ” f ( x ) $ \\ r i g h t a r r o w $ ” )
p l t . x l a b e l ( ” x $ \\ r i g h t a r r o w $ ” )
p l t . g r i d ( True )
p l t . l e g e n d ( l o c =”b e s t ” )
p l t . show ( )
Output:
7
3
Forced Oscillator 2nd Order Differential
#Forced O s c i l l a t o r
import m a t p l o t l i b . p y p l o t a s p l t
import numpy a s np
#D e f i n e t h e e q u a t i o n o f motion
d e f dgdt ( v , x , t ) :
F=0.2
s=2
Rm=5
m=4
w=np . p i /8
r e t u r n (F/m∗np . c o s (w∗ t ))− s /m∗x−Rm/m∗v
t=0
x =0.3
dxdt=v=−0.3
t f =30
n=10∗∗4
h=( t f −t ) / ( n−1)
xx , vv , t t =[x ] , [ dxdt ] , [ t ]
f o r i in range (n ) :
v=v+h∗ dgdt ( v , x , t )
x=x+h∗v
t=t+h
vv . append ( v )
xx . append ( x )
t t . append ( t )
f i g , ax=p l t . s u b p l o t s ( 2 , 1 , f i g s i z e = ( 8 , 8 ) , c o n s t r a i n e d l a y o u t=True )
ax [ 0 ] . p l o t ( t t , xx , ’ − − ’ , c o l o r =”r e d ” )
ax [ 0 ] . p l o t ( t t , vv , ’ − . ’ , c o l o r =”g r e e n ” )
ax [ 0 ] . a x v l i n e ( x=0, c o l o r =’ Black ’ )
ax [ 0 ] . a x h l i n e ( y=0, c o l o r =’ Black ’ )
ax [ 0 ] . g r i d ( True )
ax [ 0 ] . s e t y l a b e l ( ” v e l o c i t y o r d i s p l a c e m e n t $ \\ r i g h t a r r o w $ ” )
ax [ 0 ] . s e t x l a b e l ( ” time $ \\ r i g h t a r r o w $ ” )
ax [ 0 ] . s e t t i t l e ( ” P l o t o f v ( t ) vs time & x ( t ) vs time ” )
ax [ 1 ] . p l o t ( xx , vv , ’ − ’ , c o l o r =”g r e e n ” )
ax [ 1 ] . a x v l i n e ( x=0, c o l o r =’ Black ’ )
ax [ 1 ] . a x h l i n e ( y=0, c o l o r =’ Black ’ )
ax [ 1 ] . g r i d ( True )
8
ax [ 1 ] . s e t y l a b e l ( ” v ( t ) $ \\ r i g h t a r r o w $ ” )
ax [ 1 ] . s e t x l a b e l ( ” x ( t ) $ \\ r i g h t a r r o w $ ” )
ax [ 1 ] . s e t t i t l e ( ” P l o t o f v ( t ) vs x ( t ) ” )
p l t . show ( )
Output:
9
4
Fourier Series
#F o u r i e r A n a l y s i s f o r f ( x )
import m a t p l o t l i b . p y p l o t a s p l t
import numpy a s np
p l t . s t y l e . use (” c l a s s i c ”)
p i=np . p i
x i=−p i #Lower L im i t o f P e r i o d
x f=p i
#Upper L im it o f P e r i o d
L=( xf−x i ) #P e r i o d
n=100
#No o f p o i n t s i n F o u r i e r S e r i e s
npt =10∗∗4+1
#S t e p s f o r i n t e g r a t i o n
h=( xf−x i ) / ( npt −1)
#D e f i n e t h e o r i g i n a l Function
def f (x ) :
r e t u r n np . where ( ( x>=0),np . s i n ( x ) , 1 )
#D e f i n e S i n & Cos terms
def s i n i ( i , x ) :
r e t u r n np . s i n ( ( 2 ∗ p i /L) ∗ i ∗x )
def c o s i ( i , x ) :
r e t u r n np . c o s ( ( 2 ∗ p i /L) ∗ i ∗x )
aa , bb=np . z e r o s ( n+1) , np . z e r o s ( n+1)
xx=np . l i n s p a c e ( xi , xf , npt )
#F i n d i n g v a l u e s f o r Const terms
f o r i i n r a n g e ( n +1):
aa [ i ] = np . t r a p z ( c o s i ( i , xx ) ∗ f ( xx ) , xx ) / ( L/ 2 )
bb [ i ] = np . t r a p z ( s i n i ( i , xx ) ∗ f ( xx ) , xx ) / ( L/ 2 )
#C r e a t i n g Complete F o u r i e r S e r i e s
def fn (x ) :
g=aa [ 0 ] / 2
f o r i i n r a n g e ( 1 , n +1):
g += aa [ i ] ∗ c o s i ( i , x ) + bb [ i ] ∗ s i n i ( i , x )
return g
xx1=np . l i n s p a c e ( 2 ∗ xi , 2 ∗ xf , npt )
yy1=f n ( xx1 )
yy2=f ( xx1 )
10
p l t . p l o t ( xx1 , yy1 , l a b e l =”F o u r i e r Approximation o f f ( x ) ” )
p l t . p l o t ( xx1 , yy2 , ’ − − ’ , l a b e l =”Real Function ” )
p l t . x l a b e l ( ” x $ \\ r i g h t a r r o w $ ” )
p l t . y l a b e l ( ” f ( x ) $ \\ r i g h t a r r o w $ ” )
p l t . a x v l i n e ( x=0, l s = ’ −. ’)
p l t . a x h l i n e ( y=0, l s = ’ −. ’)
p l t . g r i d ( True )
p l t . t i t l e (” Fourier s e r i e s ”)
p l t . l e g e n d ( l o c =”b e s t ” )
p l t . ylim ( − 0 . 2 , 1 . 2 )
p l t . show ( )
Output:
11
5
Fourier - Gibbs Phenomena
12
Download