Thread Subject: Can you use the events function more than once?

Subject: Can you use the events function more than once?

From: ill will

Date: 19 Feb, 2009 18:55:03

Message: 1 of 2

Can I use the events function more than once?
Let's take the ballode demo for example. The events function is used to detect when the ball hits the ground. What if I also wanted to add another events function to detect something else? Maybe I also want to know when the ball is at a height of 0.5. This would mean I would have two different events functions for the same ode solver in the for loop.

function ballode
%BALLODE Run a demo of a bouncing ball.
tstart = 0;
tfinal = 30;
y0 = [0; 20];
refine = 4;
options = odeset('Events',@events,'OutputFcn', @odeplot,...
                 'OutputSel',1,'Refine',refine);
tout = tstart;
yout = y0.';
teout = [];
yeout = [];
ieout = [];
for i = 1:10
  % Solve until the first terminal event.
  [t,y,te,ye,ie] = ode23(@f,[tstart tfinal],y0,options);
  if ~ishold
    hold on
  end
  % Accumulate output.
  nt = length(t);
  tout = [tout; t(2:nt)];
  yout = [yout; y(2:nt,:)];
  teout = [teout; te]; % Events at tstart are never reported.
  yeout = [yeout; ye];
  ieout = [ieout; ie];

  ud = get(gcf,'UserData');
  if ud.stop
    break;
  end
  
  % Set the new initial conditions, with .9 attenuation.
  y0(1) = 0;
  y0(2) = -.9*y(nt,2);

  % A good guess of a valid first time step is the length of
  % the last valid time step, so use it for faster computation.
  options = odeset(options,'InitialStep',t(nt)-t(nt-refine),...
                           'MaxStep',t(nt)-t(1));
  tstart = t(nt);
end

% --------------------------------------------------------------
function dydt = f(t,y)
dydt = [y(2); -9.8];
% --------------------------------------------------------------
function [value,isterminal,direction] = events(t,y)
% Locate the time when height passes through zero in a
% decreasing direction and stop integration.
value = y(1); % Detect height = 0
isterminal = 1; % Stop the integration
direction = -1; % Negative direction only

Subject: Can you use the events function more than once?

From: Steven Lord

Date: 20 Feb, 2009 15:13:16

Message: 2 of 2


"ill will" <schoolsofthought@gmail.com> wrote in message
news:gnk9u7$65u$1@fred.mathworks.com...
> Can I use the events function more than once?
> Let's take the ballode demo for example. The events function is used to
> detect when the ball hits the ground. What if I also wanted to add another
> events function to detect something else? Maybe I also want to know when
> the ball is at a height of 0.5. This would mean I would have two different
> events functions for the same ode solver in the for loop.

You can't do this exactly the way you described, but you can do what you
want:

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/odeset.html#f92-1017470

"value, isterminal, and direction are vectors for which the ith element
corresponds to the ith event function: "

So you can have your events function check for multiple events and update
the appropriate element of the output vectors. Your events function would
become:


function [value,isterminal,direction] = events(t,y)
% Locate the time when height passes through zero in a
% decreasing direction and stop integration.
value = [y(1), y(1)-0.5]; % Detect height = 0 or 0.5
isterminal = [1, 1]; % Stop the integration
direction = [-1, -1]; % Negative direction only


Of course, in this example, assuming the ball starts above height 0.5, only
the second event should trigger.

--
Steve Lord
slord@mathworks.com

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
events ill will 19 Feb, 2009 14:00:19
options ill will 19 Feb, 2009 14:00:19
ode ill will 19 Feb, 2009 14:00:19
ballode ill will 19 Feb, 2009 14:00:19
ball ill will 19 Feb, 2009 14:00:19
bounce ill will 19 Feb, 2009 14:00:19
collision ill will 19 Feb, 2009 14:00:19
collision detec... ill will 19 Feb, 2009 14:00:19
ode45 ill will 19 Feb, 2009 14:00:19
ode15s ill will 19 Feb, 2009 14:00:19
rssFeed for this Thread

Contact us at files@mathworks.com