MATLAB使用ode45制作波函数动画

MATLAB使用ode45制作波函数动画,matlab,warnings,integration,ode,Matlab,Warnings,Integration,Ode,我正在用MATLAB制作波函数的动画。到目前为止,我还没有达到动画的一部分,因为我的ode45抛出了一个错误。我用的是de Korteweg de Vries方程 (可在此处找到:) 现在我的代码如下: function[t, N, u] = wave(u0, L, T, S, N) %First we create a vector t which stores the time entries, a vector x which %stores the location entries a

我正在用MATLAB制作波函数的动画。到目前为止,我还没有达到动画的一部分,因为我的ode45抛出了一个错误。我用的是de Korteweg de Vries方程 (可在此处找到:)

现在我的代码如下:

function[t, N, u] = wave(u0, L, T, S, N)

%First we create a vector t which stores the time entries, a vector x which
%stores the location entries and an h which is the location step size for
%the Korteweg-De Vries function.
time = linspace(0,T,S);
x = linspace(-L,L,N);
h = x(2)-x(1);

U0=u0(x);

options=odeset('RelTol',1e-13,'AbsTol',1e-13);
[t,u] = ode45(@kdv,time,U0,options);

    function dudt = kdv(t,u)

    d2udx2 = ([u(N)-2*u(1)+u(2); diff(u,2); u(N-1)-2*u(N)+u(1)] ./ h^2);
    total = d2udx2 + 3.*u.^2;

    diff_total = diff(total);
    diff_total = [diff_total(end); diff_total(1:end)];

    dudt = -[diff_total(2)-diff_total(N-1); diff(diff_total,2); diff_total(N-1)+diff_total(2)] ./ (2*h);

    end
end
现在,当我设置
f=@(x)(2/2)。*(1./cosh(sqrt(2)。*x./2)。^2)
然后调用
[t,N,u]=wave(f,20,10,200,200)的函数我得到以下错误:

警告:t=6.520003e-02时出现故障。如果不将步长减小到以下,则无法满足集成公差 时间t时允许的最小值(2.220446e-16)。 第45行(第360行) 波浪中(第23行)

此外,返回的
t
u
大小分别为
16x1
16x200
,但我认为如果没有出现警告,它们将扩展到
200x1
200x200


任何关于如何解决这个问题的提示都将不胜感激。

您做了太多的差异,构建
diff\u total
的中间步骤是多余的

  • d2udx2
    total
    的构造正确。您可以将第一个构造缩短为

    d2udx2 = diff([u(N);  u;  u(1) ], 2)./h^2`;
    
  • 下一步构造
    diff_total
    dudt
    中的差异构造是冗余的。<代码>扩散总数< /代码>构造缺少代码< > < <代码>的划分,由于某些未知的原因,在代码< dUDT < /C> >中,差分顺序为2。

  • 外部
    x
    微分最好通过卷积得到一个中心差商,就像在外部元素中一样

    dudt = -conv([ total(N); total; total(1)], [1; 0; -1], 'valid')/(2*h)
    
所有这些都给出了导数程序

函数dudt=kdv(t,u,h)
d2udx2=diff([u(end);u;u(1)],2)。/h^2;
总计=d2udx2+3*u.^2;
dudt=-conv([total(end);total;total(1)],[1;0;-1],'valid')。/(2*h);
结束
它(使用
N=80
)生成解决方案


它是一个以
c=2的速度传播的孤子波

是什么原因使迭代差增加到5阶,而
diff_total
?为什么在最终导数向量中存在二阶差?为什么不将迭代差除以适当的幂
h?Korteweg de vries方程表示du/dt=-d/dx*(d^2u/dx^2+3u^2)。因此,我认为我需要二阶差,来得到括号之间的部分,对吗?忘记我的最后一句话,正确的划分是存在的
d2udx2
,因此正确构造了
total
。移动附加备注以回答问题。