In general Poissons equation refers to a class of partial di
Solution
Entire question is about how do you solve Poisson\'s Equation in MATLAB.
Syntax
u = poisolv(b,p,e,t,f)
Description
u = poisolv(b,p,e,t,f) solves Poisson\'s equation with Dirichlet boundary conditions on a regular rectangular grid. A combination of sine transforms and tridiagonal solutions is used for increased performance.
The boundary conditions b must specify Dirichlet conditions for all boundary points.
The mesh p, e, and t must be a regular rectangular grid. For details on the mesh data representation, see initmesh.f gives the right-hand side of Poisson\'s equation.Apart from roundoff errors, the result should be the same as u = assempde(b,p,e,t,1,0,f).
Description
u = poicalc(f,h1,h2,n1,n2) calculates the solution of Poisson\'s equation for the interior points of an evenly spaced rectangular grid. The columns of u contain the solutions corresponding to the columns of the right-hand side f. h1 and h2 are the spacings in the first and second direction, and n1 and n2 are the number of points.
The number of rows in f must be n1*n2. If n1 and n2 are not given, the square root of the number of rows of f is assumed. If h1 and h2 are not given, they are assumed to be equal.
The ordering of the rows in u and f is the canonical ordering of interior points, as returned by poiindex.
The solution is obtained by sine transforms in the first direction and tridiagonal matrix solution in the second direction. n1 should be 1 less than a power of 2 for best performance.
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 x n interior grid points).
0004 % Input:
0005 % pfunc : the RHS of poisson equation (i.e. the Laplacian of u).
0006 % bfunc : the boundary function representing the Dirichlet B.C.
0007 % a,b : the interval defining the square
0008 % n : n+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(pfunc,bfunc,a,b,n)
0014
0015 h = (b-a)/(n+1); % Mesh spacing
0016
0017 [x,y] = meshgrid(a:h:b); % Uniform mesh, including boundary points.
0018
0019 % Compute u on the boundary from the Dirichlet boundary condition
0020 ub = zeros(n,n);
0021 idx = 2:n+1;
0022 idy = 2:n+1;
0023 % West and East boundaries need special attention
0024 ub(:,1) = feval(bfunc,x(idx,1),y(idy,1)); % West Boundary
0025 ub(:,n) = feval(bfunc,x(idx,n+2),y(idy,n+2)); % East Boundary
0026 % Now the North and South boundaries
0027 ub(1,1:n) = ub(1,1:n) + feval(bfunc,x(1,idx),y(1,idy));
0028 ub(n,1:n) = ub(n,1:n) + feval(bfunc,x(n+2,idx),y(n+2,idy));
0029 % Convert ub to a vector using column reordering
0030 ub = (1/h^2)*reshape(ub,n*n,1);
0031
0032 % Evaluate the RHS of Poisson’s equation at the interior points.
0033 f = feval(pfunc,x(idx,idy),y(idx,idy));
0034 % Convert f to a vector using column reordering
0035 f = reshape(f,n*n,1);
0036
0037 % Create the D2x and D2y matrices
0038 z = [-2;1;zeros(n-2,1)];
0039 D2x = 1/h^2*kron(toeplitz(z,z),eye(n));
0040 D2y = 1/h^2*kron(eye(n),toeplitz(z,z));
0041
0042 % Solve the system
0043 u = (D2x + D2y)\\(f-ub);
0044 % Convert u from a column vector to a matrix to make it easier to work with
0045 % for plotting.
0046 u = reshape(u,n,n);
0047
0048 % Append on to u the boundary values from the Dirichlet condition.
0049 u = [[feval(bfunc,x(1,1:n+2),y(1,1:n+2))];...
0050 [[feval(bfunc,x(2:n+1,1),y(2:n+1,1))] u ...
0051 [feval(bfunc,x(2:n+1,n+2),y(2:n+1,n+2))]];...
0052 [feval(bfunc,x(n+2,1:n+2),y(n+2,1:n+2))]];

