# 弹跳球¶

## 对弹跳球的建模¶

$v_\text{final} = -e v_\text{initial}$

model BouncingBall "The 'classic' bouncing ball model"
type Height=Real(unit="m");
type Velocity=Real(unit="m/s");
parameter Real e=0.8 "Coefficient of restitution";
parameter Height h0=1.0 "Initial height";
Height h;
Velocity v;
initial equation
h = h0;
equation
v = der(h);
der(v) = -9.81;
when h<0 then
reinit(v, -e*pre(v));
end when;
end BouncingBall;


equation
v = der(h);
der(v) = -9.81;
when h<0 then
reinit(v, -e*pre(v));
end when;


when（当）语句由两部分组成。第一部分是条件表达式，表示事件发生的时刻。在本例中，事件发生在“当”高度h第一次降低到0以下时。when表达式的第二部分描述了在事件出现时所发生的事情。在本例中，v的值被reinit（重新初始化）操作符重新初始化了。我们可以用reinit操作符为状态设定新的初始化条件。从概念上讲，你可以认为reinit当成是在仿真过程中插入的initial equation。但是，reinit只会改变一个变量的值，而且这种改变总是显式的（也就是说它没有initial equation那么灵活）。在本例中，reinit语句会将v的值重新初始化，使之与在碰撞前的v（也就是pre(v)）方向相反，大小变为原来的e倍。

## 数值精度¶

model StableBouncingBall
"The 'classic' bouncing ball model with numerical tolerances"
type Height=Real(unit="m");
type Velocity=Real(unit="m/s");
parameter Real e=0.8 "Coefficient of restitution";
parameter Height h0=1.0 "Initial height";
constant Height eps=1e-3 "Small height";
Boolean done;
Height h;
Velocity v;
initial equation
h = h0;
done = false;
equation
v = der(h);
der(v) = if done then 0 else -9.81;
when {h<0,h<-eps} then
done = h<-eps;
reinit(v, -e*(if h<-eps then 0 else pre(v)));
end when;
end StableBouncingBall;


  when {h<0,h<-eps} then
done = h<-eps;
reinit(v, -e*(if h<-eps then 0 else pre(v)));
end when;


when {a>0, b>0} then
...
end when;


when {a>0, b>0} then
...
end when;

when a>0 or b>0 then
...
end when;


when {time>1, time>2} then
...
end when;

when time>1 or time>2 then
...
end when;


when {time>1, time>2} then
...
end when;


when time>1 or time>2 then
...
end when;