Numerical Solution for the Laplace Equation

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 ( ∆x )
2

+

U i , j +1 − 2U i , j +U i , j −1 (∆ ) 2 y

=0

O ((∆x) 2 , ( ∆ ) 2 ) y

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(i1,j)+mymatrix(i,j+1)+mymatrix(i,j-1))/4; errormatrix(i,j)=abs(mymatrix(i,j)-tempval); end

end

end maxr=max(max(errormatrix)); itteration=itteration+1;

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) 10 20 30 50 100 BC type Corner Corner Corner Corner Corner Iterations 53 189 394 866 2267

Iteration VS Grid Size
2500 2257 2000

Iterations

1500

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

394