Solve Single PDE
This example shows how to formulate, compute, and plot the solution to a single PDE.
Consider the partial differential equation
The equation is defined on the interval for times . At , the solution satisfies the initial condition
Also, at and , the solution satisfies the boundary conditions
To solve this equation in MATLAB®, you need to code the equation, the initial conditions, and the boundary conditions, then select a suitable solution mesh before calling the solver pdepe
. You either can include the required functions as local functions at the end of a file (as done here), or save them as separate, named files in a directory on the MATLAB path.
Code Equation
Before you can code the equation, you need to rewrite it in a form that the pdepe
solver expects. The standard form that pdepe
expects is
.
Written in this form, the PDE becomes
.
With the equation in the proper form you can read off the relevant terms:
Now you can create a function to code the equation. The function should have the signature [c,f,s] = pdex1pde(x,t,u,dudx)
:
x
is the independent spatial variable.t
is the independent time variable.u
is the dependent variable being differentiated with respect tox
andt
.dudx
is the partial spatial derivative .The outputs
c
,f
, ands
correspond to coefficients in the standard PDE equation form expected bypdepe
. These coefficients are coded in terms of the input variablesx
,t
,u
, anddudx
.
As a result, the equation in this example can be represented by the function:
function [c,f,s] = pdex1pde(x,t,u,dudx) c = pi^2; f = dudx; s = 0; end
(Note: All functions are included as local functions at the end of the example.)
Code Initial Condition
Next, write a function that returns the initial condition. The initial condition is applied at the first time value tspan(1)
. The function should have the signature u0 = pdex1ic(x)
.
The corresponding function is
function u0 = pdex1ic(x) u0 = sin(pi*x); end
Code Boundary Conditions
Now, write a function that evaluates the boundary conditions. For problems posed on the interval , the boundary conditions apply for all and either or . The standard form for the boundary conditions expected by the solver is
Rewrite the boundary conditions in this standard form and read off the coefficient values.
For , the equation is
The coefficients are:
For , the equation is
The coefficients are:
Since the boundary condition function is expressed in terms of , and this term is already defined in the main PDE function, you do not need to specify this piece of the equation in the boundary condition function. You need only specify the values of and at each boundary.
The boundary function should use the function signature [pl,ql,pr,qr] = pdex1bc(xl,ul,xr,ur,t)
:
The inputs
xl
andul
correspond to and for the left boundary.The inputs
xr
andur
correspond to and for the right boundary.t
is the independent time variable.The outputs
pl
andql
correspond to and for the left boundary ( for this problem).The outputs
pr
andqr
correspond to and for the right boundary ( for this problem).
The boundary conditions in this example are represented by the function:
function [pl,ql,pr,qr] = pdex1bc(xl,ul,xr,ur,t) pl = ul; ql = 0; pr = pi * exp(-t); qr = 1; end
Select Solution Mesh
Before solving the equation you need to specify the mesh points at which you want pdepe
to evaluate the solution. Specify the points as vectors t
and x
. The vectors t
and x
play different roles in the solver. In particular, the cost and accuracy of the solution depend strongly on the length of the vector x
. However, the computation is much less sensitive to the values in the vector t
.
For this problem, use a mesh with 20 equally spaced points in the spatial interval [0,1] and five values of t
from the time interval [0,2].
x = linspace(0,1,20); t = linspace(0,2,5);
Solve Equation
Finally, solve the equation using the symmetry m
, the PDE equation, the initial conditions, the boundary conditions, and the meshes for x
and t
.
m = 0; sol = pdepe(m,@pdex1pde,@pdex1ic,@pdex1bc,x,t);
pdepe
returns the solution in a 3-D array sol
, where sol(i,j,k)
approximates the k
th component of the solution evaluated at t(i)
and x(j)
. The size of sol
is length(t)
-by-length(x)
-by-length(u0)
, since u0
specifies an initial condition for each solution component. For this problem, u
has only one component, so sol
is a 5-by-20 matrix, but in general you can extract the k
th solution component with the command u = sol(:,:,k)
.
Extract the first solution component from sol
.
u = sol(:,:,1);
Plot Solution
Create a surface plot of the solution.
surf(x,t,u) title('Numerical solution computed with 20 mesh points') xlabel('Distance x') ylabel('Time t')
The initial condition and boundary conditions of this problem were chosen so that there would be an analytical solution, given by
Plot the analytical solution with the same mesh points.
surf(x,t,exp(-t)'*sin(pi*x)) title('True solution plotted with 20 mesh points') xlabel('Distance x') ylabel('Time t')
Now, compare the numerical and analytical solutions at , the final value of . In this example .
plot(x,u(end,:),'o',x,exp(-t(end))*sin(pi*x)) title('Solution at t = 2') legend('Numerical, 20 mesh points','Analytical','Location','South') xlabel('Distance x') ylabel('u(x,2)')
Local Functions
Listed here are the local helper functions that the PDE solver pdepe calls to calculate the solution. Alternatively, you can save these functions as their own files in a directory on the MATLAB path.
function [c,f,s] = pdex1pde(x,t,u,dudx) % Equation to solve c = pi^2; f = dudx; s = 0; end %---------------------------------------------- function u0 = pdex1ic(x) % Initial conditions u0 = sin(pi*x); end %---------------------------------------------- function [pl,ql,pr,qr] = pdex1bc(xl,ul,xr,ur,t) % Boundary conditions pl = ul; ql = 0; pr = pi * exp(-t); qr = 1; end %----------------------------------------------