【问题标题】:MATLAB ode45 OutputFcn to monitor changing value of if loopMATLAB ode45 OutputFcn 监控 if 循环的变化值
【发布时间】:2020-10-09 12:21:04
【问题描述】:

我尝试保存/查看在 ode45 微分方程求解过程中由 if 循环更改的变量 m。

%Some parameter setting above
myfun=fprintf('m', num2str(m))
options = odeset('NonNegative',[1:3],'RelTol',1e-5,'AbsTol',1e-8, 'OutputFcn', @myfun);
[t,x] = ode45('myfunction', tspan, x0, options); %calculation

if 循环在所有其他方程式之前位于方程式文件中:

if x(1)>=threshold
    m=1 ;
   return
else
    m=0 ;
end

我已经查看了 ode45 的 OutputFcn 选项的 matlab 描述,还阅读了 https://de.mathworks.com/help/deeplearning/ug/customize-output-during-deep-learning-training.html 没有正确理解它。我也对其他解决方案持开放态度,以“查看”在 ode 计算期间 m 的值。

【问题讨论】:

  • 你问题中的链接不是ode相关的输出函数,ode outputFcn的相关信息见here
  • 谢谢,我也已经调查过这个链接。我刚刚提到了上面的第一个链接,因为我看到它推荐了关于 OutputFcn 的类似问题

标签: matlab variables output ode45


【解决方案1】:

创建一个单独的文件,并调用它myOutputFcn.m,代码如下

function status = myOutputFcn(t,y,flag,threshold)

    switch(flag)
        case 'init' % code to run before integration
            ;
        case ''     % code to run after each integration step
            % act on state
            if y(1)>=threshold
                m = 1;
            else
                m = 0;
            end
            % print m 
            fprintf('% 4.3f\t%i, t, m\n',t,m);
        case 'done' % code to run when integation is finished
            ;
    end

    status = 0; % need to set status, otherwise integration will halt

end

然后要在每次迭代中使用正确的阈值调用此输出函数,您必须执行以下操作

threshold = 10; % idk, your threshold
options = odeset('NonNegative',[1:3],'RelTol',1e-5,'AbsTol',1e-8, 'OutputFcn', @(t,y,flag) myOutputFcn(t,y,flag,threshold));
[t,x] = ode45('myfunction', tspan, x0, options); %calculation

【讨论】:

  • 对不起,我只能在周末测试你的代码。非常感谢@rinkert,这是我最初的打算:D 它帮助很大。还要感谢小脚本中的 cmets,所以我大致了解了哪个部分在做什么。
猜你喜欢
  • 1970-01-01
  • 2017-06-13
  • 1970-01-01
  • 1970-01-01
  • 2014-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多