solution for the laplace equation
TRANSCRIPT
Numerical Solution for the Laplace Equation
© Siamak Faridani, Oct 2006
for more information visit pitchup.com
Files:
Matlab Code
PDF Report
Abstract:In this homework Laplace equation has been solved for a rectangle, all the values on the boundary has been set to zero except for one third of one of the sides
One third of the left side is set to 1
Laplace Equation:
02
2
2
2
y
u
x
u
Decartelization:
))(,)((0)(
2
)(
222
2
1,,1,
2
,1,,1 yxOy
UUU
x
UUU jijijijijiji
Approach:Since in the former homework I faced some difficulties with Fortran and I was afraid of using it again, I chose MATLAB to solve this problem
Inputs
Number of the nodes in x and y direction will be given to the program as inputsUser will be asked if he wants to set the boundary conditions on the middle of the line or on the cornerAnd the desired maximum error will be asked as well
N = input('Number of Nodes in Y '); M = input('Number of Nodes in X ');
% Boundary values can be set along the corner or in the middlereply = input('Corner or Middle boundary conditions C/M [C]: ', 's');
%Maximum Error abs(x2-x1)maxerr= input('Desired Maximum Error [.00001]: ');
For boundary conditions the default mode is on the corner and for the maximum error the default mode is .00001. User can change them if he wants
Matlab Code:
% Laplace Equation Solver
% Siamak Faridani
% Oct 2, 2006
clc;clear all;close all;
N = input('Number of Nodes in Y '); M = input('Number of Nodes in X ');
% Boundary values can be set along the corner or in the middle
reply = input('Corner or Middle boundary conditions C/M [C]: ', 's');
%Maximum Error abs(x2-x1)
maxerr= input('Desired Maximum Error [.00001]: ');if isempty(reply) reply = 'C';endif isempty(maxerr) maxerr = .00001;endmymatrix=zeros(M,N);reply=lower(reply);if reply=='c' for i=1:floor(M/3) mymatrix(i,1)=1;endelse for i=floor(M/3):floor(2*M/3) mymatrix(i,1)=1;endend
maxr=1;errormatrix=zeros(M,N);itteration=0;while maxr>maxerr for i=2:M-1 for j=2:N-1 tempval=mymatrix(i,j); mymatrix(i,j)=(mymatrix(i+1,j)+mymatrix(i-1,j)+mymatrix(i,j+1)+mymatrix(i,j-1))/4; errormatrix(i,j)=abs(mymatrix(i,j)-tempval); end
end maxr=max(max(errormatrix)); itteration=itteration+1;end
contourf (mymatrix, 'DisplayName', 'mymatrix'); figure(gcf)itteration
Test Cases:
A 10×10 grid with default error53 Iterations
The same problem, Elemental representation
A 100×100 grid with default error2257 Iterations
The same problem elemental representation
Surface View
Middle
A 10×10 grid with default error64 Iterations
The same problem, Elemental representation
A 100×100 grid
A 100×100 grid with default error2999 Iterations
Surface Mesh Representation
N (N×N grid) BC type Iterations 10 Corner 5320 Corner 18930 Corner 39450 Corner 866100 Corner 2267
Iteration VS Grid Size
53189
394
866
2257
0
500
1000
1500
2000
2500
0 20 40 60 80 100 120
N (NxN grid)
Iter
atio
ns