Main Content

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 x=0.5, and the initial condition has a discontinuity at the right boundary x=1.

Consider the piecewise PDE

{ut=x-2x(x25ux)-1000eu(0x0.5)ut=x-2x(x2ux)-eu(0.5x1)

The initial conditions are

u(x,0)=0  (0x<1),u(1,0)=1  (x=1).

The boundary conditions are

       ux=0  (x=0),u(1,t)=1  (x=1).

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

c(x,t,u,ux)ut=x-mx(xmf(x,t,u,ux))+s(x,t,u,ux).

In this case, the PDE is in the proper form, so you can read off the values of the coefficients.

{ut=x-2x(x25ux)-1000eu(0x0.5)ut=x-2x(x2ux)-eu(0.5x1)

The values for the flux term f(x,t,u,ux) and source term s(x.t,u,ux) change depending on the value of x. The coefficients are:

m=2

c(x,t,u,ux)=1

{f(x,t,u,ux)=5ux(0x0.5)f(x,t,u,ux)=ux(0.5x1)

{s(x,t,u,ux)=-1000eu(0x0.5)s(x,t,u,ux)=-eu(0.5x1)

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 to x and t.

  • dudx is the partial spatial derivative u/x.

  • The outputs c, f, and s correspond to coefficients in the standard PDE equation form expected by pdepe. These coefficients are coded in terms of the input variables x, t, u, and dudx.

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 u(x,t0) for any value of x. Use the function signature u0 = pdex2ic(x) to write the function.

The initial conditions are

u(x,0)=0        (0x<1),u(1,0)=1        (x=1).

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 axb, the boundary conditions apply for all t and either x=a or x=b. The standard form for the boundary conditions expected by the solver is

p(x,t,u)+q(x,t)f(x,t,u,ux)=0.

Since this example has spherical symmetry (m=2), 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 pL=qL=0. For the right boundary condition, you can rewrite the boundary condition in the standard form and read off the coefficient values for pR and qR.

For x=1, the equation is u(1,t)=1(u-1)+0ux=0. The coefficients are:

  • pR(1,t,u)=u-1

  • qR(1,t)=0

The boundary function should use the function signature [pl,ql,pr,qr] = pdex2bc(xl,ul,xr,ur,t):

  • The inputs xl and ul correspond to u and x for the left boundary.

  • The inputs xr and ur correspond to u and x for the right boundary.

  • t is the independent time variable.

  • The outputs pl and ql correspond to pL(x,t,u) and qL(x,t) for the left boundary (x=0 for this problem).

  • The outputs pr and qr correspond to pR(x,t,u) and qR(x,t) for the right boundary (x=1 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 x=0.5 to account for the discontinuous interface, as well as points near x=1 because of the inconsistent initial value (u(1,0)=1) and boundary value (u(1,t)=0) at that point. The solution changes rapidly for small t, 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 kth component of the solution uk 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 kth 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 u plotted at the selected mesh points for x and t. Since m=2 the problem is posed in a spherical geometry with spherical symmetry, so the solution only changes in the radial x direction.

surf(x,t,u)
title('Numerical solution with nonuniform mesh')
xlabel('Distance x')
ylabel('Time t')
zlabel('Solution u')

Figure contains an axes object. The axes object with title Numerical solution with nonuniform mesh contains an object of type surface.

Now, plot just x and u to get a side view of the contours in the surface plot. Add a line at x=0.5 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')

Figure contains an axes object. The axes object with title Solution profiles at several times contains 17 objects of type line.

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
%----------------------------------------------

See Also

Related Topics