
One good way of making an animation is by simulating a physical system and illustrating the variables in that system using a dynamically changing graphical display.

The classic such physical system is a simple gravity pendulum.



Task

Create a simple physical model of a pendulum and animate it.




>g=gearth$; l=1m;
>function f(x,y) := [y[2],-g*sin(y[1])/l]
>function h(a) := ode("f",linspace(0,a,100),[0,2])[1,-1]
>period=solve("h",2)
 2.06071780729
>t=linspace(0,period,30); s=ode("f",t,[0,2])[1];
>function anim (t,s) ...
$  setplot(-1,1,-1,1);
$  markerstyle("o#");
$  repeat
$  for i=1 to cols(t)-1;
$    clg; 
$    hold on;
$    plot([0,sin(s[i])],[1,1-cos(s[i])]);
$    mark([0,sin(s[i])],[1,1-cos(s[i])]);
$    hold off;
$    wait(t[i+1]-t[i]);
$  end;
$  until testkey();
$  end
$endfunction
>anim(t,s);
>
