Model-based design in Julia: Bouncing ball
Published:
Introduction
In this article I am investigating how to model the “bouncing ball” example: a classic example of a hybrid dynamic system
Theory
This model is very simple and based on the example at DifferentialEquations.jl
We have the position and velocity of the ball defined as state u1 and u2. The velocity is the time derivative of the position and the acceleration (time derivative of the velocity) is equal to the gravitational constant.
Next to these ODE’s (Ordinary differential equations), we also need to specify the logic for when the ball hits the ground. When the ball hits the ground, the speed of the ball is inverted taking into account the “coefficient of restituation” (p2)
Implementation in Julia
We can model this using the functions provided by DifferentialEquations.jl
function f(du, u, p, t)
du[1] = u[2]
du[2] = -p[1]
end
function condition(u, t, integrator) # Event when condition(u,t,integrator) == 0
u[1]
end
function affect!(integrator)
integrator.u[2] = -integrator.p[2]*integrator.u[2]
end
using DifferentialEquations
cb = ContinuousCallback(condition, affect!)
u0 = [1.0, 0.0]
tspan = (0.0, 4.0)
p = [9.8, 0.8]
prob = ODEProblem(f, u0, tspan, p)
sol = solve(prob, Tsit5(), callback = cb)
using Plots
gr()
plot(sol,xaxis = "Time (t)", title="Bouncing ball",label = ["position [m]" "velocity [m/s]"])