Sie sind auf Seite 1von 10

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:
∂ 2u ∂ 2u
+ =0
∂x 2 ∂y 2

Decartelization:

U i +1, j − 2U i , j +U i −1, j U i , j +1 − 2U i , j +U i , j −1
+ =0 O ((∆x) 2 , ( ∆y ) 2 )
( ∆x ) 2
( ∆y ) 2

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 inputs
User will be asked if he wants to set the boundary conditions on the middle of the line or
on the corner
And 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 middle


reply = 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';
end
if isempty(maxerr)
maxerr = .00001;
end
mymatrix=zeros(M,N);
reply=lower(reply);
if reply=='c'
for i=1:floor(M/3)
mymatrix(i,1)=1;
end
else
for i=floor(M/3):floor(2*M/3)
mymatrix(i,1)=1;
end
end

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 error


53 Iterations

The same problem, Elemental representation


A 100×100 grid with default error
2257 Iterations

The same problem elemental representation


Surface View

Middle

A 10×10 grid with default error


64 Iterations
The same problem, Elemental representation

A 100×100 grid

A 100×100 grid with default error


2999 Iterations
Surface Mesh Representation

N (N×N grid) BC type Iterations


10 Corner 53
20 Corner 189
30 Corner 394
50 Corner 866
100 Corner 2267

Iteration VS Grid Size

2500

2257

2000

1500
Iterations

1000
866

500
394
189
0 53
0 20 40 60 80 100 120
N (NxN grid)

Das könnte Ihnen auch gefallen