Initial conditions for PDEPE
이전 댓글 표시
Hi community,
I am trying to solve 9 PDEs using pdepe routine for which the initial condition for all these equations is zero, Ci(t=0) = 0. My solution mesh is as:
% Solution mesh
x = linspace(0,1,50); % space
t = linspace(0,100,100); % time
I attempted to code the initial conditions as:
% icfun_sr() defines the initial conditions
function u0 = icfun_sr(x)
u0 = zeros(9,1);
end
which I can then pass to solve the equations as:
% Solve equations
m=0;
sol = pdepe(m, @pdefun_sr, @icfun_sr, @bcfun_sr, x, t, [], A, B, D);
The error thrown as
Error using sr_code>icfun_sr
Too many input arguments.
Error in pdepe (line 229)
temp = feval(ic,xmesh(1),varargin{:});
Error in sr_code (line 32)
sol = pdepe(m, @pdefun_sr, @icfun_sr, @bcfun_sr, x, t, [], A, B, D);
Full code below:
close all
clear all
clc
global A B D Ncomp
% Constants
Rtime = 2;
u = 0.7;
L = 1;
D_ax = 1.5e-3;
Bo = u*L/D_ax;
density = 2000;
poros = 0.45;
Ncomp = 9;
% mesh
x = linspace(0,1,50); % space
t = linspace(0,100,100); % time
% Other parameters;
A = -1/Rtime;
B = 1/Rtime*Bo;
D = density/poros;
% Solve equation
m=0;
pdef = @(x, t, u, dudx) pdefun_sr(x, t, u, dudx);
sol = pdepe(m, pdef, @icfun_sr, @bcfun_sr, x, t);
% Extract solutions
for i = 1:Ncomp
u(i) = sol(:,:,i);
end
waterfall(x,t,u(1)), map=[0 0 0]; colormap(map), view(15,60)
function [c,f,s] = pdefun_sr(x, t, u, dudx)
global A B D Ncomp
c = ones(Ncomp,1); % diagonal terms
k = ones(12,1);
u(10) = 6; % or u(10) = 6 - exp(-10*t);
f = [0; % diffusion term
0;
0;
0;
A*u(5) + B*dudx(5);
0;
A*u(7) + B*dudx(7);
A*u(8) + B*dudx(8);
A*u(9) + B*dudx(9)
];
% source term
s = [k(7).*u(3) - k(1).*u(1);
k(9).*u(6) - k(2).*u(2);
k(12).*u(10) - (k(3) + k(5) + k(8) + k(7)).*u(3); % fails at u(10)
k(10).*u(6) + k(11).*u(5) - k(6).*u(4) - k(4).*u(4);
D*( k(6).*u(4) - k(11).*u(5) );
k(8).*u(3) - (k(9) + k(10)).*u(6);
D*( k(1).*u(1) );
D*( k(2).*u(2) + k(3).*u(3) );
D*( k(5).*u(3) + k(4).*u(4) )
];
end
% icfun_sr() defines the initial conditions
function u0 = icfun_sr(x)
global Ncomp
u0 = zeros(Ncomp,1);
end
function [pl,ql,pr,qr] = bcfun_sr(xl,ul,xr,ur,t,A,B,D)
% bcfun_sr() defines the initial conditions
global Ncomp
pl = zeros(Ncomp,1).*ul;
ql = zeros(Ncomp,1);
pr = zeros(Ncomp,1);
qr = ones(Ncomp,1); % or ones(dudx)*Ncomp times
end
채택된 답변
추가 답변 (1개)
Bill Greene
2023년 3월 31일
편집: Bill Greene
2023년 3월 31일
The problem is that you are using an old, undocumented form of the of the pdepe function
to pass additional parameters to the functions called by pdepe. If you use this form, you must
include the extra parameters in ALL of the called functions, e.g.
function u0 = icfun_sr(x,A,B,C)
The reason that this old form of pdepe is undocumented is that anonymous functions provide a
superior way of passing extra parameters to called functions. In your example, you could define
the anonymous function
pdef = @(x, t, u, dudx) pdefun_sr(x, t, u, dudx, A, B, D);
and call pdepe
sol = pdepe(m, pdef, @icfun_sr, @bcfun_sr, x, t);
But even after changing your code to either of the two solutions, you will still get errors due
to the variable k being undefined in function pdefun_sr
.
댓글 수: 16
Matterazzi
2023년 3월 31일
You will get problems with the components of your solution vector for which you prescribed f = 0.
For the other components, take the convective parts of the fluxes into the source term s and don't leave them in f. The reason is that your formulation from above will collide with the boundary condition at x=xr.
Matterazzi
2023년 3월 31일
You cannot use MATLAB's "pdepe" in its generic form.
Either you discretize your equations in space on your own or you try Bill Greene's code that can handle additional ODEs:
I think he will support you here if you have problems with his code.
And what I meant was to put the
A*dudx(5),A*dudx(7),A*dudx(8) and A*dudx(9)
(the convective part) in s, not
B*d^2u/dx^2(5),B*d^2u/dx^2(7),B*d^2u/dx^2(8) and B*d^2u/dx^2(9)
(the diffusive part).
Bill Greene
2023년 3월 31일
I suggest you post the equations you are trying to solve in mathematical form(you can use LaTex notation for this).
I'm fairly certain that your latest error is due to
pl = zeros(Ncomp,1).*ul;
ql = zeros(Ncomp,1);
not being a valid definition of boundary conditions at the left end.
Matterazzi
2023년 3월 31일
Bill Greene
2023년 4월 1일
I assumed that the error Torsten pointed out in your mathematical description was just a typo on your part-- that you really meant zero-flux at x=1. I also assumed that your left-end BC was also a typo-- that you really want C_i=0 at x=0. Accordingly I changed the pl definition in your BC function to
pl = ul;
This allows pdepe to complete the analysis. However C_1, C_2, C_3, C_4, and C_6 are showing spurious oscillations. This is likely due to the lack of a diffusion term in these equations.
Matterazzi
2023년 4월 1일
편집: Matterazzi
2023년 4월 1일
Torsten
2023년 4월 1일
Your value for B is approximately 233 ! I expected something in the order of D_ax. Don't you think this must be wrong ?
Setting
f = [B*1e-7*dudx(1); % diffusion term
B*1e-7*dudx(2);
B*1e-7*dudx(3);
B*1e-7*dudx(4);
B*dudx(5);
B*1e-7*dudx(6);
B*dudx(7);
B*dudx(8);
B*dudx(9)
];
instead of
f = [0; % diffusion term
0;
0;
0;
B*dudx(5);
0;
B*dudx(7);
B*dudx(8);
B*dudx(9)
];
introduces a slight amount of diffusivity that prevents the spurious oscillations to happen (at least for the value of B you use at the moment).
Matterazzi
2023년 4월 2일
Torsten
2023년 4월 2일
Just out of interest: Why do components 5,7,8 and 9 move with velocity A and diffuse while the other components are spatially fixed and don't diffuse ?
Matterazzi
2023년 4월 3일
Torsten
2023년 4월 4일
Why is k equal to initial guess k0?
Because you overwrite the parameters supplied by lsqcurvefit in this line:
k = ones(12,1)*1E-2;
Matterazzi
2023년 4월 4일
카테고리
도움말 센터 및 File Exchange에서 Calculus에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






