Академический Документы
Профессиональный Документы
Культура Документы
ee16b131
Aravinda Prasad
1 INTRODUCTION:
Solving for the currents in a resistor. The currents depend on the shape of the resistor and we want to
see if R = rL/A works or not.
1
can't leap out of the material into air. Since current is proportional to the Electric Field, what this
means
is the gradient of f should be tangential
4 code
from pylab import *
from numpy . l i n a l g import lstsq
import math
import m p l _ t o o l k i t s . mplot3d . a x e s 3 d as p3
#f u n c t i o n for s o l v i n g . . . . . pi , e r r o r s
def f u n s o l v e r ( s i z e , Nx , Ny ) :
i f ( ( Ny− s i z e )%2 == 0 ) :
N b e g i n = ( Ny− s i z e ) / 2
else :
N b e g i n = ( ( Ny− s i z e ) / 2 ) + 1
Nend = Ny−N b e g i n
p i = z e r o s ( ( Nx , Ny ) )
p i [ 0 , N b e g i n : Nend +1] = 1
p i [ − 1 , N b e g i n : Nend +1] = 0
errors = []
for i in range ( Niter ) :
o l d p i = p i . copy ( )
#u p d a t i n g . . . . . .
p i [1: −1 ,1: −1]= ( 0 . 2 5 ) * ( p i [1: −1 ,0: −2]+ p i [ 1 : − 1 , 2 : ] + p i [0: −2 ,1: −1]+ p i [ 2 : , 1 : − 1 ] )
#b o u n d a r y . . . . . .
pi [1: −1 ,0] = pi [1: −1 ,1]
p i [1: −1 , −1] = p i [1: −1 , −2]
#t o p . . r o w s
p i [ 0 , 1 : Nbegin ] = p i [ 1 , 1 : Nbegin ]
p i [ 0 , ( Nend + 1 ) : − 1 ] = p i [ 1 , ( Nend + 1 ) : − 1 ]
#bottom . . . . r o w s
pi [ − 1 , 1 : Nbegin ] = pi [ − 2 , 1 : Nbegin ]
p i [ − 1 , Nend +1: − 1] = p i [ − 2 , Nend +1: − 1]
#e r r o r . . .
e r r o r s . append ( ( a b s ( p i − o l d p i ) ) . max ( ) )
return p i , e r r o r s , Nx , Ny
# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
#p l o t s . . . . r a n g e of x
x = l i n s p a c e ( 1 , N i t e r +1 , N i t e r )
x1 = r a n g e ( 0 , N i t e r , 5 0 )
x2 = r a n g e ( 5 0 0 , N i t e r , 5 0 )
x3 = r a n g e ( 0 , N i t e r , 1 )
x4 = l i n s p a c e ( 5 0 1 , N i t e r +1 , N i t e r − 500)
err1 = errors [ : : 5 0 ]
err2 = errors [500:]
#f i t s . . . e r r o r s
P=z e r o s ( ( N i t e r , 2 ) )
P[: ,0] = 1
P[: ,1] = x
Q = log ( errors )
X = l s t s q (P ,Q ) [ 0 ]
A = e * * (X [ 0 ] )
B = X[ 1 ]
print "fit1 values of A, B" , A, B
d = Niter −500
p1 = z e r o s ( ( d , 2 ) )
p1 [ : , 0 ] = 1
p1 [ : , 1 ] = x4
q1 = l o g ( e r r 2 )
X1 = l s t s q ( p1 , q1 ) [ 0 ]
A1 = e * * ( X1 [ 0 ] )
B1 = X1 [ 1 ]
print "fit2 values of A, B" , A1 , B1
y1 = A* e x p (B* x )
y2 = A1 * e x p ( B1 * x4 )
#S t o p p i n g conditons . . .
N = 2000
e r r o r S C = (−A/B ) * ( e x p (B * (N+ 0 . 5 ) ) )
print " error_stopping c o n d i t i o n " , errorSC
#p l o t s . . . .
t i t l e (" Evolution of error with i t e r a t i o n ")
xlabel ( ' iteration ')
p l o t ( x1 , e r r 1 , " r o " , l a b e l = ' error ' )
y s c a l e ( ' log ' )
p l o t ( x , y1 , " r " , l a b e l = " f i t 1 ")
y s c a l e (" log ")
p l o t ( x4 , y2 , " g " , l a b e l = ' fit2 ')
y s c a l e ( ' log ' )
legend ( loc = ' upper right ' )
show ( )
#3D . . . . .
f i g 1=f i g u r e ( 4 ) # open a new figure
ax=p3 . Axes3D ( f i g 1 ) # Axes3D is the means to do a surface plot
x=a r a n g e ( 1 , Nx+1) # c r e a t e x and y axes
y=a r a n g e ( 1 , Ny+1)
X, Y=m e s h g r i d ( x , y ) # c r e a t e s arrays out of x and y
t i t l e ( ' The 3−D s u r f a c e plot of the potential ')
s u r f = ax . p l o t _ s u r f a c e (Y, X, pi , r s t r i d e =1 , c s t r i d e =1 , cmap=cm . j e t , l i n e w i d t h = 0
show ( )
#c o n t o u r . . . . p l o t
contour ( pi , 5 0 )
t i t l e ( " Contour plot of p o t e n t i a l ")
show ( )
#v e c t o r . . . . . . p l o t
Jx = z e r o s ( ( Nx , Ny ) )
Jy = z e r o s ( ( Nx , Ny ) )
Jx [ 1 : −1 ,1: −1] = ( 0 . 5 ) * ( p i [1: −1 ,: −2] − p i [ 1 : −1 ,2:])
Jy [ 1 : − 1 , 1 : − 1 ] = ( 0 . 5 ) * ( p i [: −2 ,1: −1] − p i [ 2 : , 1 : − 1 ] )
q u i v e r ( y , x , Jy . t r a n s p o s e ( ) , Jx . t r a n s p o s e ( ) )
t i t l e ( ' Vector plot of the current flow ' )
show ( )
J y t = Jy . t r a n s p o s e ( )
J x t = Jx . t r a n s p o s e ( )
#a v g values . . .
I a v g _ e n t e r = sum ( J y t [ : , 1 ] )
I a v g _ o u t = sum ( J y t [ : , −2])
Iavg = ( 0 . 5 ) * ( I a v g _ e n t e r+I a v g _ o u t )
Idiff = I a v g _ e n t e r −I a v g _ o u t
print " I_avg " , I a v g
print " I_diff " , I d i f f
# least squares deviation
E2 = 0
for i in r a n g e ( Ny ) :
for j in r a n g e ( Nx ) :
E2 += ( 1 . 0 / ( Nx * Ny ) ) * ( ( p i [ i , j ] − ( ( V0 ) * ( Nx− j ) / Nx ) ) * * ( 2 ) )
print " deviation " , E2
5 GRAPHS
Errors:
Errors reduce very slowly,it is an exponential decay only for larger iteration numbers.
Remember that if the t is of the form
Bx
y = Ae
the thing to do is to take the log. Then we have
logy = logA+Bx
That is why it looks like a straight line in a semi-log plot
6 stopping condition
error
Bx
y = Ae
A
Error = -
B
exp(B(N + 0.5))
for N =2000 the error is approximately 0.0.2.
5
6
8 Contour Plot of the Potential
10 Resistance
Iavg is the average of the entering and leaving currents, while Idi is the dierence
between the entering and the leaving currents. If the code has converged, Idi should have gone to
zero.
I got the values as..for N =2000 for 30 by30 grid.
I_avg 0.67406033725
I_di 0.112581909188
8
11 Theory
If the electrodes covered the entire top and
bottom surfaces, we can then solve the problem easily, and the answer is:
L−y
f = V0
L
This is got from solving Laplace's equation.
9
10
11
13 Verication
Least squares deviation:
1
(ϕij − V 0 L−yi 2
P
e2 = )
N xN y L
Now varying Nx and Ny both, keeping the ratio xed we get,
for Nx=Ny=30 we get the deviation 0.33805250084
for Nx=Ny=100 we get the deviation 0.291185148561
for Nx=Ny=500 we get the deviation 0.0323666796814
for Nx=Ny=100 we get the deviation 0.0162348119522
.....
so,
e2 decays (decreses) slowly with the increse of Nx,Ny.
12
for very large values of Nx,Ny it approximately goes to zero.
13