Bouncing ball example
const unsigned MaxBang = 10;
Constant g(9.81);
struct Ball {
unsigned count;
class LimitY: public ConditionDown {
Ball *b;
void Action() {
b->Bang();
}
public:
LimitY(Ball * ball):
ConditionDown(ball->y),
b(ball) { }
};
LimitY ylim;
Integrator v, y;
Ball(double initialposition):
count(0),
ylim(this),
v(-g - v * 0.1),
y(v, initialposition) { }
void Bang() {
Out();
Print(
"\n# Bounce#%u\n", ++count);
v = -0.9 * v.Value();
y = 0;
if (count >= MaxBang)
else
Out();
}
void Out() {
Print(
"%f %9.4g %9.4f\n",
T.
Value(), y.Value(), v.Value());
}
};
Ball b1(10);
Print(
"# Ball2 - bouncing ball model output\n");
}