Mathematica demos for teaching

by Oliver Knill

2. Interactive example with dynamics

This simple example simulates a free gas under gravitational influence in a rectangular container. Click into the box to create new particles.
h[x_]:=If[Abs[x]<1,1,-1];
f[{{a_,b_},{v_,w_}}]:={{a+h[a] v,b+h[b] w},{h[a] v,(h[b]-0.0001)w-0.001}};
DynamicModule[{c={}}, r:=0.03*(Random[]-1/2);
  EventHandler[Graphics[{RGBColor[1,0,0],PointSize[0.05],
    Point[Dynamic[c=Map[f,c]; Map[First,c]]]},PlotRange ->{{-1,1},{-1,1}},
    Background -> RGBColor[0.9,0.9,1],ImageSize->500],
    "MouseDown" :> (AppendTo[c,{MousePosition["Graphics"],{r,r}}])]
]