Solving Partial Differential Equations - MATLAB & Simulink
Solving Partial Differential Equations - MATLAB & Simulink
• ∂u
2
∂ u
Equations with a time derivative are parabolic. An example is the heat equation = .
2
∂t ∂x
• 2
∂ u
Equations without a time derivative are elliptic. An example is the Laplace equation = 0 .
2
∂x
pdepe requires at least one parabolic equation in the system. In other words, at least one equation in the system must
include a time derivative.
pdepe also solves certain 2-D and 3-D problems that reduce to 1-D problems due to angular symmetry (see the
argument description for the symmetry constant m for more information).
Partial Differential Equation Toolbox™ extends this functionality to generalized problems in 2-D and 3-D with Dirichlet
and Neumann boundary conditions.
The coupling of the partial derivatives with respect to time is restricted to multiplication by a diagonal matrix
( )
∂u
c x, t, u, . The diagonal elements of this matrix are either zero or positive. An element that is zero corresponds to
∂x
an elliptic equation, and any other element corresponds to a parabolic equation. There must be at least one parabolic
equation. An element of c that corresponds to a parabolic equation can vanish at isolated values of x if they are mesh
points (points where the solution is evaluated). Discontinuities in c and s due to material interfaces are permitted
provided that a mesh point is placed at each interface.
Solution Process
To solve PDEs with pdepe, you must define the equation coefficients for c, f, and s, the initial conditions, the behavior of
the solution at the boundaries, and a mesh of points to evaluate the solution on. The function call sol =
pdepe(m,pdefun,icfun,bcfun,xmesh,tspan) uses this information to calculate a solution on the specified mesh:
https://www.mathworks.com/help/matlab/math/partial-differential-equations.html 1/6
01.12.2020 Solving Partial Differential Equations - MATLAB & Simulink
Together, the xmesh and tspan vectors form a 2-D grid that pdepe evaluates the solution on.
Equations
You must express the PDEs in the standard form expected by pdepe. Written in this form, you can read off the values of
the coefficients c, f, and s.
In MATLAB you can code the equations with a function of the form
2
∂u ∂ u
In this case pdefun defines the equation = . If there are multiple equations, then c, f, and s are vectors with
2
∂t ∂x
Initial Conditions
At the initial time t = t , for all x, the solution components satisfy initial conditions of the form
0
(
u x, t0 ) = u ( x).
0
In MATLAB you can code the initial conditions with a function of the form
function u0 = icfun(x)
u0 = 1;
end
In this case u0 = 1 defines an initial condition of u (x,t ) = 1. If there are multiple equations, then u0 is a vector with
0 0
Boundary Conditions
At the boundary x = a or x = b, for all t, the solution components satisfy boundary conditions of the form
( )
∂u
(
p x, t, u ) + q( x, t ) f x, t, u, = 0.
∂x
q(x,t) is a diagonal matrix with elements that are either zero or never zero. Note that the boundary conditions are
expressed in terms of the flux f, rather than the partial derivative of u with respect to x. Also, of the two coefficients
p(x,t,u) and q(x,t), only p can depend on u.
In MATLAB you can code the boundary conditions with a function of the form
pL and qL are the coefficients for the left boundary, while pR and qR are the coefficients for the right boundary. In this
case bcfun defines the boundary conditions
(
uL xL, t )=0
(
u R x R, t )=1
https://www.mathworks.com/help/matlab/math/partial-differential-equations.html 2/6
01.12.2020 Solving Partial Differential Equations - MATLAB & Simulink
If there are multiple equations, then the outputs pL, qL, pR, and qR are vectors with each element defining the boundary
condition of one equation.
Integration Options
The default integration properties in the MATLAB PDE solver are selected to handle common problems. In some cases,
you can improve solver performance by overriding these default values. To do this, use odeset to create an options
structure. Then, pass the structure to pdepe as the last input argument:
sol = pdepe(m,pdefun,icfun,bcfun,xmesh,tspan,options)
Of the options for the underlying ODE solver ode15s, only those shown in the following table are available for pdepe.
The time mesh you specify is used purely for output purposes, and does not affect the internal time steps taken by the
solver. However, the spatial mesh you specify can affect the quality and speed of the solution. After solving an
equation, you can use pdeval to evaluate the solution structure returned by pdepe with a different spatial mesh.
(
u x, 0 )=T .
0
Also, the temperature is zero at the left boundary, and nonzero at the right boundary, so the boundary conditions are
(
u 0, t ) = 0,
(
u L, t ) = 1.
To solve this equation in MATLAB, you need to code the equation, initial conditions, and 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 in this example), 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 make sure that it is in the form that the pdepe solver expects:
( ) ( ( )) ( )
∂u ∂u −m ∂ m ∂u ∂u
c x, t, u, = x x f x, t, u, + s x, t, u, .
∂x ∂t ∂x ∂x ∂x
• m = 0
• c = 1
https://www.mathworks.com/help/matlab/math/partial-differential-equations.html 3/6
01.12.2020 Solving Partial Differential Equations - MATLAB & Simulink
• ∂u
f =
∂x
• s = 0
The value of m is passed as an argument to pdepe, while the other coefficients are encoded in a function for the
equation, which is
(Note: All functions are included as local functions at the end of the example.)
The initial condition function for the heat equation assigns a constant value for u0 . This function must accept an input
for x , even if it is unused.
function u0 = heatic(x)
u0 = 0.5;
end
The standard form for the boundary conditions expected by the pdepe solver is
( )
∂u
(
p x, t, u ) + q( x, t ) f x, t, u, = 0.
∂x
Written in this form, the boundary conditions for this problem are
(
u 0, t ) + (0 ⋅ f ) = 0,
(u( L, t ) − 1) + (0 ⋅ f ) = 0.
So the values for p and q are
• pL = uL, q L = 0.
• p R = u R − 1, qR = 0 .
Use a spatial mesh of 20 points and a time mesh of 30 points. Since the solution rapidly reaches a steady state, the
time points near t = 0 are more closely spaced together to capture this behavior in the output.
L = 1;
x = linspace(0,L,20);
t = [linspace(0,0.05,20), linspace(0.5,5,10)];
Solve Equation
Finally, solve the equation using the symmetry m , the PDE equation, the initial condition, the boundary conditions, and
the meshes for x and t .
m = 0;
https://www.mathworks.com/help/matlab/math/partial-differential-equations.html 4/6
01.12.2020 Solving Partial Differential Equations - MATLAB & Simulink
sol = pdepe(m,@heatpde,@heatic,@heatbc,x,t);
Plot Solution
colormap hot
imagesc(x,t,sol)
colorbar
xlabel('Distance x','interpreter','latex')
ylabel('Time t','interpreter','latex')
title('Heat Equation for $0 \le x \le 1$ and $0 \le t \le 5$','interpreter','latex')
Local Functions
odeexamples
https://www.mathworks.com/help/matlab/math/partial-differential-equations.html 5/6
01.12.2020 Solving Partial Differential Equations - MATLAB & Simulink
edit exampleFileName.m
exampleFileName
pdex1 Simple PDE that illustrates the formulation, computation, Solve Single PDE
and plotting of the solution.
pdex3 Problem that requires computing values of the partial Solve PDE and Compute Partial
derivative. Derivatives
pdex4 System of two PDEs whose solution has boundary layers at Solve System of PDEs
both ends of the interval and changes rapidly for small t.
pdex5 System of PDEs with step functions as initial conditions. Solve System of PDEs with Initial
Condition Step Functions
References
[1] Skeel, R. D. and M. Berzins, "A Method for the Spatial Discretization of Parabolic Equations in One Space Variable,"
SIAM Journal on Scientific and Statistical Computing, Vol. 11, 1990, pp. 1–32.
See Also
bvp4c | ode45 | odeset | pdepe | pdeval
Related Topics
• Solve Single PDE
• Solve System of PDEs
https://www.mathworks.com/help/matlab/math/partial-differential-equations.html 6/6