使用ODE45查找函数的最大值

时间:2013-05-16 22:28:54

标签: matlab differential-equations

我试图在MATLAB中的微分方程系统中找到其中一个方程的位置。我试图使用odeset的事件属性。我如何选择特定方程式我的功能?

options = odeset('Events',@event);
[t x tm xm ie] = ode45(@Lorenz,[0 200],I,options);


function X = Lorenz(t,x)
r = 15;
sigma = 10;
b = 8/3;
X(1,1) = sigma*(x(2,1)-x(1,1));
X(2,1) = r*(x(1,1)) - x(2,1) -x(1,1)*x(3,1);
X(3,1) = x(1,1)*x(2,1) - b*x(3,1);
end

function [value,isterminal,direction] = event(t,x)
value  = Lorenz(t,x); %I can't specify X(3,1) here
isterminal = 0;
direction = -1;
end

特别是我试图在X(3,1)= 0时记录。

谢谢

2 个答案:

答案 0 :(得分:2)

基本上,查看documentation,如果您有兴趣查看x(3)= 0,那么您需要重写事件函数:

function [value,isterminal,direction] = event(t,x)
value  = x(3); %I can't specify X(3,1) here --> why not?? Lorenz(t,x) is going to return the differential. That's not what you want
isterminal = 0;
direction = 0; %The desired directionality should be "Detect all zero crossings"
end

现在我不知道你在

中如何定义I
[t x tm xm ie] = ode45(@Lorenz,[0 200],I,options);

但是你对方程的解决方案在几个点附近非常稳定,如果x(3)在零时间为负,你可能只看到一个零交叉。

[t x tm xm ie] = ode45(@khal,[0 5],[10 -1 -20],options);
tm =

    0.1085

enter image description here

答案 1 :(得分:2)

如果您正在寻找ODE的最大值,正如您的问题标题所示,那么您非常接近。您正在使用微分方程本身的根来找到这些点,即当导数为零时。这与具有零(或其他)值但具有相关性的解决方案略有不同。问题是你正在指定value = Lorenz(t,x),当你只对x(3)感兴趣时,ODE函数会返回一个向量。但是你可以访问状态向量并且有三种选择。

最简单的:

function [value,isterminal,direction] = event(t,x)
b = 8/3;
value = x(1)*x(2)-b*x(3); % The third equation from Lorenz(t,x) 
isterminal = 0;
direction = -1;

或者效率低下:

function [value,isterminal,direction] = event(t,x)
y = Lorenz(t,x); % Evaluate all three equations to get third one
value = y(3);
isterminal = 0;
direction = -1;

或者,如果您想要所有三个维度的最大值:

function [value,isterminal,direction] = event(t,x)
value = Lorenz(t,x);
isterminal = [0;0;0];
direction = [-1;-1;-1];

如果您对全局最大值感兴趣,那么您需要处理输出xm。或者,如果您处于系统具有某些振荡行为的状态,那么您可以将isterminal切换为1或只查看xm中的第一个值。< / p>

最后,您可以考虑通过匿名函数传递参数:

r = 15;
sigma = 10;
b = 8/3;
f = @(t,x)Lorenz(t,x,r,sigma,b);

I = [1;5;10];
options = odeset('Events',@(t,x)event(t,x,b));

[t,x,tm,xm,ie] = ode45(f,[0;10],I,options);

使用:

function X = Lorenz(t,x,r,sigma,b)
...

function [value,isterminal,direction] = event(t,x,b)
...