[Math] How does MATLAB Simulink get such accurate ODE solution results

ordinary differential equationssimulink

I have a fixed-step model (0.001s) in Simulink in which a second order ODE is effectively being set up, a free spring-mass-damper system (m=2, c=0.3, k=1, x0=0, x_dot_0=4).

I am studying the solvers and in particular the implementation of the fourth order Runge-Kutta method (ODE4). I have the analytical function generating the exact solution for comparison, and am finding that the ODE4 solver to be much more accurate over the first 50 seconds (50000 steps) of solution than a hand-implemented version coded once into an excel spreadsheet and again into a C++ application.

The Simulink model deviates by around 10^-14 from the exact solution, essentially the accuracy of the double precision value. A hand-implemented RK4 solution is getting only 10^-2 accurate.

Does anyone have experience with this and know what Simulink is doing to get accuracy beyond that which I can get by hand? I know it potentially uses sub-steps for greater accuracy though I have tested using 0.000001s steps and only got to 10^-5 accuracy and I am sure it can't be using anywhere near that step size.

It is possible I have not implemented the RK formula correctly.

Best Answer

So it turns out, as happens, that it was a mathematical error in the implementation that yielded a very small absolute error, related to the presence and absence of the time step multiplier on several terms.

The first error was to miss multiplying the small state deltas in the second, third and fourth tangent calculations for the second state. More importantly I had combined two varying definitions of the method, resulting in one too many multiplications by the time step when adding the slope delta to the current state values.

The warning here I suppose is to ensure a consistent set of equations is sourced while developing the method. I had drawn from several varying sources.

Related Question