Вы находитесь на странице: 1из 13

Week 5 assignment on Python: Laplace Equation

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.

2 The Resistor Problem:


A voltage VAB = 1V is applied across the terminals of a resistor.
As a result, current ows. The current at each


point can be described by a current density j. This
current density is related to the local Electric Field
by the conductivity.
~j ~
= svE
Now the Electric eld is the gradient of the potential,
~
E = =∇f
and continuity of charge yields
∇·~j = - dρ
dt
Combining these equations we obtain
∇·(=sv∇f) = = ∂ρ∂t
Assuming that our resistor contains a material of constant conductivity, the equation becomes
∇2 φ= σ1 ∂ρ
∂t
For DC currents, the right side is zero, and we obtain
∇2 φ= 0.

3 Numerical Solution in 2-Dimensions


Laplace's equation is easily transformed into a dierence equation. The equation can be written out in
Cartesian coordinates
∂2ϕ
∂x2
+ ∂2ϕ
∂y2
= 0
we obtain,
ϕi+1,j+ϕi−1,j+ϕi,j+1+ϕi,j−1
fi, j =
4
Thus, if the solution holds, the potential at any point should be the average of its neighbours. This
is a very
general result and the above calculation is just a special case of it.
At boundaries where the electrode is present, just put the value of
potential itself. At boundaries where there is no electrode, the current

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

# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

N i t e r =2000 # number of iterations to perform


V0 = 1
p i , e r r o r s , Nx , Ny = f u n s o l v e r ( 1 0 , 3 0 , 3 0 )

#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.

7 Surface Plot of Potential


For the default values I got the following plot (the boundary conditions
were slightly dierent with V2 = 0):

5
6
8 Contour Plot of the Potential

9 Vector Plot of Currents


Our equations are,
jx = =∂ f/∂ x
jy = =∂ f/∂ y
This numerically translates to
Jx,i j = (fi, j=1 =fi, j+1)
1
2
Jy,i j = (fi=1, j =fi+1, j)
1
2
Plotting the current density using quiver. Here is what I got:
The current quickly spreads out to ll the entire cross-section and then ows in the direction of the
negative electrode. There is a corresponding gathering of current at the negative electrode since
current can
only enter or leave via the electrodes.

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.

12 Same plots for Nx=Ny =size of electrode

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

Вам также может понравиться