In this section, we discuss some algorithms to solve numerically boundary value porblems for Laplace's equation (∇2u = 0), Poisson's equation (∇2u = g(x,y)), and Helmholtz's equation (∇2u + k(x,y) u = g(x,y)). We start with the Dirichlet problem in a rectangle R=[0,a]×[0,b].
Actually, matlab has a special Partial Differential Equation Toolbox to solve some partial differential equations using finite element analysis including the heat transfer equations and Laplace's equations under various boundary conditions. The explanation how to use it is provided on Mathworks site. We demonstrait its applications in some examples below.
The Laplacian operator must be expressed in a discrete form suitable for numerical computations. The formula for approximating f″(x) is obtained from
f″(x)=f(x+h)−2f(x)+f(x−h)h2+O(h2).
When this formula is applied to the Laplace equation involving approximation of partial derivatives uxx and uyy and the results are added, we obtain
Suppose that we seek a solution of the Laplace equation in the rectangular domain [0,a]×[0,b]. Assume that the rectangle is divided into (n-1)×(m-1) squares with side h so that a = nh and b = mh because we assume also that b/a = m/n.
which has order of accuracy O(h2) at all interior grid points (x,y)=(xi,yj) for i = 2, 3, …, n-1 and j = 2, 3, …, m-1. The grid points are uniformly spaced: xi+1=xi+h,xi−1=xi−h,yj+1=yj+h,yj−1=yj−h. Using notation ui,j for u(xi,yj), the discrete Laplace equation can be written as
Δui,j≈ui+1,j+ui−1,j+ui,j+1+ui,j−1−4ui,jh2=0,
which is known as the five-point difference formula for Laplace's equation. This formula is usually called the five-point stencil of a point in the grid is a stencil made up of the point itself together with its four "neighbors". It is used to write finite difference approximations to derivatives at grid points. This formula relates the function value ui+1,j≡u(xi+1,yj) to its four neighboring values ui+1,j, ui-1,j, ui,j+1, and ui,j-1, as shown in stencil below. The multiple h2 can be eliminated and we obtain the Laplacian computational formula:
Assume that the values u(x,y) are known at the following boundary grid points:
u(x1,yj)=u1,jfor 2≤j≤m−1(on the left),u(xi,y1)=ui,1for 2≤i≤n−1(on the bottom),u(xn,yj)=u1,jfor 2≤j≤m−1(on the right),u(xi,ym)=ui,1for 2≤i≤n−1(on the top).
Then applying the Laplace computational formula at each of the interior points of R will creat a linear system of (n-2) equations in (n-2) unknowns, which is solved to obtain approximations to u(x,y) at the interior points of R.
Program (Dirichlet method for Laplace's equation): to approximate the solution of uxx+uyy=0 over R={(x,y):0≤x≤a,0≤y≤b with u(x,0)=f0(x),u(x,b)=fb(x), for 0 ≤ x ≤ a;, and u(0,y)=g0(y),u(a,y)=ga(y), for 0 ≤ y ≤ b. It is assumed that Δx = Δy = h and that integers n and m exist so that a=nh and b=mh.
function U=dirichlet(f0,fb,g0,ga,a,b,h,tol,max1)
% Input -- f0,fb,g0,ga are boundary functions input as a strings
% -- a and b right end points of [0,a] and [0,b]
% -- h step size
% -- tol is the tolerance
% -- max1
% Output -- U solution matrix;
% Initialize parameters and U
n=fix(a/h)+1;
m=fix(b/h)+1;
ave=(a*(feval(f0,0)+feval(fb,0))+b*(feval(g0,0)+feval(ga,0)))/(2*a+2*b);
U=ave*ones(n,m);
% Boundary conditions
U(1,1:m)=feval(g0,0:h:(m-1)*h))';
U(n,1:m)=feval(ga,0:h:(m-1)*h))';
U(1:n,1)=feval(fa,0:h:(n-1)*h))';
U(1:n,m)=feval(fb,0:h:(n-1)*h))';
U(1,1)=(U(1,2)+U(2,1))/2;
U(1,m)=(U(i,m-1)+U(2,m))/2;
U(n,1)=(U(n-1,1)+U(n,2))/2;
U(n,m)=(U(n-1,m)+U(n,m-1))/2;
% SQR parameter
w=4/(2+sqrt(4-(cos(pi/(n-1))+cos(pi/(m-1)))^2));
% Refine approximations and sweep operator throughout the grid
err=1;
cnt=0;
while((err>tol)&(cnt<=max1))
err=0;
for j=2:m-1
for i=2:n-1
relx=w*(U(i,j+1)+U(i,j-1)+U(i+1,j)+U(i-1,j)-4*U(i,j))/4;
U(i,j)=U(i,j)+relx;
if (err<=abs(relx))
err=abs(relx);
end
end
end
cnt=cnt+1;
end
U=flipud(U');
For example, suppose that the region is a rectangle with n=6 and m=5, and that the unknown values of u(xi,yj) at the twelve interior grid points are labeled p1, p2, ... ,p12 and positioned in the grid as shown in the figure above. The Laplacian computational formula is applied at each of the interior grid points, and the result is the system A p = b of twelve linear equations:
−4p1+p2+p4=−u2,1−u1,2p1−4p2+p3+p5=−u3,1,
Example 1:
Consider the Laplace equation in a square subject to the mixd boundary conditions
Laplace equation:uxx+uyy=0for −1<x<1 and −1<y<1,Dirichlet boundary conditions on x:u(−1,y)=50,andu(1,y)=40,Neumann boundary condition on y:uy(x,0)=1,anduy(x,1)=−1,Corner regularity condition:function u(x,y)is bounded at every corner point.
To solve this problem numerically, we use matlab special tool box for solving PDEs with constant boundaey conditions.
Using matlab toolbox for solving the given boundary value problem with constant mixed boundary conditions:
model = createpde;
geometryFromEdges(model,@squareg); % takes the geometry of a square
pdegplot(model,'EdgeLabels','on')
xlim([-1.1,1.1]) % limits to x values beyond where our object lies
axis equal
applyBoundaryCondition(model,'dirichlet','edge',3,'u',40); % edge 3 has a dirichlet value of 40
applyBoundaryCondition(model,'dirichlet','edge',1, 'u', 50);% edge 1 has a dirichlet value of 50
applyBoundaryCondition(model,'neumann','edge',[2,4], 'g', -1); % neumann conditions on edge 2 and 4 value of -1
specifyCoefficients(model,'m',0,'d',0,'c',1,'a',0,'f',10);% see attached document, these are the same conditions used in the mathWorks example
generateMesh(model,'HMax', 0.1); % affects the smoothness of the plot
results = solvepde(model);
u = results.NodalSolution;
pdeplot(model,'XYData', u, 'ZData', u)
Solution of Laplace's equation.
matlab code.
■
For non-constant boundary conditions, matlab has a special toolbox.
To solve problems using PDEModel 0bjects, please follow
web page that gives the general explanation of PDESolver.
The cor4esponing mesh and geometry explanation is given on the
web.