【发布时间】:2015-04-10 13:28:39
【问题描述】:
我有一个用于计算酸度如何变化的 ODE。一切都很好,只是我想在酸度达到临界点时改变一个常数。这应该是我想模拟的某种不可逆的效果。
我的常量来自一个结构文件 (c) 我在 ODE 函数中加载过一次。
[Time,Results] = ode15s(@(x, c) f1(x, c),[0 c.length],x0,options);
我在这里遇到的主要问题不是告诉 Matlab 更改常数,而是记住如果它已经在模拟期间发生过一次。所以 Matlab 应该采用不可逆变化的常数,而不是我一开始提供的那个。
我想编写一个在 ODE 运行时保存的标志和一个if 条件,“如果标志存在,则更改常量”。该怎么做?
更新一: 问题解决了
这是第一个自己的解决方案,它没有经过打磨,需要结构文件的方法。这意味着,应该在事件中突然改变的常量必须是结构文件,这些文件在 ODE 函数中传递到应该评估的函数中(查看上面的 ODE 语法)。该函数接受如下输入:
function [OUTPUT] = f1(t, x, c)
% So here, the constants all start with c. followed by the variable name. (because they are structs instead of globals)
%% write a flag when that event happens
if c.someODEevent <= 999 && exist ('flag.txt') == 0
dlmwrite ('flag.txt',1);
end
%% next iteration will either write the flag again or not. more importantly, if it exists, the constant will change because of this.
if exist ('flag.txt') == 2
c.changingconstant = c.changingconstant/2;
end
end
请查看 Horchlers 的友好回答,您必须注意这样的步骤可能会引入不准确之处,并且您必须小心检查您的代码是否完成了它应该做的事情。
【问题讨论】:
标签: matlab simulation ode numerical-integration