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

Solving Laplace in Python

The document discusses solving Laplace's equation numerically to calculate the potential distribution in a resistor. It provides the Python code used and plots of the error over iterations, 3D surface plot of potential, contour plot of potential lines, and vector plot of current flow.

Uploaded by

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

Solving Laplace in Python

The document discusses solving Laplace's equation numerically to calculate the potential distribution in a resistor. It provides the Python code used and plots of the error over iterations, 3D surface plot of potential, contour plot of potential lines, and vector plot of current flow.

Uploaded by

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

Week 5: Laplace Equations

Shyam Shankar H R
EE15B127
Electrical Dept.
February 23, 2017

1 Introduction
This report contains the solutions of the week-5 assignment of EE2307. The answers to the questions, the python code, and
the plots obtained are entered below.

1.1 The python code


from pylab import *
from numpy import *
import mpl_toolkits.mplot3d.axes3d as p3
print Enter arguments in the form Nx Ny Nbegin Nend Niter
if(len(sys.argv)==6):
Nx=int(sys.argv[1])
Ny=int(sys.argv[2])
else:
Nx=30
Ny=30
def potential(Nx,Ny):
Nbegin = 0
Nend = Ny
Niter = 2000 #.... Initialising the phi matrix (with electrode potential)
phi = zeros((Nx,Ny))
phi[0,Nbegin:Nend] = 1
phi[Ny-1,Nbegin:Nend] = 0
errors = zeros(Niter)
print phi
for k in arange(Niter):
#.... saving a copy for error check.....
oldphi = phi.copy()
#.... updating phi array.........
phi[1:-1,1:-1] = 0.25*(phi[1:-1,0:-2]+phi[1:-1,2:]+phi[0:-2,1:-1]+phi[2:,1:-1])
#.... assering boundaries.......
phi[1:-1,0] = phi[1:-1,1]
phi[1:-1,Nx-1] = phi[1:-1,Nx-2
phi[0,0:Nbegin] = phi[1,0:Nbegin
phi[0,Nend:] = phi[1,Nend:]
phi[Ny-1,0:Nbegin] = phi[Ny-2,0:Nbegin]
phi[Ny-1,Nend:] = phi[Ny-2,Nend:]
#.... computing error........
errors[k] = (abs(oldphi-phi)).max()

k = arange(Niter)

1
#....... Getting best-fit line
#..... y = mx+c ==> x(m)+ 1(c) = y
#M c = b form, where M = [x 1], b = y
# So, c = lstsq(M,b)
b = arange(Niter)
M1 = zeros((Niter,2))
M1[:,0] = 1 M1[:,1] = b
b = log(errors)
c = lstsq(M1,b)[0]
M2 = zeros((Niter-500,2))
b2=arange(500,Niter)
M2[:,0]=1
M2[:,1]=b2
b2=log(errors[500:])
c2=lstsq(M2,b2)[0]
x=linspace(0,Niter-1,Niter)
#.... Error v/s iterations plot .....
fig1 = figure()
semilogy(k,errors,ro,markevery=50)
semilogy(x, exp(c[0]+c[1]*x), r)
semilogy(x[500:],exp((c2[0]+c2[1]*x[500:])),g)
suptitle("Evolution of error with iteration", fontsize=20)
xlabel("iteration", fontsize=16)
ylabel("error (log scale)", fontsize=16)
savefig("fig_err.pdf")
#.... Surface plot of potential ......
fig2 = figure(4)
ax = p3.Axes3D(fig2) #... Axes3D is used to do the surface plot
x = arange(1,Nx+1)
y = arange(1,Ny+1)
X,Y = meshgrid(x,y) #... creates arrays out of x and y
title("3-D surface plot of the potential")
surf = ax.plot_surface(Y, X, phi, rstride=1, cstride=1, cmap=cm.jet, linewidth=0)
savefig("fig_surf.pdf")
#.... Contour plot of potential ......
fig3 = figure()
title("Contour plot of potential (Equipotential lines)")
cp = contour(Y, X, phi)
clabel(cp, inline=True, fontsize=10)
savefig("fig_cont.pdf")
#..... Evaluating current .....
fig4 = figure() Jx = zeros((Nx,Ny))
Jy = zeros((Nx,Ny))
Jx[1:-1,1:-1] = 0.5*(phi[1:-1,0:-2]-phi[1:-1,2:])
Jy[1:-1,1:-1] = 0.5*(phi[0:-2,1:-1]-phi[2:,1:-1])
quiver(y,x,Jy.transpose(),Jx.transpose())
title("Vector plot of the current flow")
savefig("fig_cur.pdf")
show()
return phi
phi = potential(Nx,Ny)
E=zeros(Nx)
for i in range(Ny):
E[i]=sum(pow((phi[:,i]-1*(Ny-i)/Ny),2))
E1=sum(E)/(Nx*Ny)
print E1

2
1.2 Theory
The potential distribution inside resistor is found by solving Laplace equation:

2 = 0

To solve it numerically, we compute


i+1,j + i1,j + i,j+1 + i,j1
i,j =
4
Here we utilize vectors for time-efficiency.Thus the potential at any point should be the average of its neighbours.
We keep iterating till the solution converges, i.e, max change of elements in fall below a certain value (tolerance). At
boundaries where the electrode is present, we just put the value of potential itself. At boundaries where there is no electrode,
the current should be tangential because charge cant leap out of the material into air. Since current is proportional to the
Electric Field, what this means is the gradient of should be tangential. This is implemented by requiring that should not
vary in the normal direction.

1.3 Plots obtained:


(For all cases, electrode potentials are taken +1V (top) and 0V (bottom))
1. For Nx = 30, Ny =30, Nxbegin = 10, Nend = 20

Evolution of error with iteration 3-D surface plot of the potential


10 0
error
fit1 1.0
10 -1 fit2
0.8
error (log scale)

0.6
10 -2
0.4

10 -3 0.2
0.0
30
10 -4 25
20
0 15
5
10 -5 10 10
0 500 1000 1500 2000 15
20 5
iteration 25
30 0

30 Contour plot of potential (Equipotential lines) 30 Vector plot of the current flow

25 25

20 20
0.600

0.300

15 15
0.450
0.900

0.750

0.150

10 10

5
5

0
5 10 15 20 25 30 0 5 10 15 20 25 30

3
2. For Nx = 30, Ny =30, Nxbegin = 0, Nend = 30 (electrode covering surfaces)

Evolution of error with iteration 3-D surface plot of the potential


10 0
error
fit1 1.0
10 -1 fit2
0.8
10 -2
error (log scale)

0.6

10 -3 0.4

0.2
10 -4
0.0
30
10 -5 25
20
0 15
5
10 -6 0 10 10
500 1000 1500 2000 15
20 5
iteration 25
30 0

30 Contour plot of potential (Equipotential lines) 30 Vector plot of the current flow

25 25

20 20

15 15
0.900

0.750

0.600

0.450

0.300

0.150

10 10

5
5

0
5 10 15 20 25 30 0 5 10 15 20 25 30

1.4 Verification (Least Square Deviation)


Calling the function for the case of the electrode covering the entire top and bottom surfaces, we obtain a potential distribution:
i,j . To compute the least square deviation, we use:

1 X L yi 2
2 = (i,j V0 )
Nx Ny i,j L

For Nx = 30, Ny = 30, with electrodes covering the entire top and bottom surfaces, we get 2 = 0.338
Similarly, for Nx = 60, Ny = 60, 2 = 0.254, and for Nx = 150, Ny = 150, 2 = 0.106.
We observe that2 decreases as the electrode size increases. It is because as the electrode size increases, the fringing effect
involed in the field distribution becomes insignificant. This enables the computed values to resemble the physical values to a
greater extent. The value of 2 doesnt approach zero in reasonable time, since the algorithm used is not so good.

You might also like