Work Experience – Dynamic Causal Modeling

What we wanted to Achieve:

Throughout my two week work experience placement at Embecosm, I worked on a project to create a dynamical system of a double pendulum. A dynamical system, meaning a system containing objects that are continuously effected by active forces caused as a result of various interacting objects. The system that was created was a double pendulum that demonstrates chaotic motion; motion that is highly sensitive to small changes within the starting values. Consequently the slightest variation in starting parameters dramatically changes the motions of the pendulum. This is significant as the python model later became a suitable demonstration of the ability of the C++, DCM (Dynamic Causal Modeling) program that it was eventually embedded into. The purpose of this DCM program was to make predictions on the position of one of the pendulums rods based on limited and purposely inaccurate knowledge of the other.

The GitHub repository can be found here and some background material here.

Baseline double pendulum animation.

What we Tried First:

The first thing I want to outline out of all the things we tried was creating a model of orbiting planets. This began with a discussion about how dynamical systems similar to a double pendulum or the orbit of planets can be simulated with William and familiarizing myself with other examples of chaotic systems. However, when we tried to simulate the orbit of three planets it just produced a chaotic but wrong diagram of small dots representing planets. Following this I lightly researched other dynamical systems including population growth, movement of fluids, rational individuals playing a negotiation game and the swinging of a pendulum. As a result understanding how these models could be reproduced as a visualization in greater depth.

Creating and Integrating the Model:

Subsequently we began creating a different model, that was intended to produce a simulation of a double pendulum, resulting in an animation that appeared to move realistically by respecting the constraints set by the length of each of the rods. However, the difference in the location of each consecutive position of the pendulum was remarkably large, which was later discovered to be caused by subtle logic errors that presumably were a result of side effects when manipulating the current state. Nonetheless these were ironed out the following day with the help of William and small adjustments were made to several values that effected how the animation of the double pendulum updated it’s position and as a result improved the quality of the pendulum simulation. Creating a simple yet functional simulation of a double pendulum was produced that would be the baseline for repeated refinement and later be adjusted to create various versions. The first of these versions being a variation of the model that updated its position in real-time decreasing the time spent waiting for the program to run and hence making it easier to test the expected motions of the pendulum. Another variation of this baseline program was a demonstration of the impact caused by slightly different characteristics within several pendulums beginning from identical starting positions, making each pendulum behave differently. This illustrated how unpredictable and chaotic the motions of the pendulum are, since seemingly insignificant changes have a dramatic impact on the pendulums motions.

For that reason any attempt a reproducing the initial parameters of a pendulum need to be both precise and accurate to achieve a similar result. Which became significant when the Python program was integrated with a C++ program written by William, using dynamic causal modeling to predict the actual position of the pendulum based off some distorted starting parameters.

Comparison of the pendulum produced from the integrated program (Blue), against the True (Green) and Distorted (Orange) values.


After running through all this work, we saw some success, with the following results. The creation of different variations of a pendulum simulation that originated from a simple baseline model. These include a visualization that updates the pendulums location in real-time and a multi-pendulum simulation where several pendulums with slight variation in the starting values cause differences that can be observed. Also, the incorporation of these models into a C++ program that was then capable of making accurate predictions of the original pendulum based off limited and distorted starting values was remarkable successful.


The project did eventually work as intended, however there were an abundance of challenging moments, which are summarized here. The first was a result of some confusion with the conversion between degrees and radians causing interesting results when expecting a particular starting value that appeared radically different when tested. Others happened when attempting to embed Python within C++, in particular the repeatedly vague and ambiguous error messages of C++ and continuous segmentation faults; caused by attempting to access fictitious memory locations, which I quickly and unfortunately became familiar with as we attempted to find the cause using GDB, a debugger for C++.

Caused by a bug making the location of y in the first weight the same as x


Despite a functioning model, the limited two weeks spent on this project inevitably meant there are limits in its function, the most significant being the amount of time taken for the simulation to calculate the location of each rod as it swings. This is because the current version takes a significant amount of time for the program to calculate relatively exact figures for a visualization that lasts a duration where you can see different chaotic movements. Moreover, the pendulum appears to have slightly less precision cause by a limiting refresh rate (interval) between each consecutive frame meaning the movements are smooth but inexact, consequently making a less pleasing animation. Nevertheless, we attempted to solve this issue by initially profiling the program and changing the data type that was constantly being manipulated, in order to be able to store the positions of each rod, this resulted in a simpler program and only a slight increase in speed. However, these could be completely fixed with further changes to the program, which would have the impact of making both the calculation of the values and the plotting animation significantly quicker. Therefore, decreasing the impact of these limitations as the programs boundaries would be higher. Additionally other future implementations include the plotting of separate graphs that demonstrate irregular motions by plotting either the change in the angles of each rod by each other, or the change in angular velocity of the second rod. Furthermore, there could be additional versions of the pendulum that perhaps allow for external forces to change the pendulums environment (friction).


To summarize, The original baseline double pendulum simulation; written in Python, was embedded within a C++ program that created an almost identical pendulum to the original (with some tolerance), from a belief in the accuracy of distorted starting values. Moreover, this baseline pendulum was also adapted to update in real-time and to show a comparison of the differences in movements of similar pendulums, successfully showing how chaotic the motions are.