0% found this document useful (0 votes)
0 views

Python Sem 4

Uploaded by

spandymandal05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Python Sem 4

Uploaded by

spandymandal05
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

MAC – 9 (LNB)

( Numerical Analysis using PYTHON )


Department – PHYSICS
Sem. – IV
Reg. No. – A01-1152 -111- 002 - 2023
of 2023 - 24
Roll No – 102

April 20, 2025


Contents

I Monte Carlo Integration 1

1 Circle and Inscribed Square 3


1.1 Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

2 Orthonormality of Sine Functions 5


2.1 Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.3 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

3 Common Area Estimation 7


3.1 Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2 Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.3 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

II Gauss Elimination and Backward Substitution 9

4 Circuit Problem 11
4.1 Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.2 Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.3 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Part I

Monte Carlo Integration

1
Circle and Inscribed Square

1.1 Problem Statement


Consider a square is inscribed in a circle having equation: x2 + y 2 = 2.
(a) Sketch the diagram in your answer script.
(b) Write a Python program to evaluate the area excluded by the square but included by the circle
using the Monte Carlo integration method.

1.2 Python Code

# Monte C a r l o I n t e g r a t i o n
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

def monte carlo ( radius ) :


n = 10∗∗5 # no o f p o i n t s
x i , x f , y i , y f = −r a d i u s , r a d i u s , −r a d i u s , r a d i u s
# c r e a t e random p o i n t s a l o n g x & y
xx random = np . random . rand ( n ) ∗ ( xf−x i ) + x i
yy random = np . random . rand ( n ) ∗ ( yf−y i ) + y i

inside = (
( xx random ∗∗2 + yy random ∗∗2 <= r a d i u s ∗ ∗ 2 ) &
( ( xx random < −1) | ( xx random > 1 ) | ( yy random < −1) | ( yy random >
1) )
)
ou ts ide = ˜( i n s i d e )

xx o u t = xx random [ o u t s i d e ]
yy o u t = yy random [ o u t s i d e ]
xx i n = xx random [ i n s i d e ]
yy i n = yy random [ i n s i d e ]

a r e a = ( yf−y i ) ∗ ( xf−x i )
i n t e g r a l = ( len ( yy in ) / n ∗ area )
return i n t e g r a l , xx in , yy in , xx out , yy out

r a d i u s = np . s q r t ( 2 )
i n t e g r a l a r e a , xx in , yy in , xx out , yy out = monte carlo ( radius )
print ( integral area )

p l t . f i g u r e ( f i g s i z e =(8 , 5 ) )
p l t . s c a t t e r ( x x i n , y y i n , c o l o r =”b l a c k ” , s =5, a l p h a =1, l a b e l =”P o i n t s
I n s i d e ”)

3
4 CHAPTER 1. CIRCLE AND INSCRIBED SQUARE

p l t . s c a t t e r ( xx out , yy out , c o l o r =”b l a c k ” , s =5, a l p h a =0.05 , l a b e l =”P o i n t s


O ut s id e ” )

# Add p l o t d e c o r a t i o n s
p l t . x l a b e l ( ” x−a x i s ” , f o n t s i z e =12)
p l t . y l a b e l ( ” y−a x i s ” , f o n t s i z e =12)
p l t . l e g e n d ( l o c =”upper r i g h t ” , f o n t s i z e =10)
p l t . g r i d ( True , l i n e s t y l e =’−−’, a l p h a =0.4)
p l t . a x i s (” t i g h t ”)

# Annotate t h e i n t e g r a l r e s u l t on t h e p l o t
plt . text (
0 . 0 5 , 0 . 9 5 , f ” Estimated Area = { i n t e g r a l a r e a : . 5 f } ” ,
t r a n s f o r m=p l t . gca ( ) . transAxes ,
f o n t s i z e =12 , bbox=d i c t ( f a c e c o l o r =’ white ’ , a l p h a =0.8) ,
v e r t i c a l a l i g n m e n t =’ top ’
)

plt . tight layout ()


p l t . show ( )

1.3 Output


(a) Area outside square but inside circle x2 + y 2 = 2 (b) Area inside square but outside circle
x2 + y 2 = 1

Figure 1.1: Monte Carlo Integration Results for Two Geometries


Orthonormality of Sine Functions

2.1 Problem Statement


Write down a Python program to check the following orthonormality relation through Monte Carlo
integration:
Z π
1
sin(nx) sin(mx) dx = δm,n for m, n ̸= 0
π −π

2.2 Python Code

# Monte C a r l o I n t e g r a t i o n
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

d e f m o n t e c a r l o ( mode , n ,m, xi , x f ) :
n0 = 10∗∗5 # no o f p o i n t s

# c r e a t e random p o i n t s a l o n g x & y
xx random = np . random . rand ( n0 ) ∗ ( xf−x i ) + x i

yy = np . s i n ( n∗ xx random ) ∗np . s i n (m∗ xx random ) i f mode==1 e l s e np . c o s ( n


∗ xx random ) ∗np . c o s (m∗ xx random )
y i = min ( yy ) i f min ( yy ) < 0 e l s e 0
y f = max( yy )

yy random = np . random . rand ( n0 ) ∗ ( yf−y i ) + y i


i n s i d e p o s i t i v e = ( ( yy >= 0 ) & ( yy random <= yy ) & ( yy random >= 0 ) )
i n s i d e n e g a t i v e = ( ( yy < 0 ) & ( yy random > yy ) & ( yy random < 0 ) )
inside = inside positive | inside negative
ou ts ide = ˜( i n s i d e )

y y i n p o s i = yy random [ i n s i d e p o s i t i v e ]
y y i n n e g a = yy random [ i n s i d e n e g a t i v e ]

xx o u t = xx random [ o u t s i d e ]
yy o u t = yy random [ o u t s i d e ]
xx i n = xx random [ i n s i d e ]
yy i n = yy random [ i n s i d e ]

a r e a = ( yf−y i ) ∗ ( xf−x i )
i n t e g r a l = ( ( l e n ( y y i n p o s i ) − l e n ( y y i n n e g a ) ) / n0 ∗ a r e a )
return i n t e g r a l

5
6 CHAPTER 2. ORTHONORMALITY OF SINE FUNCTIONS

z z s i n = np . z e r o s ( ( 6 , 6 ) )
z z c o s = np . z e r o s ( ( 6 , 6 ) )

f o r n in range (6) :
f o r m in range (6) :
i n t e g r a l a r e a = np . round ( m o n t e c a r l o ( 1 , n ,m,−np . pi , np . p i ) /np . p i ,
1)
z z s i n [ n ,m] = i n t e g r a l a r e a
i n t e g r a l a r e a = np . round ( m o n t e c a r l o ( 2 , n ,m,−np . pi , np . p i ) /np . p i ,
1)
z z c o s [ n ,m] = i n t e g r a l a r e a

print ( zz sin )
print ( zz cos )

2.3 Output

0 −0 0 −0 0 2 −0 −0 0 −0 0
   
0
0
 1 0 0 −0 0 

0
 1 0 −0 0 0
0
 0 1 0 0 −0

−0 0
 1 −0 0 −0
0
 0 0 1 −0 0 

0
 0 0 1 0 0
 0 −0 −0 0 1 0  0 −0 −0 −0 1 −0
−0 −0 −0 −0 0 1 0 0 −0 −0 −0 1

Figure 2.1: Orthogonality of Sin Figure 2.2: Orthogonality of Cos


Common Area Estimation

3.1 Problem Statement


Consider the two following functions:
1
f (x) = x2 and g(x) =
x2

(a) Give a free-hand sketch of these functions and identify the common area enclosed between them.

(b) Write a Python program to estimate the common area using the Monte Carlo integration
method.

3.2 Python Code

# Monte C a r l o I n t e g r a t i o n
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

def f (x) :
r e t u r n np . where ( x <= 1 , x ∗ ∗ 2 , 1/ x ∗ ∗ 2 )

d e f m o n t e c a r l o ( xi , xf , f ) :
n = 10∗∗5 # no o f p o i n t s

# c r e a t e random p o i n t s a l o n g x & y
xx random = np . random . rand ( n ) ∗ ( xf−x i ) + x i

yy = f ( xx random )
y i = min ( yy ) i f min ( yy ) < 0 e l s e 0
y f = max( yy )

yy random = np . random . rand ( n ) ∗ ( yf−y i ) + y i


i n s i d e p o s i t i v e = ( ( yy >= 0 ) & ( yy random <= yy ) & ( yy random >= 0 ) )
i n s i d e n e g a t i v e = ( ( yy < 0 ) & ( yy random > yy ) & ( yy random < 0 ) )
inside = inside positive | inside negative
ou ts ide = ˜( i n s i d e )

y y i n p o s i = yy random [ i n s i d e p o s i t i v e ]
y y i n n e g a = yy random [ i n s i d e n e g a t i v e ]

