0% found this document useful (0 votes)
10 views2 pages

EM3 Cex 5

The document describes a computational exercise for solving Laplace's equation in a square domain using finite difference methods in MATLAB. It provides a detailed explanation of the M-file 'laplacefd.m' containing functions to compute the solution and visualize the potential. The exercise includes instructions for setting boundary conditions and generating plots for the solution and the coefficient matrix's sparsity structure.

Uploaded by

Mado Saeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

EM3 Cex 5

The document describes a computational exercise for solving Laplace's equation in a square domain using finite difference methods in MATLAB. It provides a detailed explanation of the M-file 'laplacefd.m' containing functions to compute the solution and visualize the potential. The exercise includes instructions for setting boundary conditions and generating plots for the solution and the coefficient matrix's sparsity structure.

Uploaded by

Mado Saeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Engineering Mathematics | CHEN30101 Computational Exercise V

The BVP associated with Laplace’s equation in a square domain with given
boundary data gL , gR , gB , gT is

uxx + uyy = 0 in (0, ℓ) × (0, ℓ)


u(0, y) = gL (y), u(ℓ, y) = gR (y), y ∈ (0, ℓ)
u(x, 0) = gB (x), u(x, ℓ) = gT (x), x ∈ (0, ℓ).

A finite difference approximation to the solution with given boundary data g


can be computed by constructing the following M-file laplacefd.m containing
two MATLAB functions (all in the same file).

function [u,A] = laplacefd(xx,top,bottom,yy,left,right)


% Finite difference solution of Laplace’s equation in a square
% input parameters
% xx grid points for x coords (row vector)
% top potential on top edge (row vector)
% bottom potential on bottom edge (row vector)
% yy grid points for y coords (column vector)
% left potential on left edge (column vector)
% right potential on right edge (column vector)
% output parameters
% u solution (matrix of all grid point values)
% A coefficient matrix
n=length(xx)-1;
nn=(n-1)*(n-1); h=xx(end)/n;
fprintf(’subdivision parameter : %g\n’,h)
% set up interior numbering
nint=reshape(1:nn,n-1,n-1);
zerorow=zeros(1,n+1); zerocol=zeros(n-1,1);
G=[zerorow; [zerocol,nint,zerocol]; zerorow];
% set up finite difference matrix using matlab function delsq
A=delsq(G);
% set up the RHS vector
b=zeros(nn,1);
b(1:n-1)=b(1:n-1)+left(2:n);
b(nn-n+2:nn)= b(nn-n+2:nn)+right(2:n);
b(1:n-1:nn)= b(1:n-1:nn)+top(2:n)’;
b(n-1:n-1:nn)=b(n-1:n-1:nn)+bottom(2:n)’;
bint=reshape(b,n-1,n-1);
% solve using matlab sparse solver
x=A\b;
xint=reshape(x,n-1,n-1);
% add boundary values and plot the solution
u=[top; [left(2:n),xint,right(2:n)]; bottom];
fdsolplot(xx,u)
function fdsolplot(xx,u)
[X,Y]=meshgrid(xx,xx);
subplot(121)
mesh(X,Y,flipud(u)), axis(’square’)
title(’potential solution and grid’)
subplot(122)
contour(X,Y, flipud(u),20), axis(’square’)
title(’potential solution contours’)
return

To compute a 5–point finite difference solution for a problem with the potential
at the top edge set to unity and the three edges set to zero, simply type

>> h=1/16; xx=0:h:1; yy=[1:-h:0]’;


>> [u,A] = laplacefd(xx,ones(size(xx)),0*xx,yy,0*yy,0*yy);
>> figure, spy(A), title(’matrix nonzero structure’)

You should generate the solution plot shown below and a sparsity plot of the
225 × 225 coefficient matrix. You might also like to try to compute a solution
to a different problem; for example with boundary data gL = y, gR = 1 + 2y,
gB = x, and gT = 1 + 2x.

potential solution and grid


potential solution contours
1
0.9
1
0.8
0.8
0.7
0.6 0.6

0.4 0.5
0.4
0.2
0.3
0
1 0.2
1 0.1
0.5
0.5 0
0 0.2 0.4 0.6 0.8 1
0 0

You might also like