简体   繁体   English

使用ODE45查找函数的最大值

[英]Finding the maxima of a function using ODE45

I'm trying to locate the locations of one of the equations in a system of differential equations in MATLAB.I'm trying to use the Events propety of odeset.How do I pick out the particular equation in my function? 我试图在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

In particular I'm trying to record whenever X(3,1) = 0. 特别是我试图在X(3,1)= 0时记录。

Thank you 谢谢

Basically, looking at the documentation , if you're interested in looking at when x(3) = 0, then you need to rewrite your event function: 基本上,查看文档 ,如果您有兴趣查看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

Now I don't know how you defined I in 现在我不知道你是如何定义I

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

But your solution to the equation is very stable around a few points and you may only see one zero crossing if x(3) at time zero is negative. 但是你对方程的解决方案在几个点附近非常稳定,如果x(3)在零时间为负,你可能只看到一个零交叉。

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

    0.1085

在此输入图像描述

If you're looking for maxima of an ODE, as the title of your question indicates, then you are very close. 如果您正在寻找ODE的最大值,正如问题的标题所示,那么您非常接近。 You're using the the roots of the differential equation itself to find these points, ie, when the derivatives are zero. 您正在使用微分方程本身的根来找到这些点,即当导数为零时。 This is slightly different from the solution having zero (or some other) value, but related. 这与具有零(或其他)值但具有相关性的解决方案略有不同。 The problem is that you're specifying value = Lorenz(t,x) and the ODE function returns a vector when you're only interested in x(3) . 问题是你指定value = Lorenz(t,x) ,当你只对x(3)感兴趣时,ODE函数会返回一个向量。 But you have access to the state vector and have have three alternatives. 但是您可以访问状态向量并且有三种选择。

The simplest: 最简单的:

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;

Or, less efficiently: 或者,效率较低:

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;

Or, if you want maxima for all three dimensions: 或者,如果您想要所有三个维度的最大值:

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

If you're interested in the global maximum then you'll need to process the outputs, xm . 如果您对全局最大值感兴趣,那么您将需要处理输出xm Or if you're in a regime where the system has certain oscillatory behaviors, then you may be able to just switch isterminal to 1 or just look at the first value in xm . 或者,如果您处于系统具有某些振荡行为的状态,那么您可以将isterminal切换为1或仅查看xm中的第一个值。

Lastly, you might consider passing your parameters via anonymous functions: 最后,您可以考虑通过匿名函数传递参数:

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);

with: 有:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM