Solve PDE with Discontinuity
This example shows how to solve a PDE that interfaces with a material. The material interface creates a discontinuity in the problem at , and the initial condition has a discontinuity at the right boundary .
Consider the piecewise PDE
The initial conditions are
The boundary conditions are
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 make sure that it is in a form that the pdepe
solver expects. The standard form that pdepe
expects is
.
In this case, the PDE is in the proper form, so you can read off the values of the coefficients.
The values for the flux term and source term change depending on the value of . The coefficients are:
Now you can create a function to code the equation. The function should have the signature [c,f,s] = pdex2pde(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] = pdex2pde(x,t,u,dudx) c = 1; if x <= 0.5 f = 5*dudx; s = -1000*exp(u); else f = dudx; s = -exp(u); end end
(Note: All functions are included as local functions at the end of the example.)
Code Initial Conditions
Next, write a function that returns the initial conditions. The initial condition is applied at the first time value and provides the value of for any value of . Use the function signature u0 = pdex2ic(x)
to write the function.
The initial conditions are
The corresponding function is
function u0 = pdex2ic(x) if x < 1 u0 = 0; else u0 = 1; end 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 t and either or . The standard form for the boundary conditions expected by the solver is
Since this example has spherical symmetry (), the pdepe
solver automatically enforces the left boundary condition to bound the solution at the origin, and ignores any conditions specified for the left boundary in the boundary function. So for the left boundary condition, you can specify . For the right boundary condition, you can rewrite the boundary condition in the standard form and read off the coefficient values for and .
For , the equation is . The coefficients are:
The boundary function should use the function signature [pl,ql,pr,qr] = pdex2bc(xl,ul,xr,ur,t)
:
The inputs
xl
andul
correspond to u and x for the left boundary.The inputs
xr
andur
correspond to u and x 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] = pdex2bc(xl,ul,xr,ur,t) pl = 0; ql = 0; pr = ur - 1; qr = 0; end
Select Solution Mesh
The spatial mesh should include several values near to account for the discontinuous interface, as well as points near because of the inconsistent initial value () and boundary value () at that point. The solution changes rapidly for small , so use a time step that can resolve this sharp change.
x = [0 0.1 0.2 0.3 0.4 0.45 0.475 0.5 0.525 0.55 0.6 0.7 0.8 0.9 0.95 0.975 0.99 1]; t = [0 0.001 0.005 0.01 0.05 0.1 0.5 1];
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 = 2; sol = pdepe(m,@pdex2pde,@pdex2ic,@pdex2bc,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 8-by-18 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 plotted at the selected mesh points for and . Since the problem is posed in a spherical geometry with spherical symmetry, so the solution only changes in the radial direction.
surf(x,t,u) title('Numerical solution with nonuniform mesh') xlabel('Distance x') ylabel('Time t') zlabel('Solution u')
Now, plot just and to get a side view of the contours in the surface plot. Add a line at to highlight the effect of the material interface.
plot(x,u,x,u,'*') line([0.5 0.5], [-3 1], 'Color', 'k') xlabel('Distance x') ylabel('Solution u') title('Solution profiles at several times')
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] = pdex2pde(x,t,u,dudx) % Equation to solve c = 1; if x <= 0.5 f = 5*dudx; s = -1000*exp(u); else f = dudx; s = -exp(u); end end %---------------------------------------------- function u0 = pdex2ic(x) %Initial conditions if x < 1 u0 = 0; else u0 = 1; end end %---------------------------------------------- function [pl,ql,pr,qr] = pdex2bc(xl,ul,xr,ur,t) % Boundary conditions pl = 0; ql = 0; pr = ur - 1; qr = 0; end %----------------------------------------------