Show HN: Simple pendulum simulation with rational trigonometry
grondilu.github.io
It's not obvious we get any FPS gain, but still I find it cool to even be able to get the same result differently.
grondilu.github.io
It's not obvious we get any FPS gain, but still I find it cool to even be able to get the same result differently.
This thinking also applies to orbital simulations -- in an elliptical orbit, an object's radius from the central mass, and its velocity, are constantly changing, but (barring friction) its energy, the sum of GPE and KE, must be a constant.
Not necessarily so -- many (most) discrete integrators do not exactly conserve the Hamiltonian. In fact, large classes of geometric integrators yield noticeable (but bounded) amounts of 'noise' on top of the true Hamiltonian.
> Not necessarily so
But the "constant sum" I refer to is the sum of gravitational potential energy and kinetic energy in the system. If that were not true, if the sum were not a constant (and remembering this is an ideal model without friction), then energy would not be conserved. So, yes, necessarily so.
(This is in contrast to Physics 101 approach of a free body diagram where you add up a bunch of forces on an object. In particular the 'constraints' that the system faces (in this example, it's that the pendulum is confined to moving along a circle) are implicitly included in the parametrization of the system and so there's no need to ever consider directly the force of the bar of the pendulum that holds the weight in place.)
The first example on the page parametrizes the pendulum in the 'obvious' way, by noting that you know the state of a pendulum if you know what angle the weight forms relative to the vertical (plus also the rate at which that angle is changing). This is nice, but the solution involves trigonometric functions.
The second one parametrizes the same system in a different way. Here the mu parameter is, roughly speaking, the height of the weight above the minimum (but not really). It still specifies where the pendulum is (the equation M(mu) = ... tells you the (x,y) coordinates of the pendulum's weight for a particular mu), so we can still calculate the energy of the pendulum in terms of mu. Energy has two components, kinetic and potential. Potential is the just height from 0 times mass times gravitational constant g. The kinetic is mv^2/2 and the speed v can be determined by using the parametrization above M(mu)= .... The end result is a different means of expressing the same system, but interesting in a manner that does not involve trigonometric functions despite being 'so obviously' trigonometric in nature.
One might hope that eliminating a trig function would speed up simulation. I think it's clear that one would need a more demanding simulation to make the difference noticeable.
I think this is a fun little project and it was nice to see it introduce me to a new concept (rational trig).
Edit: addendum
How exactly does one do ordinary calculus with only rational numbers?
In fact, part of the appeal I find in rational trigonometry is that it should be easier to adapt for computing, since as you pointed out, computers can not represent irrational numbers. Yet trigonometric functions usually return irrational numbers, and a computer can't do better than approximate them.
On the contrary, it is possible to do exact arithmetic on rational numbers. Granted, you can't do that with floating points, though.
PS. To be clear: although the numbers actually used in the code are floating point numbers, the equations have a purely algebraic form (no sine, cosine or sqrt), so it should at least in principle be possible to use a Rational number type instead, and the computation would then be exact. The only approximation would be due to the Runge-Kutta algorithm.
One that I like for doing exact arithmetic on numbers represented by black-box functions is a continued fraction representation, see http://perl.plover.com/yak/cftalk/INFO/gosper.txt
(Note: floating point is successful because trying to use irrational numbers in computations is most of the time a huge waste of computational resources compared to approximating with a binary fraction.)
If you’re using floating point, you can use the number ∞ for this. You’ll need some special cases in the code for it.
Cf. https://en.wikipedia.org/wiki/Stereographic_projection
https://en.wikipedia.org/wiki/Tangent_half-angle_substitutio...
I have no idea what could be done to avoid it.
var now = Date.now()/1000;
var dt = now - then;
Adding this line fixes it: dt = dt > 1 ? 1 : dt;
[1] https://github.com/grondilu/grondilu.github.io/blob/47d41d/p...> The callback rate may be reduced to a lower rate when running in background tabs or in hidden <iframe>s in order to improve performance and battery life.
[1] https://developer.mozilla.org/it/docs/Web/API/window/request...
Your page contains a typo, however: “The Euler-Lagrance equation then gives...”
There's a specific definition of the set of rational numbers which doesn't quite mean what I think you meant when you include it in your title. I mean, it's your code, you decide what you call it but it will most likely confuse others.