Solve ordinary differential equations with events

This script solves an ordinary differential equation with events.


Updated Sat, 18 May 2019 22:28:30 +0000

View License

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

Cite As

Lateef Adewale Kareem (2023). 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

Community Treasure Hunt

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

Start Hunting!


Version Published Release Notes