Animate a pendulum: Difference between revisions

m
{{out}}
m (→‎{{header|Tcl}}: ==={{libheader|Tk}}===)
m ({{out}})
Line 2:
 
{{task|Temporal media}}{{requires|Graphics}}
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 [[wp:Pendulum|simple gravity pendulum]].
The classic such physical system is a [[wp:Pendulum|simple gravity pendulum]].
 
For this task, create a simple physical model of a pendulum and animate it.
Line 98 ⟶ 99:
end Main;</lang>
 
{{out}}
Output:
<pre> X: 5.00000E+00 Y: 8.66025E+00
X: 4.95729E+00 Y: 8.68477E+00
Line 476 ⟶ 477:
}
</lang>
This program is tested with wxWidgets version 2.8 and 2.9. The whole project, including makefile for compiling on Linux can be download from [https://github.com/orbitcowboy/wxPendulum github].
The whole project, including makefile for compiling on Linux
can be download from [https://github.com/orbitcowboy/wxPendulum github].
[[File:WxPendulumScreenshot.png]]
 
Line 672 ⟶ 675:
This simulation uses this formula directly, updating the velocity from the acceleration and the position from the velocity; inaccuracy results from the finite timestep.
 
The event flow works like this:
The event flow works like this: The ''clock'' object created by the simulation steps the simulation on the specified in the interval. The simulation writes its output to <code><var>angle</var></code>, which is a ''Lamport slot'' which can notify of updates. The ''whenever'' set up by <code>makeDisplayComponent</code> listens for updates and triggers redrawing as long as ''interest'' has been expressed, which is done whenever the component actually redraws, which happens only if the component's window is still on screen. When the window is closed, additionally, the simulation itself is stopped and the application allowed to exit. (This logic is more general than necessary; it is designed to be suitable for a larger application as well.)
The ''clock'' object created by the simulation steps the simulation on the specified in the interval.
The simulation writes its output to <code><var>angle</var></code>, which is a ''Lamport slot'' which can notify of updates.
The ''whenever'' set up by <code>makeDisplayComponent</code> listens for updates and triggers redrawing as long as ''interest'' has been expressed, which is done whenever the component actually redraws, which happens only if the component's window is still on screen.
When the window is closed, additionally, the simulation itself is stopped and the application allowed to exit.
(This logic is more general than necessary; it is designed to be suitable for a larger application as well.)
 
<lang e>#!/usr/bin/env rune
Line 2,476 ⟶ 2,484:
 
=={{header|RLaB}}==
The plane pendulum motion is an interesting and easy problem in which the facilities of RLaB for numerical computation and simulation
in which the facilities of RLaB for numerical computation and simulation
are easily accessible.
The parameters of the problem are <math>L</math>, the length of the arm, and <math>g</math> the magnitude of the gravity.
and <math>g</math> the magnitude of the gravity.
 
We start with the mathematical transliteration of the problem. We solve it in plane (2-D) in terms of <math>\theta</math> describing the angle between the <math>z</math>-axis and the arm of the pendulum, where the downwards direction is taken as positive.
We solve it in plane (2-D) in terms of <math>\theta</math> describing
The Newton equation of motian, which is a second-order non-linear ordinary differential equation (ODE) reads
the angle between the <math>z</math>-axis and the arm of the pendulum,
where the downwards direction is taken as positive.
The Newton equation of motianmotion, which is a second-order non-linear ordinary differential equation (ODE) reads
ordinary differential equation (ODE) reads
:<math>\ddot\theta = -\frac{g}{L} \sin \theta</math>
In our example, we will solve the problem as, so called, initial value problem (IVP). That is, we will specify that at the
initial value problem (IVP).
That is, we will specify that at the time ''t=0'' the pendulum was at rest <math>\dot\theta(0)=0</math>, extended at an angle <math>\theta(0)=0.523598776</math>
radians (equivalent to 30 degrees).
 
RLaB has the facilities to solve ODE IVP which are accessible through ''odeiv'' solver. This solver requires that the ODE be written as the first order differential equation,
be written as the first order differential equation,
:<math>\dot u = f(u) </math>
Here, we introduced a vector <math>u = [\theta, \dot\theta] = [u_1, u_2]</math>, for which the original ODE reads
for which the original ODE reads
:<math>\dot\theta = \dot u_1 = u_2 = f_1(u)</math>
:<math>\ddot\theta = \dot u_2 = -\frac{g}{L} \sin \theta = -\frac{g}{L} \sin u_1 =f_2(u)</math>.
Line 2,562 ⟶ 2,577:
 
{{trans|Tcl}}
This does not have the window resizing handling that Tcl does --
 
I did not spend enough time in the docs to figure out
This does not have the window resizing handling that Tcl does -- I did not spend enough time in the docs to figure out how to get the new window size out of the configuration event. Of interest when running this pendulum side-by-side with the Tcl one: the Tcl pendulum swings noticibly faster.
how to get the new window size out of the configuration event.
Of interest when running this pendulum side-by-side with the Tcl one:
the Tcl pendulum swings noticibly faster.
 
<lang ruby>require 'tk'
Anonymous user