Main Content

Solve System of PDEs

This example shows how to formulate, compute, and plot the solution to a system of two partial differential equations.

Consider the system of PDEs

u1t=0.0242u1x2-F(u1-u2),

u2t=0.1702u2x2+F(u1-u2).

(The function F(y)=e5.73y-e-11.46y is used as a shorthand.)

The equation holds on the interval 0x1 for times t0. The initial conditions are

u1(x,0)=1,

u2(x,0)=0.

The boundary conditions are

xu1(0,t)=0,u2(0,t)=0,xu2(1,t)=0,u1(1,t)=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 the form that the pdepe solver expects:

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

In this form, the PDE coefficients are matrix-valued and the equation becomes

[1001]t[u1u2]=x[0.024u1x0.170u2x]+[-F(u1-u2)F(u1-u2)].

So the values of the coefficients in the equation are

m=0

c(x,t,u,ux)=[11] (diagonal values only)

f(x,t,u,ux)=[0.024u1x0.170u2x]

s(x,t,u,ux)=[-F(u1-u2)F(u1-u2)]

Now you can create a function to code the equation. The function should have the signature [c,f,s] = pdefun(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. It is a two-element vector where u(1) is u1(x,t) and u(2) is u2(x,t).

  • dudx is the partial spatial derivative u/x. It is a two-element vector where dudx(1) is u1/x and dudx(2) is u2/x.

  • The outputs c, f, and s correspond to coefficients in the standard PDE equation form expected by pdepe.

As a result, the equations in this example can be represented by the function:

function [c,f,s] = pdefun(x,t,u,dudx)
c = [1; 1];
f = [0.024; 0.17] .* dudx;
y = u(1) - u(2);
F = exp(5.73*y)-exp(-11.47*y);
s = [-F; F];
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 condition. The initial condition is applied at the first time value and provides the value of u(x,t0) for any value of x. The number of initial conditions must equal the number of equations, so for this problem there are two initial conditions. Use the function signature u0 = pdeic(x) to write the function.

The initial conditions are

u1(x,0)=1,

u2(x,0)=0.

The corresponding function is

function u0 = pdeic(x)
u0 = [1; 0];
end

Code Boundary Conditions

Now, write a function that evaluates the boundary conditions

xu1(0,t)=0,u2(0,t)=0,xu2(1,t)=0,u1(1,t)=1.

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.

Written in this form, the boundary conditions for the partial derivatives of u need to be expressed in terms of the flux f(x,t,u,ux). So the boundary conditions for this problem are

For x=0, the equation is

[0u2]+[10][0.024u1x0.170u2x]=0.

The coefficients are:

pL(x,t,u)=[0u2],

qL(x,t)=[10].

Likewise, for x=1 the equation is

[u1-10]+[01][0.024u1x0.170u2x]=0.

The coefficients are:

pR(x,t,u)=[u1-10],

qR(x,t)=[01].

The boundary function should use the function signature [pl,ql,pr,qr] = pdebc(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] = pdebc(xl,ul,xr,ur,t)
pl = [0; ul(2)];
ql = [1; 0];
pr = [ur(1)-1; 0];
qr = [0; 1];
end

Select Solution Mesh

The solution to this problem changes rapidly when t is small. Although pdepe selects a time step that is appropriate to resolve the sharp changes, to see the behavior in the output plots you need to select appropriate output times. For the spatial mesh, there are boundary layers in the solution at both ends of 0x1, so you need to specify mesh points there to resolve the sharp changes.

x = [0 0.005 0.01 0.05 0.1 0.2 0.5 0.7 0.9 0.95 0.99 0.995 1];
t = [0 0.005 0.01 0.05 0.1 0.5 1 1.5 2];

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,@pdefun,@pdeic,@pdebc,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). Extract each solution component into a separate variable.

u1 = sol(:,:,1);
u2 = sol(:,:,2);

Plot Solution

Create surface plots of the solutions for u1 and u2 plotted at the selected mesh points for x and t.

surf(x,t,u1)
title('u_1(x,t)')
xlabel('Distance x')
ylabel('Time t')

surf(x,t,u2)
title('u_2(x,t)')
xlabel('Distance x')
ylabel('Time t')

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] = pdefun(x,t,u,dudx) % Equation to solve
c = [1; 1];
f = [0.024; 0.17] .* dudx;
y = u(1) - u(2);
F = exp(5.73*y)-exp(-11.47*y);
s = [-F; F];
end
% ---------------------------------------------
function u0 = pdeic(x) % Initial Conditions
u0 = [1; 0];
end
% ---------------------------------------------
function [pl,ql,pr,qr] = pdebc(xl,ul,xr,ur,t) % Boundary Conditions
pl = [0; ul(2)];
ql = [1; 0];
pr = [ur(1)-1; 0];
qr = [0; 1];
end
% ---------------------------------------------

See Also

Related Topics