使用ODE45查找函数的最大值 [英] Finding the maxima of a function using ODE45

查看:179
本文介绍了使用ODE45查找函数的最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在MATLAB的微分方程系统中定位一个方程的位置,试图利用odeset的事件属性,如何在函数中挑选特定的方程呢? /p>

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?

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时进行记录.

In particular I'm trying to record whenever X(3,1) = 0.

谢谢

推荐答案

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

If you're looking for maxima of an ODE, as the title of your question indicates, then you are very close. You're using the the roots of the differential equation itself to find these points, i.e., 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). But you have access to the state vector and have have three alternatives.

最简单的

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;

或者,如果您希望所有三个维度均达到最大值:

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

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

If you're interested in the global maximum then you'll need to process the outputs, 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.

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

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

具有:

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

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

这篇关于使用ODE45查找函数的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