Python Sem 4
Python Sem 4
4 Circuit Problem 11
4.1 Problem Statement . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.2 Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4.3 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Part I
1
Circle and Inscribed Square
# 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
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
# 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 ’
)
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
# 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
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
(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.
# 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 )
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
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
9
Circuit Problem
(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.
import numpy a s np
# 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)
# 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