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 conditions
chapter 5.pptx: drainage and irrigation engineering
FDM Numerical solution of Laplace Equation using MATLAB
1. Numerical Analysis
Visit my BlogSpot
http://ayaozaki.blogspot.com/2014/06/
fdm-numerical-solution-of-laplace.html
6/25/2014 Aya Zaki 1
2. 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
3. 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)
5. 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
9. • 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.2
0
10
20
30
40
50
Distance x
Numerical solution computed with solving the Matrix.
Distance y
10. 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)
12. • 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.2
0
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.2
0
10
20
30
40
50
13. • 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.2
0
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.2
0
10
20
30
40
50
0
0.05
0.1
0.15
0.2
0
0.05
0.1
0.15
0.2
0
10
20
30
40
50
14. • 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.2
0
10
20
30
40
50
Distance x
Numerical solution computed with 20 iteration.
Distance y
The same results were obtained as before.
15. 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)