代码之家  ›  专栏  ›  技术社区  ›  Ohm

在OCTAVE中使用Hindmarhs ODE解算器LSODE

  •  1
  • Ohm  · 技术社区  · 10 年前

    我正在学习OCTAVE,我正在尝试使用LSODE ODE解算器来集成 FitzHugh–Nagumo model 。我的尝试如下:

    time = linspace(0,200,1000);
    u0 = rand(32,32);
    v0 = rand(32,32);
    
    vec_u0 = reshape(u0,[1,size(u0)(1)*size(u0)(2)]);
    vec_v0 = reshape(v0,[1,size(v0)(1)*size(v0)(2)]);
    vec_FHN0 = horzcat(vec_u0,vec_v0);
    
    FHN = lsode("FHN_eq_vec", vec_FHN0, time);
    FHN(end)
    

    其中我定义的所有函数都在我在GitHub中设置的存储库中- link 我创建了一个函数,将FHN模型的两个2D字段转换为行向量(正如我从 examples 这里LSODE积分器使用行向量作为输入)。我收到以下错误消息:

    >> fhn_integrate_lsode
    warning: non-integer range used as index
    warning: called from
        FHN_eq_vec at line 3 column 7
        fhn_integrate_lsode at line 9 column 5
    error: reshape: can't reshape 0x1 array to 1x1 array
    error: called from
        FHN_eq_vec at line 4 column 3
        fhn_integrate_lsode at line 9 column 5
    error: lsode: evaluation of user-supplied function failed
    error: called from
        fhn_integrate_lsode at line 9 column 5
    error: lsode: inconsistent sizes for state and derivative vectors
    error: called from
        fhn_integrate_lsode at line 9 column 5
    >>
    

    有人知道问题出在哪里?

    1 回复  |  直到 10 年前
        1
  •  2
  •   Sebastian    10 年前

    此问题已在答复 http://octave.1599824.n4.nabble.com/Using-Hindmarsh-s-ODE-solver-LSODE-in-OCTAVE-td4674210.html

    然而,快速查看您的代码 解很可能是一个源于 pde空间离散化。,

    $dx(t)/dt=f(x,t):=-K x(t)+r(t)$

    其中K是平方矩阵(拉普拉斯?!)f是时间相关的 匹配维度的函数。我想你的系统很僵硬 (由于右侧的负拉普拉斯) 对10^(-4)量级的错误感到满意。因此,你应该适应 lsode的选项:

    lsode_options("integration method","stiff");
    lsode_options("absolute tolerance",1e-4);
    lsode_options("relative tolerance",1e-4);
    

    然后

    T = 0:1e-2:1; % time vector
    K = sprandsym(32,1)+eye(32); % symmetric stiffness matrix
    x0 = rand(32,1); % initial values
    r = @(t) rand(32,1)*sin(t); % excitation vector
    f = @(x,t) (-K*x+r(t)); % right-hand-side function
    x=lsode (f, x0, T); % get solution from lsode
    

    您应该利用雅可比df/dx的任何知识,因为这将 加快计算速度。在线性ODE的情况下,这是微不足道的:

    f = {@(x,t) -K*x+r(t), @(x,t) -K}; % right-hand-side function.
    

    另一方面,如果系统具有额外的质量矩阵

    百万美元dx(t)/dt=-K x(t)+r(t)$

    事情可能会变得更加复杂。你可能想用另一个 时间步进器。只有M有完整的等级,你才能做到

    f = @(x,t) ( M\(-K*x+r(t)) ); % right-hand-side function
    

    这通常不是很有效。

    再见Sebastian