我正在 MATLAB 的 pdepe 求解器中求解以下一维传输方程。
在入口处(左边界),应用狄利克雷边界条件. (1为入口节点号)
在出口(右边界),扩散通量被忽略。
在 MATLAB 的pdepe求解器中实现上述边界条件。
function [pl,ql,pr,qr] = bcfun(xl,cl,xr,cr,t)
pl = cl - 3;
ql = 0;
pr = 0;
qr = 1;
end
据我了解,空间方向是离散化的,并且使用求解器求解得到的ode15s
ode pdepe
。
我试图在我自己的代码版本中做同样的事情,它实现了pdepe
求解器中所做的事情。pdepe
但是,我的结果与求解器不一致。我对一阶导数使用了后向差分方案,对二阶导数使用了中心差分方案。我不确定在 MATLAB 的 pdepe 求解器中实现的方案。
我通过以下方式实现了边界条件。
dC(1) = 0
dC(nnode,1) = -v*(C(nnode) - C(nnode-1))/delx + (D/delx^2)*2*(C(nnode-1) - C(nnode))
右边界条件:
在最后一个节点,
dC(nnode,1) = -v*(C(nnode) - C(nnode-1))/delx + (D/delx^2)*(C(nnode-1) - C(nnode) +C(nnode+1))
等于
dC(nnode,1) = -v*(C(nnode) - C(nnode-1))/delx + (D/delx^2)*2*(C(nnode-1) - C(nnode))
完整的代码是
function sol=so()
format short
global D nnode init_co find_index v
m = 0;
delx = 0.25;
xend = 10;
D = 500;
v = 200;
x = 0:delx:xend;
find_index = x;
tspan = 0:0.00001:1;
init_co = [3 ; zeros(length(x)-1,1)];
nnode = length(x);
%% pdepe solver
sol = pdepe(m,@pdefun,@icfun,@bcfun,x,tspan)
figure(1)
subplot(1,2,2)
plot(tspan,sol)
xlabel('time')
ylabel('c')
xlim([-0.01 0.5])
ylim([2.995 3.005])
title('MATLAB - pdepe')
grid on
function [g,f,s] = pdefun(x,t,c,DcDx)
g = 1;
f = D*DcDx;
s = -v*DcDx;
end
function c0 = icfun(x)
c0 = init_co(find(find_index==x));
end
function [pl,ql,pr,qr] = bcfun(xl,cl,xr,cr,t)
pl = cl - 3;
ql = 0;
pr = 0;
qr = 1;
end
function dC= mysolver(t,C)
dC(1,1) = 0;
for i = 2:nnode-1
dC(i,1) = -v*(C(i) - C(i-1))/delx + D/delx^2*(C(i-1) -2*C(i) + C(i+1));
end
dC(nnode,1) = -v*(C(nnode) - C(nnode-1))/delx + (D/delx^2)*2*(C(nnode-1) - C(nnode)); % DdC/dx = 0
end
%% my solver
[tspan C] = ode15s(@(t,s) mysolver(t,s), tspan , init_co);
figure(1)
subplot(1,2,2)
plot(tspan,C)
xlabel('time')
ylabel('c')
xlim([-0.01 0.5])
ylim([2.995 3.005])
title('My solver')
grid on
% figure(2)
% plot(tspan, abs(sol - C))
% title('Absolute error')
end
产生的绝对错误(pdepe 解决方案 - 我的实现)是
此外,绝对误差随着网格尺寸的增加(
delx
从 0.25 增加到 1)而增加。
我不确定为什么绝对误差会增加。是因为我使用了向后和居中的差异方案,还是因为我的边界条件的实现方式?
有什么建议?