0
$\begingroup$

I am trying to use Matlab's pdepe.m to solve the first order parabolic PDE $$\frac{\partial u}{\partial x}+\frac{\partial u}{\partial x}=x$$ I have not had trouble coding the argument of pdepe @pdefun:

function [c,f,s] = pdefun(x,t,u,DuDx)
c = 1;
f = -u;
s = x;
end

Since this is first order in space and time, we have one boundary condition and one initial condition. pdepe takes as an argument @bcfun(xl,ur,xr,ur,t), which has two boundary conditions. I understand for a unique solution to a PDE that is second order in space we would need two boundary conditions. However, given that this first order PDE can be expressed with pdepe, I would expect there is a way to code in only a single boundary condition, since a second boundary condition would overconstrain the solution.

I have tried coding in a single boundary condition as follows:

function [pl,ql] = pdebc(xl,ul,t)
pl = ul;
ql = 0;
end

But Matlab gives the error:

Error using prob1pdepe
Too many input arguments.

Error in pdepe (line 250)
[pL,qL,pR,qR] = feval(bc,xmesh(1),y0(:,1),xmesh(nx),y0(:,nx),t(1),varargin{:});

Is there a way to solve PDEs of this type with pdepe? If so, how do I deal with @bcfun in a way that does not overconstrain the problem?

My full code is shown below

clearvars; close all; clc

% definitions
x = linspace(0,10,256); % mesh with L=1
t = linspace(0,50,256); 
sol = pdepe(0,@(x,t,u,DuDx)pdedef(x,t,u,DuDx),@(x)pdeic(x),@(xl,ul,t)pdebc(xl,ul,t),x,t);

function [c,f,s] = pdedef(x,t,u,DuDx)
c = 1;
f = -u;
s = x;
end
% % % % % % % % % % % % % % % % % % % % % % %
function u0 = pdeic(x)
u0 = 0;
end
% % % % % % % % % % % % % % % % % % % % % % %
function [pl,ql] = pdebc(xl,ul,t)
pl = ul;
ql = 0;
end

I've tried a number of variations including of how I call the bcfun including

sol = pdepe(0,@(x,t,u,DuDx)pdedef(x,t,u,DuDx),@(x)pdeic(x),@(xl,ul,xr,ur,t)pdebc(xl,ul,t),x,t);

which gives the following error:

Error using prob1pdepe>pdebc
Too many output arguments.

Error in prob1pdepe (line 6)
sol = pdepe(0,@(x,t,u,DuDx)pdedef(x,t,u,DuDx),@(x)pdeic(x),@(xl,ul,xr,ur,t)pdebc(xl,ul,t),x,t);

Error in pdepe (line 250)
[pL,qL,pR,qR] = feval(bc,xmesh(1),y0(:,1),xmesh(nx),y0(:,nx),t(1),varargin{:});

I've tried making bcfun take the arguments xr and ur but do nothing with them:

function [pl,ql] = pdebc(xl,ul,xr,ur,t)
pl = ul;
ql = 0;
end

When I do this, the error message is

Error using prob1pdepe>pdebc
Too many output arguments.

Error in prob1pdepe (line 6)
sol = pdepe(0,@(x,t,u,DuDx)pdedef(x,t,u,DuDx),@(x)pdeic(x),@(xl,ul,xr,ur,t)pdebc(xl,ul,xr,ur,t),x,t);


Error in pdepe (line 250)
[pL,qL,pR,qR] = feval(bc,xmesh(1),y0(:,1),xmesh(nx),y0(:,nx),t(1),varargin{:});

for both of the ways I called @bcfun in pdepe.

Any advice would be great. Thanks!

$\endgroup$
3
  • 1
    $\begingroup$ I assume your PDE as-written has a typo? Also the error you show is apparently due to a basic matlab coding error. I suggest you post your complete matlab code. $\endgroup$ Mar 5, 2023 at 14:18
  • $\begingroup$ I have updated the post to include the full code and a few of the variations I tried and their corresponding error messages $\endgroup$ Mar 5, 2023 at 16:34
  • $\begingroup$ You might consider using the method of characteristics on this equation rather than the numerical method of lines. $\endgroup$ Mar 6, 2023 at 20:10

1 Answer 1

1
$\begingroup$

I have included my version of your code below.

The first significant change is that I have moved the pdepe definition of your du/dx term from f to s. Obviously this defines the same PDE. The reason for this change is so we can use a "trick" to define the boundary condition at the right end required by pdepe. Since f equals zero over the entire domain for all time, defining a boundary condition of $f=0$ at the right end causes no harm. This is shown in my version of the pdebc function.

Here are a couple of plots of the solution.

enter image description here

enter image description here

Finally, it is worth mentioning that pdepe is designed to solve parabolic PDE, e.g. ones with second derivatives with respect to x. That is why it expects boundary conditions at both ends of the domain. However, it is sometimes possible to solve simple first-order, hyperbolic PDE like this one. In general, solving hyperbolic PDE with pdepe often requires more tricks and approximations.

function matlabAnswers_3_5_2023
% definitions
x = linspace(0,10,100); % mesh with L=1
t = linspace(0,15,256); 
sol = pdepe(0,@pdedef,@pdeic,@pdebc,x,t);
figure; plot(x, sol(end,:));
title('u at final time'); xlabel 'x'
figure; plot(t, sol(:,end));
title('u at right end as a function of time')
xlabel 'time'
end


function [c,f,s] = pdedef(x,t,u,DuDx)
c = 1;
f = 0;
s = x-DuDx;
end
% % % % % % % % % % % % % % % % % % % % % % %
function u0 = pdeic(x)
u0 = 0;
end
% % % % % % % % % % % % % % % % % % % % % % %
function [pl,ql,pr,qr] = pdebc(xl,ul,xr,ur,t)
pl = ul;
ql = 0;
pr=0;
qr=1;
end
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.