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.
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
. 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);
with:
function X = Lorenz(t,x,r,sigma,b)
...
function [value,isterminal,direction] = event(t,x,b)
...