0% found this document useful (0 votes)
30 views9 pages

Solve PDEs Package Pdepe-1-2

Pde solver for matlab

Uploaded by

ahnaf rafid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views9 pages

Solve PDEs Package Pdepe-1-2

Pde solver for matlab

Uploaded by

ahnaf rafid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Partial Differential Equations solver package pdepe

Single or System of PDE in MATLAB


Dr. Md. Kamrujjaman
Associate Professor
Department of Mathematics
University of Dhaka, Dhaka, Bangladesh
Email: [email protected]

Section-1: PDE in One Space Dimension


For initial boundary value partial differential equations with time t and a single spatial variable x,
MATLAB has a built-in solver pdepe.

Reference: Partial Differential Equations in MATLAB by P. Howard

Section-2: Single initial-boundary value problems


Example 1. Suppose, for example, that we would like to solve the heat equation

MATLAB specifies such parabolic PDE in the form

with boundary conditions


where xl represents the left endpoint of the boundary and xr represents the right endpoint of the
boundary, and initial condition
u(0, x) = f(x).
Note: Observe that the same function b appears in both the equation and the boundary
conditions.
Typically, for clarity, each set of functions will be specified in a separate M-file.
 That is, the functions c, b, and s associated with the equation should be specified in one
M-file,
 the functions p and q associated with the boundary conditions in a second M-file (again,
keep in mind that b is the same and only needs to be specified once), and
 finally the initial function f(x) in a third.
 The command pdepe will combine these M-files and return a solution to the problem.
In our example, we have

which we specify in the function M-file eqn1.m. (The specification m = 0 will be made later.)

function [c,b,s] = eqn1(x,t,u,DuDx)


%EQN1: MATLAB function M-file that specifies a PDE in time and one space dimension.
c = 1;
b = DuDx;
s = 0;
For our boundary conditions, we have
p(0, t, u) =u; q(0, t) = 0 [BC: u-0=0]
p(1, t, u) =u−1; q(1, t) = 0; [BC: u-1=0]
which we specify in the function M-file bc1.m.

function [pl,ql,pr,qr] = bc1(xl,ul,xr,ur,t)


%BC1: MATLAB function M-file that specifies boundary conditions
%for a PDE in time and one space dimension.
pl = ul;
ql = 0;
pr = ur-1;
qr = 0;

For our initial condition, we have

which we specify in the function M-file initial1.m.

function value = initial1(x)


%INITIAL1: MATLAB function M-file that specifies the initial condition
%for a PDE in time and one space dimension.
value = 2*x/(1+x^2);
We are finally ready to solve the PDE with pdepe. In the following script M-file, we choose a
grid of x and t values, solve the PDE and create a surface plot of its solution (see in Figure 1.1).

%pde1.m: script M-file that solves and plots sols to the PDE stored in eqn1.m
Clc; close all; clear all
m = 0;
% define the solution mesh
x = linspace(0,1,20);
t = linspace(0,2,10);
% solve the PDE
u = pdepe(m,@eqn1,@initial1,@bc1,x,t);
%Plot solution
figure(1);
surf(x,t,u);
title('Surface plot of solution.');
xlabel('Distance x'); ylabel('Time t');
grid on
Often, we find it useful to plot solution profiles, for which t is fixed, and u is plotted against x.
The solution u(t, x) is stored as a matrix indexed by the vector indices of t and x. For example,
u(1, 5) returns the value of u at the point (t(1), x(5)). We can plot u initially (at t = 0) with the
command plot(x,u(1,:)) (see Figure 1.2).

Finally, a quick way to create a movie of the profile's evolution in time is with the following
MATLAB sequence.

fig = plot(x,u(1,:),'erase','xor')
for k=2:length(t)
set(fig,'xdata',x,'ydata',u(k,:))
pause(.5)
end

If you try this out, observe how quickly solutions to the heat equation approach their equilibrium
configuration. It is noted that the equilibrium configuration is the one that ceases to change in
time.
Section-3: System of Parabolic Partial Differential Equations
We next consider a system of two partial differential equations, though still in time and one
space dimension.
Example 2. Consider the nonlinear system of partial differential equations

This is a non-dimensionalized form of a PDE model for two competing populations. As with
solving ODE in MATLAB, the basic syntax for solving systems is the same as for solving single
equations, where each scalar is simply replaced by an analogous vector. In particular, MATLAB
specifies a system of n PDE as

Observe that the functions ck, bk, and sk can depend on all components of u and ux with boundary
conditions
and initial conditions

In our example equation, we have

which we specify with the MATLAB M-file eqn2.m.


function [c,b,s] = eqn2(x,t,u,DuDx)
%EQN2: MATLAB M-file that contains the coefficients for
%a system of two PDE in time and one space dimension.
c = [1; 1];
b = [1; 1] .* DuDx;
s = [u(1)*(1-u(1)-u(2)); u(2)*(1-u(1)-u(2))];
For our boundary conditions, we have

which we specify in the function M-file bc2.m.


function [pl,ql,pr,qr] = bc2(xl,ul,xr,ur,t)
%BC2: MATLAB function M-file that defines boundary conditions
%for a system of two PDE in time and one space dimension.
pl = [0; ul(2)];
ql = [1; 0];
pr = [ur(1)-1; 0];
qr = [0; 1];
For our initial conditions, we have

which we specify in the function M-file initial2.m.


function value = initial2(x);
%INITIAL2: MATLAB function M-file that defines initial conditions
%for a system of two PDE in time and one space variable.
value = [x^2; x*(x-2)];

We solve equation (1.2) and plot its solutions with pde2.m (see Figure 1.5).
%pde2.m: MATLAB script M-file that solves the PDE
%stored in eqn2.m, bc2.m, and initial2.m
Clc; close all; clear all
m = 0;
x = linspace(0,1,10);
t = linspace(0,1,10);
sol = pdepe(m,@eqn2,@initial2,@bc2,x,t);
u1 = sol(:,:,1);
u2 = sol(:,:,2);
subplot(2,1,1)
surf(x,t,u1);
title('u1(x,t)'); xlabel('Distance x'); ylabel('Time t');
subplot(2,1,2)
surf(x,t,u2);
title('u2(x,t)'); xlabel('Distance x'); ylabel('Time t');
Instead of pdepe, how can we solve these partial differential equations by using FDM, FEM
or some other robust numerical methods?

Best of luck!

You might also like