File Exchange

## Solve ordinary differential equations with events

version 1.0.0 (558 KB) by
This script solves an ordinary differential equation with events.

Updated 18 May 2019

% To simulate a ball bouncing off stair case after dropping from a height of
% 10meters onto a stair case starting from 5meters. Let the stair case be
% defined by the function y(x) = 5-floor(x). And the ball rolled off an edge
% at height of 10meters with velocity of 0.5m/s.

% The system of first order differential equation is:
odefun = @(t,y) [0.5; y(3); -9.8];

% Initial condition is:
initcon = [0, 10, 0];

% Time span of the simulation:
tspan = [0,20];

% Since we want the ball to bounce off the stair case, the effect condition is:
effectcon = @(t,y) [y(2) - (5-floor(y(1))),1,-1];

% The effect matrix is to recoil only the vertical velocity with restitution of 0.8:
effectmat = [1,0,0;0,1,0;0,0,-0.8];

% Solve the differential equation:
[t,y] = SolveOdeWithEvent(odefun, initcon, tspan, effectcon, effectmat);

% Generate the absisca for the stair case
x = linspace(y(1,1),y(end,1),10000);

% Animate the result.
figure
r = 0:0.01*pi:2*pi;
cx = 0.1*cos(r); cy = 0.1*sin(r);
plot(x,5-floor(x)); ; axis equal; hold on
c = plot(cx + y(1,1),cy + y(1,2) + 0.1);
axis([min(y(:,1)),max(y(:,1)),min(y(:,2)),max(y(:,2))]);
drawnow
for tt = 0.03:0.03:t(end)
xy = interp1(t,y,tt);
c.XData = cx + xy(1);
c.YData = cy + xy(2) + 0.1;
drawnow;
end

### Cite As

Lateef Adewale Kareem (2021). Solve ordinary differential equations with events (https://www.mathworks.com/matlabcentral/fileexchange/71586-solve-ordinary-differential-equations-with-events), MATLAB Central File Exchange. Retrieved .

##### MATLAB Release Compatibility
Created with R2015a
Compatible with R2012b to R2019a
##### Platform Compatibility
Windows macOS Linux