Part F : A few Notes on Programming Aspects

Runge-Kutta method

A trebuchet's moving components are a large beam, a counter-weight and the object to be slung across a certain distance. Consideration of Newton's and Euler's laws of motion one arrives at second order differential equations which are for a trebuchet so complicated that they can not be solved analytically.

A simple example of a single second order differential equation would be :

\begin{equation*} \ddot{y} = - g \end{equation*} which describes the vertical motion of an object falling under the influence of gravity in the absence of air resistance. y is the distance from earth's surface and is assumed to be small in comparison to the radius of earth. One has to provide some what is called initial condition by saying - for example that at time t=0 the height of the object under consideration and its velocity are such and such.

For this simple example the analytical solution is known :

\begin{equation*} y = h - 1/2 g t^2 \end{equation*}

which not only satifies the differential equation but also the condition of y = h and zero velocity at time t=0.

Numerical methods like the here employed Runge-Kutta method can find approximations to the "true" solution of differential equations only if the problem is stated in terms of one or more simultaneous first order differential equations. Converting a second order differential equation to two first order equations involves a little "magic" trick which is and times also used in solving non-linear differential equations. You simply define a new variable, call it z for example, which is equal to the first derivative of y which in this case is the velocity of the falling object.

\begin{align*} \dot{y} &= z \\ \dot{z} &= -g \end{align*}

subject to the initial condition of y(t=0) = 0 and z(t=0) = 0.

In terms of proramming it is of advantage to employ a function (fixed set of code lines) to advance a set of n first order differential equations where the number of such equation can be chosen without having to re-code the function. To make that possible we don't invent individual names to the dependent variable, like y and z in the above example but have an array of n elements, Y[] in our case such that Y[0] corresponds to y(t) and Y[1] to z(t).

In case of the trebuchet we have three second order differential equations one for each of the angles α, β and γ, see the image below.

Because the dynamic equations are second order the derivative of the above mentioned three angles appear in vector Y[] as well.

Y[0] = \(\alpha\) = angle between beam axis and vertical. 90° when beam is horizonatal
Y[1] = \(\dot{\alpha}\) = derivative of alpha w.r.t. time
Y[2] = \(\beta\) = angle between weight and vertical
Y[3] = \(\dot{\beta}\) = derivative of beta w.r.t. time
Y[4] = \(\gamma\) = angle between rope to pumpkin and vertical
Y[5] = \(\dot{\gamma}\) = derivative of gamma w.r.t. time

In our program ( in javascript) we used the standard Runge-Kutta method of 4th order a standard work horse for many (but not all) applications. see Wikipedia

Trebuchet HomePage Trebuchet-Math/Physics Background Part A : Dynamics of counter-weight
Part B : Dynamics of pumpkin Part C : Dynamics of Beam Part D : Sling Finger Analysis
Part E : Stress Analysis Part F : A few Notes on Programming Aspects

Zig Herzog;
Last revised: 03/06/20