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
In particular I'm trying to record whenever X(3,1) = 0.
Thank you
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 inx(3)
. But you have access to the state vector and have have three alternatives.The simplest:
Or, less efficiently:
Or, if you want maxima for all three dimensions:
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 switchisterminal
to1
or just look at the first value inxm
.Lastly, you might consider passing your parameters via anonymous functions:
with:
Basically, looking at the documentation, if you're interested in looking at when x(3) = 0, then you need to rewrite your event function:
Now I don't know how you defined
I
inBut 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.