Finding the maxima of a function using ODE45

前端 未结 2 2064
太阳男子
太阳男子 2020-12-11 23:21

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

2条回答
  •  青春惊慌失措
    2020-12-11 23:53

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

提交回复
热议问题