fdm numerical solution of laplace equation using matlab
DESCRIPTION
Finite Difference Method Numerical solution of Laplace Equation using MATLAB. 2 computational methods are used. U can vary the number of grid points and the boundary conditionsTRANSCRIPT
Numerical Analysis
Visit my BlogSpot
http://ayaozaki.blogspot.com/2014/06/fdm-numerical-solution-of-laplace.html
6/25/2014 Aya Zaki 1
A Finite Difference Method for Laplace’s Equation
• A MATLAB code is introduced to solve Laplace Equation.
• 2 computational methods are used:– Matrix method
– Iteration method
• Advantages of the proposed MATLAB code:– The number of the grid point can be freely chosen
according to the required accuracy.
– The boundary conditions can simply be changed.
6/25/2014 Aya Zaki 2
A Finite Difference Method for Laplace’s Equation (cont.)
• Example (Sheet 4)
• Grid: N=3
• B.C. shown
6/25/2014 Aya Zaki 3
50
37.5
25
12.5
37.52512.5
000
0
0
0
0
0
200mm
200mm
T(i,j)
I. Matrix computation method
• Example (Sheet 4)• Grid: N=3• The code generates the equations to be solved: 𝑨 𝑼 = 𝑩
6/25/2014 Aya Zaki 4
k
-4 1 0 1 0 0 0 0 01 -4 1 0 1 0 0 0 00 1 -4 0 0 1 0 0 01 0 0 -4 1 0 1 0 00 1 0 1 -4 1 0 1 00 0 1 0 1 -4 0 0 10 0 0 1 0 0 -4 1 00 0 0 0 1 0 1 -4 10 0 0 0 0 1 0 1 -4
00
-12.500
-25. 0-12.5-25.0-75.0
T11T21T31T21T22T23T31T32T33
=
U = inv(A)*B;
%Re-arrange
for j= 1:N
for i=1:N
T(j+1,i+1)= U((j-
1)*N+i);
end
end
for i= 1:N
for j=1:N
k= (j-1)*N +i;
A(k,k)= -4;
for m = i-1: 2:i+1
if ((m<1) ||(m>N))
B(k)= B(k) -T(m+1,j+1);
else
l = (j-1)*N+m;
A(k,l)= 1;
end
end
for n = j-1: 2:j+1
if ((n<1) ||(n>N))
B(k)= B(k)- T(i+1,n+1);
else
l = (n-1)*N+i;
A(k,l)= 1;
end
end
end
end
I. Matrix computation method(cont.)
• Code
6/25/2014 Aya Zaki 5
N=3;
T = zeros(N+2, N+2);
x = linspace(0,200e-3, N+2);
y = linspace(0,200e-3, N+2);
%Boundary Conditions
% Y- left
T(:,N+2) = linspace(0,50,N+2)
% Top
T(N+2,:)= linspace(0,50,N+2)
A , B
• T=
I. Matrix computation method(cont.)
6/25/2014 Aya Zaki 6
0 0 0 0 0
0 3.125 6.25 9.375 12.5
0 6.25 12.5 18.75 25
0 9.375 18.75 28.125 37.5
0 12.5 25 37.5 50
50
37.5
25
12.5
37.52512.5
000
0
0
0
0
0 3.125 6.25 9.375
6.25
9.375
12.5 18.75
18.75 28.125
I. Matrix computation method(cont.)
6/25/2014 Aya Zaki 7
0
0.05
0.1
0.15
0.2
0
0.05
0.1
0.15
0.20
10
20
30
40
50
Distance x
Numerical solution computed with solving the Matrix.
Distance y
surf(x,y,T);
• Plotting T
• Plotting T
I. Matrix computation method(cont.)
6/25/2014 Aya Zaki 8
contour(x,y,T);
Temperature plot (contourf)
0 0.05 0.1 0.15 0.20
0.02
0.04
0.06
0.08
0.1
0.12
0.14
0.16
0.18
0.2
0
5
10
15
20
25
30
35
40
45
• N= 50
I. Matrix computation method(cont.)
6/25/2014 Aya Zaki 9
0
0.05
0.1
0.15
0.2
0
0.05
0.1
0.15
0.20
10
20
30
40
50
Distance x
Numerical solution computed with solving the Matrix.
Distance y
for k=1:20
for i=2:N+1
for j=2:N+1
un(i,j)=(un(i+1,j)+un(i-1,j)+un(i,j+1)+un(i,j-1))/4;
end
end
end
II. Iteration computation method• Code
– Number of iterations = 20 – The better the initial guess, the faster the computation is.– For simplicity, the initial value for all points is chosen as zero.
6/25/2014 Aya Zaki 10
N=3;
T = zeros(N+2, N+2);
x = linspace(0,200e-3, N+2);
y = linspace(0,200e-3, N+2);
%Boundary Conditions
% Y- left
T(:,N+2) = linspace(0,50,N+2)
% Top
T(N+2,:)= linspace(0,50,N+2)
• T=
II. Iteration computation method (cont.)
6/25/2014 Aya Zaki 11
0 0 0 0 0
0 3.125 6.25 9.375 12.5
0 6.25 12.5 18.75 25
0 9.375 18.75 28.125 37.5
0 12.5 25 37.5 50
• Plotting T
II. Iteration computation method (cont.)
6/25/2014 Aya Zaki 12
0
0.05
0.1
0.15
0.2
0
0.05
0.1
0.15
0.20
10
20
30
40
50
Distance x
Numerical solution computed with 20 iteration.
Distance y
The same results were obtained as before.
0
0.05
0.1
0.15
0.2
0
0.05
0.1
0.15
0.20
10
20
30
40
50
• Plotting T
II. Iteration computation method (cont.)
6/25/2014 Aya Zaki 13
0
0.05
0.1
0.15
0.2
0
0.05
0.1
0.15
0.20
10
20
30
40
50
Distance x
Numerical solution computed with 20 iteration.
Distance y
The same results were obtained as before.
0
0.05
0.1
0.15
0.2
0
0.05
0.1
0.15
0.20
10
20
30
40
50
0
0.05
0.1
0.15
0.2
0
0.05
0.1
0.15
0.20
10
20
30
40
50
• Plotting T
II. Iteration computation method (cont.)
6/25/2014 Aya Zaki 14
0
0.05
0.1
0.15
0.2
0
0.05
0.1
0.15
0.20
10
20
30
40
50
Distance x
Numerical solution computed with 20 iteration.
Distance y
The same results were obtained as before.
A Finite Difference Method for Laplace’s Equation (cont.)
• Example (Sheet 4)
• Grid: N=3
• B.C. with lower edge insulated.
6/25/2014 Aya Zaki 15
50
37.5
25
12.5
37.52512.5
000
0
0
0
0
0
200mm
200mm
T(i,j)
Lower Edge Insulated
• Example (Sheet 4)
• Grid: N=3
• B.C. with lower edge insulated.
6/25/2014 Aya Zaki 16
50
37.5
25
12.5
37.52512.5
0
0
0
0
T
0 5.0367 8.6453 8.6450 0
0 5.7508 10.4498 12.9673 12.5000
0 7.5166 14.4358 20.2743 25.0000
0 9.8797 19.5024 28.6942 37.5000
0 12.5000 25.0000 37.5000 50.0000
Lower Edge Insulated
• Plot of T
6/25/2014 Aya Zaki 17
N =3
0
0.05
0.1
0.15
0.2
0
0.05
0.1
0.15
0.20
10
20
30
40
50
Distance x
Numerical solution computed.
Distance y
Lower Edge Insulated
• N = 100
6/25/2014 Aya Zaki 18
0
0.05
0.1
0.15
0.2
0
0.05
0.1
0.15
0.20
10
20
30
40
50
Distance x
Numerical solution computed.
Distance y
0 0.05 0.1 0.15 0.20
10
20
30
40
50
Distance y
Tem
pera
ture
(C
)
Plot of Temperature at different x points
0mm
100mm
200mm
Lower Edge Insulated
• N = 100
6/25/2014 Aya Zaki 19
Temperature plot (contourf)
0 0.05 0.1 0.15 0.20
0.02
0.04
0.06
0.08
0.1
0.12
0.14
0.16
0.18
0.2
0
5
10
15
20
25
30
35
40
45