xx o u t = xx random [ o u t s i d e ]
yy o u t = yy random [ o u t s i d e ]
xx i n = xx random [ i n s i d e ]
yy i n = yy random [ i n s i d e ]

7
8 CHAPTER 3. COMMON AREA ESTIMATION

a r e a = ( yf−y i ) ∗ ( xf−x i )
i n t e g r a l = (( len ( y y i n p o s i ) − len ( yy in nega ) ) / n ∗ area )
return i n t e g r a l , xx in , yy in , xx out , yy out

i n t e g r a l a r e a , xx in , yy in , xx out , yy out = monte carlo (0 ,6 , f )


print ( integral area )

p l t . f i g u r e ( f i g s i z e =(8 , 5 ) )
p l t . s c a t t e r ( x x i n , y y i n , c o l o r =”b l a c k ” , s =5, a l p h a =1, l a b e l =”P o i n t s
I n s i d e ”)
p l t . s c a t t e r ( xx out , yy out , c o l o r =”b l a c k ” , s =5, a l p h a =0.05 , l a b e l =”P o i n t s
O ut s id e ” )

# Add p l o t d e c o r a t i o n s
p l t . x l a b e l ( ” x−a x i s ” , f o n t s i z e =12)
p l t . y l a b e l ( ” y−a x i s ” , f o n t s i z e =12)
p l t . l e g e n d ( l o c =”upper r i g h t ” , f o n t s i z e =10)
p l t . g r i d ( True , l i n e s t y l e =’−−’, a l p h a =0.4)
p l t . a x i s (” t i g h t ”)
plt . tight layout ()
p l t . show ( )

3.3 Output

1
Figure 3.1: Monte Carlo estimation of the common area under the curves y = x2 and y = x2
.
Part II

Gauss Elimination and Backward


Substitution

9
Circuit Problem

4.1 Problem Statement

(a) Draw the given circuit diagram with proper labeling of all components and four distinct current
variables through each branch in the clockwise direction.
(b) Apply Kirchhoff’s Voltage Law (KVL) to all closed loops in the circuit and write the resulting
system of linear equations in terms of the four unknown currents.
(c) Solve the system using the Gauss Elimination Method followed by Backward Substitution.
(d) Implement the solution using Python and display the current values accurate to 3 decimal places.

4.2 Python Code

import numpy a s np

# Augmented matrix [A| b ] r e p r e s e n t i n g t h e system o f l i n e a r e q u a t i o n s


A = np . a r r a y ( [
[ 1 , −1, −1, 0 , 0 ] ,
[4 , 5 , 0 , 3 , 5] ,
[ 0 , 3 , −6, 1 4 , 3 ] ,
[ 4 , 0 , 7 , −6, 5 ]
])

n = l e n (A) # Number o f e q u a t i o n s ( rows )

# Forward E l i m i n a t i o n ( Gauss E l i m i n a t i o n )
f o r i i n r a n g e ( 0 , n−1) :
# P a r t i a l p i v o t i n g : swap row with maximum e l e m e n t i n c u r r e n t column
g = i + np . argmax (A[ i : , i ] )

11
12 CHAPTER 4. CIRCUIT PROBLEM

A[ [ i , g ] ] = A[ [ g , i ] ]

# E l i m i n a t e e n t r i e s below t h e p i v o t
f o r j i n r a n g e ( i +1, n ) :
u = A[ j , i ] / A[ i , i ] # M u l t i p l i e r f o r t h e c u r r e n t row
A[ j , i : ] = A[ j , i : ] − u ∗ A[ i , i : ]

p r i n t ( ” Upper T r i a n g u l a r Matrix : ” )
p r i n t (A)

# Back S u b s t i t u t i o n to f i n d the s o l u t i o n vector I


I = np . z e r o s ( n )
f o r k i n r a n g e ( n−1 , −1, −1) :
s = np . sum (A[ k , k+1:n ] ∗ I [ k + 1 : ] ) # Sum o f known v a r i a b l e s
I [ k ] = (A[ k , n ] − s ) / A[ k , k ] # Solve f o r current variable

# P r i n t t h e r e s u l t rounded t o 3 d e c i m a l p l a c e s
p r i n t (” Solution ( Currents ) : ” )
p r i n t ( np . round ( I , 3 ) )

4.3 Output
I0 = 0.875 A
I1 = 0.000 A
I2 = 0.667 A
I3 = 0.500 A

You might also like