How to find the maximum of a variable within a function ?

Hi,
My question is, suppose I have a function like the following (got this eg. from MATLAB ODE solver help)
function dy = rigid(t,y)
dy = zeros(3,1); % a column vector
dy(1) = y(2) * y(3);
dy(2) = -y(1) * y(3);
dy(3) = -0.51 * y(1) * y(2);
I solve for this system of ODEs using the ode45 solver.
But suppose I'd like to find the maximum of one of the variables say y(1) within the function rigid.m and then say for instance, dy(2)= -max(y(1)) * y(3), how do i do it? Specifying, max(y(1)) within the function rigid.m doesn't seem to work in calculating the maximum value of the variable y(1).
I'd appreciate any help with this. Thanks, Lakshmi

2 Comments

You want max(y(1)) be the maximum value calculated until the moment or during all integration interval?
Hi Jose,
I want max(y(1)) to be constant during all the integration interval.
Thanks, Lakshmi

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 29 Sep 2012
Edited: Star Strider on 29 Sep 2012
See odeset, (specifically the ODE Events Property) and Event Location and Advanced Event Location for details.
The example in Advanced Event Location seems to be able to do what you want. You will have to adapt the events function for the orbitode example, but it seems to perform the sort of detection you describe.
I haven't used the 'Events' option myself recently, so I leave you to experiment with it.

1 Comment

I am also not familiar with odeset. Let me take a look at it. Thanks for the suggestion.
Lakshmi

Sign in to comment.

Asked:

on 28 Sep 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!