Matlab Program For Second Order FD Solution To Poisson's Equation

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Matlab Program for Second Order FD

Solution to Poisson’s Equation


Code:
0001 % Numerical approximation to Poisson’s equation over the square [a,b]x[a,b] with
0002 % Dirichlet boundary conditions. Uses a uniform mesh with (n+2)x(n+2) total
0003 % points (i.e, n interior grid points).
0004 % Input:
0005 % ffun : the RHS of poisson equation (i.e. the Laplacian of u).
0006 % gfun : the boundary function representing the Dirichlet B.C.
0007 % a,b : the interval defining the square
0008 % m : m+2 is the number of points in either direction of the mesh.
0009 % Ouput:
0010 % u : the numerical solution of Poisson equation at the mesh points.
0011 % x,y : the uniform mesh.
0012 %
0013 function [u,x,y] = fd2poisson(ffun,gfun,a,b,m)
0014
0015 h = (b-a)/(m+1); % Mesh spacing
0016
0017 [x,y] = meshgrid(a:h:b); % Uniform mesh, including boundary points.
0018
0019 idx = 2:m+1;
0020 idy = 2:m+1;
0021
0022 % Compute boundary terms, south, north, east, west
0023 ubs = feval(gfun,x(1,1:m+2),y(1,1:m+2)); % Include corners
0024 ubn = feval(gfun,x(m+2,1:m+2),y(m+2,1:m+2)); % Include corners
0025 ube = feval(gfun,x(idy,m+2),y(idy,m+2)); % No corners
0026 ubw = feval(gfun,x(idy,1),y(idy,1)); % No corners
0027
0028 % Evaluate the RHS of Poisson’s equation at the interior points.
0029 f = feval(ffun,x(idy,idx),y(idy,idx));
0030
0031 % Adjust f for boundary terms
0032 f(:,1) = f(:,1) - ubw/h^2; % West
0033 f(:,m) = f(:,m) - ube/h^2; % East
0034 f(1,1:m) = f(1,1:m) - ubs(idx)/h^2; % South
0035 f(m,1:m) = f(m,1:m) - ubn(idx)/h^2; % North
0036
0037 f = reshape(f,m*m,1);
0038
0039 % Create the D2x and D2y matrices
0040
0041 % Full matrix version. Can be made faster with Matlab’s sparse library.
0042 z = [-2;1;zeros(m-2,1)];
0043 D2x = 1/h^2*kron(toeplitz(z,z),eye(m));
0044 D2y = 1/h^2*kron(eye(m),toeplitz(z,z));
0045
0046 % Solve the system
0047 u = (D2x + D2y)\f;
0048
0049 % Convert u from a column vector to a matrix to make it easier to work with
0050 % for plotting.
0051 u = reshape(u,m,m);
0052
0053 % Append on to u the boundary values from the Dirichlet condition.
0054 u = [ubs;[ubw,u,ube];ubn];
0055
0056 end
Example:
Use the above Matlab code to solve the Poisson problem

∇2 u(x, y) = −5π 2 sin(πx) cos(2πy) for (x, y) ∈ Ω = (0, 1) × (0, 1)


u(x, y) = sin(πx) cos(2πy) for (x, y) ∈ ∂Ω

0001 % Script for testing fd2poisson over the square [a,b]x[a,b]


0002 a = 0;
0003 b = 1;
0004 k = 6;
0005 m = 2^k-1; % Number of interior grid points in one direction
0006
0007 f = @(x,y) -5*pi^2*sin(pi*x).*cos(2*pi*y); % Laplacian(u) = f
0008 g = @(x,y) sin(pi*x).*cos(2*pi*y); % u = g on Boundary
0009 uexact = @(x,y) g(x,y); % Exact solution is g.
0010
0011 % Compute and time the solution
0012 tic
0013 [u,x,y] = fd2poisson(f,g,a,b,m);
0014 gedirect = toc;
0015
0016 fprintf(’Direct Gaussian elimination take %d s\n’,gedirect);
0017
0018 %% Plot solution
0019 figure, set(gcf,’DefaultAxesFontSize’,10,’PaperPosition’, [0 0 3.5 3.5]),
0020 surf(x,y,u), xlabel(’x’), ylabel(’y’), zlabel(’u(x,y)’),
0021 title(strcat(’Numerical Solution to Poisson Equation, h=’,num2str(h)));
0022
0023 % Plot error
0024 figure, set(gcf,’DefaultAxesFontSize’,10,’PaperPosition’, [0 0 3.5 3.5]),
0025 surf(x,y,u-uexact(x,y)),xlabel(’x’),ylabel(’y’), zlabel(’Error’),
0026 title(strcat(’Error, h=’,num2str(h)));

Numerical Solution to Poisson Equation, h=0.025

0.5

0
u(x,y)

−0.5

−1

−1.5
1
1
0.5
0.5

y 0 0 x

2
Error, h=0.2 Error, h=0.1

0 0.01

0
−0.05

−0.01
u(x,y)

u(x,y)
−0.1
−0.02

−0.15
−0.03

−0.2 −0.04
1 1
1 1
0.5 0.5
0.5 0.5

y 0 0 x y 0 0 x
Error, h=0.05 Error, h=0.025

−3 −3
x 10 x 10

2 0.5

0 0

−2 −0.5
u(x,y)

Error

−4 −1

−6 −1.5

−8 −2

−10 −2.5
1 1
1 1
0.5 0.5
0.5 0.5

y 0 0 x y 0 0 x

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy