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,j =
φi+1,j + φi−1,j + φi,j+1 + φi,j−1 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 can’t 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
10 0
3-D surface plot of the potential error fit1 fit2
1.0 0.8 0.6
10 -2
0.4 10 -3
0.2
10 -4 0 10 -5
0
500
1000
1500
iteration
20
20 0.450
15
20
25
30 0
5
10
15
20
25
0.0 30
Vector plot of the current flow
15
0.150
0.600
0.300
25
0.750
10
30
25
15
5
2000
Contour plot of potential (Equipotential lines)
30
0.900
error (log scale)
10 -1
10
10
5
5 5
10
15
20
25
0
30
3
0
5
10
15
20
25
30
2. For Nx = 30, Ny =30, Nxbegin = 0, Nend = 30 (electrode covering surfaces)
Evolution of error with iteration
10 0
3-D surface plot of the potential error fit1 fit2
10 -1
1.0
error (log scale)
0.8 10 -2
0.6 0.4
10 -3
0.2
10 -4 10 -5 0 10 -6 0
500
1000
1500
iteration
0.150
15
0.300
15
0.450
20 0.600
20 0.750
25
0.900
25
15
20
25
30 0
5
10
15
25
Vector plot of the current flow
30
10
10
5
5 5
1.4
10
2000
Contour plot of potential (Equipotential lines)
30
5
20
0.0 30
10
15
20
25
0
30
0
5
10
15
20
25
30
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: 2 =
1 X L − yi 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 doesn’t approach zero in reasonable time, since the algorithm used is not so good.
4