4 ms·
Double Pendulum in fewer than 100 lines of JavaScript
- javajosh 11y agoThis is good work, and should be within the capability of any upper-division physics major. I think that the important point is using the Euler method for the DEs, rather than attempt to find some closed-form theta(t).
- amk_ 11y agoUsually these types of simulations require that you don't use Euler approximation.
- tlarkworthy 11y agoThis pendulum will lose energy because Euler is not a good integration scheme if you value accuracy. RK4 is almost as easy to implement but dramatically higher accuracy. Fixed step sizes will either be inefficient or lose energy around around critical points in the dynamic system. RK with adaptive stepping solve all these problems and leads to a more efficient use of computational resources. You will probably be able to leave the simulation undisturbed for days, and it will still be swinging merrily. https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods Such methods are already wrapped up in the ODE solver of numerics.js http://www.numericjs.com/documentation.html http://www.numericjs.com/documentation.html (see dopri)
- drdeca 11y agoWould it somewhat work to store the initial potential energy, and when running each step, correct one (or more?) of the values so as to make the energy of the system correct, while making the smallest change? I mean, I figure that would make it so it doesn't lose energy, but I don't know if that would make the simulation significantly worse in other ways.
- amk_ 11y agoWhere would you put the accumulated error? Ultimately you end up creating something like the 4th order Runge-Kutta mentioned mentioned above, which in a sense 'correct' the Euler solution by including the effects of higher-order terms. https://en.wikipedia.org/wiki/Numerical_methods_for_ordinary_differential_equations#Generalizations https://en.wikipedia.org/wiki/Numerical_methods_for_ordinary...
- drdeca 11y agoI was thinking that, instead of ensuring that the corrections are in the right direction, that they just be as small as possible such that the total energy remains constant. So, I was thinking that it would make the results worse in some ways, but keep the total energy constant by just constraining the total energy, and using that to e.g. change the angle or rate of rotation of one of the two parts. So, e.g., calculating the values as this does, and then from the initial energy, and all the values except for the rate of rotation of the lower part, calculate the rate of rotation for the lower part (if multiple values for the rate of rotation would give the same KE , choose the one which is closest to the the one calculated at first). It wouldn't really be 'correct' in the differential equations thing, it would just be forcing the total energy to be correct? So, I figure it would make it so it doesn't eventually slow down, but might make the other bits be quite inaccurate?