Main Content

Validate Simulink Model Using Symbolic Math Toolbox

This example shows how to model a bouncing ball, which is a classical hybrid dynamic system. This model includes both continuous dynamics and discrete transitions. It uses the Symbolic Math Toolbox™ to help explain some of the theory behind ODE solving in theSimulink® Model of a Bouncing Ball.

Assumptions

  • The ball rebounds with no angle

  • There is no drag

  • Height at time t=0 is 10 m

  • Thrown with upwards velocity of 15 m/s

Derivation

The coefficient of restitution is defined as

C r = v b - v a / u a - u b

where v is the speed of object before impact and u is the speed after.

We split the second order differential equation

d 2 h d t 2 = - g

into

d h d t = v discretized as h ( t + Δ t ) - h ( t ) Δ t = v ( t )

and

d v d t = - g discretized as v ( t + Δ t ) - v ( t ) Δ t = - g

We will use basic 1st order numerical integration using forward Euler.

h ( t + Δ t ) = h ( t ) + v ( t ) Δ t

v ( t + Δ t ) = v ( t ) - g Δ t

Solve the Problem Analytically

Using the Symbolic Math Toolbox, we can approach the problem analytically. This allows us to solve certain problems more easily, such as determining precisely when the ball first hits the ground (below).

Declare our symbolic variables.

symsgtH(t)h_0v_0

Split the second order differential equation d 2 h d t 2 = - g into d h d t = v and d v d t = - g .

Dh = diff(H); D2h = diff(H, 2) == g
D2h(t) =

2 t 2 H ( t ) = g

Solve the ODE usingdsolve:

eqn = dsolve(D2h, H(0) == h_0, Dh(0) == v_0)
eqn =

g t 2 2 + v 0 t + h 0

参数化探索密苏里州的抛物线轮廓tion usingsubs:

eqn = subs(eqn, [h_0, v_0, g], [10, 15, -9.81])
eqn =

- 981 t 2 200 + 15 t + 10

Find the time at which the ball hits the ground by solving for zero:

assume(t > 0) tHit = solve(eqn == 0)
tHit =

20 5 26 109 + 500 327

Visualize the solution:

fplot(eqn,[0 10]) ylim([0 25])

Figure contains an axes object. The axes object contains an object of type functionline.

Format your exact results using variable precision arithmetic withvpa:

disp(['The ball with an initial height of 10m and velocity of 15m/s will hit the ground at 'char(vpa(tHit, 4))' seconds.'])
The ball with an initial height of 10m and velocity of 15m/s will hit the ground at 3.621 seconds.

Solve the Problem Numerically

Setup Simulation Parameters

Properties of the ball

c_bounce = .9;% Bouncing's coefficient of restitution; perfect restitution would be 1

Properties of the simulation

gravity = 9.8;% Gravity's acceleration (m/s)height_0 = 10;% Initial height at time t=0 (m)velocity_0=15;% Initial velocity at time t=0 (m/s)

Declaring the simulation timestep

dt = 0.05;% Animation timestep (s)t_final = 25;% Simulate period (s)t = 0:dt:t_final;% TimespanN = length(t);% Number of iterations

Initialize simulation quantities

h=[];% Height of the ball as a function of time (m)v=[];% Velocity of the ball (m/sec) as a function of time (m/s)h(1)=height_0; v(1)=velocity_0;

Simulate the bouncing ball (we will use basic 1st order numerical integration using forward Euler):

fori=1:N-1 v(i+1)=v(i)-gravity*dt; h(i+1)=h(i)+v(i)*dt;% When the ball bounces (the height is less than 0),% reverse the velocity and recalculate the position.% Using the coefficient of restitutionifh(i+1) < 0 v(i)=-v(i)*c_bounce; v(i+1)=v(i)-gravity*dt; h(i+1)=0+v(i)*dt;endend

Visualize and validate the simulation

plot(t,h,'o') holdonfplot(eqn,[0 10]) title('Height over time'25) ylim ([0])off

Figure contains an axes object. The axes object with title Height over time contains 2 objects of type line, functionline.

plot(t,v) title('Velocity over time')

Figure contains an axes object. The axes object with title Velocity over time contains an object of type line.

Validate Numerics with Analytics

Compare your analytical results with your numeric results.

As a reminder the time of impact was:

disp(['The ball with an initial height of 10m and velocity of 15m/s will hit the ground at 'char(vpa(tHit, 4))' seconds.'])
The ball with an initial height of 10m and velocity of 15m/s will hit the ground at 3.621 seconds.

From the numerical simulation we can find the closest value in the simulation when h ( t i ) 0

i = ceil(double(tHit/dt)); t([i-1 i i+1])
ans =1×33.5500 3.6000 3.6500
plot(t,h,'o') holdonfplot(eqn,[0 10]) plot(t([i-1 i i+1 ]),h([i-1 i i+1 ]),'*R') title('Height over time') xlim([0 5]) ylim([0 25]) holdoff

Figure contains an axes object. The axes object with title Height over time contains 3 objects of type line, functionline.