MATLAB ode45输出Fcn以监控if循环的变化值



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

%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。

创建一个单独的文件,并使用以下代码调用此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.3ft%i, t, mn',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