How to solve differential equations on an arduino with the runge-kutta method

This is just a small update on my experiments with the Arduino. I implemented the runge-kutta-method for solving a multibody system a few weeks ago. So this is a working implementation of the standard 4th-order runge-kutta ODE (ordinary differential equations) solver for the arduino platform, something I haven’t seen elsewhere.

(Note: If you are just looking for the arduino sketch, you’ll find a link at the end of this blog post. If you are wondering about related questions or have trouble adopting it to your needs, please leave a comment.
Update in August 2016: See also my new post on achievable simulation rates with an Arduino Uno/Nano and Due)

My main goal was to get a better grip on simulation speeds. Why is my simulation so slow? is a question I’m really thinking about a lot. Since I wasn’t able to find a good example of an arduino sketch for ODE solving online, this may be interesting anyway.

The sketch can be used to simulate systems of ordinary differential equations (ODEs) in state form:

arduino_sketch_ODE

I currently include a simple 2-mass multibody system (→ 4 state variables) but this can be changed easily. My Arduino Uno achieves something around 1000 simulation steps per second which is less than I expected. This seems to result mainly from the many floating point operations the sketch requires (each +, – or * with floating point numbers uses approx. 100 CPU cycles).

For comparison: The same code compiled in C++ on my laptop achieves around 2 million simulation steps per second. Also C++ allows me to use double precision for the variables, resulting in smaller rounding arrors (arguably a significant difference for some simulations).

Nonetheless, the arduino sketch is still fast enough if you want to do some simple physics modelling. For example something like:

  • take analog input from a human,
  • have the arduino simulate a physical system and
  • output the behavior to either a screen or an actuator.

(Update: I now did exactly that – see the update here)

Here’s the link to the arduino sketch (updated version from August 2016).

Update on April 27th 2015: Fixed some minor grammar issues and also changed the caption.
Update on Feb. 26th 2016: The link to the sketch at least didn’t work for me. –> now updated.
Update on May. 7th 2016: Added some clarifications in the introduction, making it easier to find the code. I’m currently looking at ODE solvers in C++, mainly the odeint and GSL packages, so maybe there will be an update some time in the future.
Update on Aug. 24th 2016: Updated the arduino sketch and removed the bonus link (blog no longer active).

Advertisements

2 thoughts on “How to solve differential equations on an arduino with the runge-kutta method

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s