Classic Runge-Kutta method implemented in C. Let's see how to use it to run a simulation. Classic Runge-Kutta or RK4 is the most known method of the Runge-Kutta family. We can use it to simulate the evolution of a dynamic system, as an alternative to more basic methods like Euler. Let's take the simple model of a car described in https://buff.ly/3fsMaGL and refreshed in the picture below. We have shown that its motion is described by: dv/dt = (F - b * v^2) / m The RK4 provides an approach to compute y(t_n+1) starting from the knowledge of y_n and f(t, y) = dv(t)/dt. Here's the method: y(t_n+1) = y_n + T*(k1 + 2*k2 + 2*k3 + k4)/6, where t_n+1 = t_n + T Also: k1 = f(t_n, y_n) k2 = f(t_n + T/2, y_n + T*k1/2) k3 = f(t_n + T/2, y_n + T*k2/2) k4 = f(t_n + T, y_n + T*k3) In the picture below you can find: - A recap of the method - The model of the car above implemented in C with forward Euler and with the classic Runge-Kutta method - The result of a simulation that compares the continuous model implemented in Xcos (max integration time 0.01s) and the two implementations above (using T = 5 seconds) The parameters used are: m = 1000 kg b = 1 N*s^2/m^2 I limited the integration time in Xcos to 0.01s so that the result is very accurate and I chose T = 5s to push the two iterative methods to be in trouble, to see which one performs better. In this case, we can observe from the simulation result that RK4 is a lot more accurate (closer to Xcos) than Euler. A clarification on the implementation of the functions in C. The transient nature of the system is obtained by running the functions N times to generate N values of v, starting from N values of F. For example: v[i] = Car_FE(F[i], 1000, 1, 5); where i = 0 to N-1. If you enjoyed this follow me for more tips on control and embedded software engineering. Hit the 🔔 on my profile to get a notification for all my new posts. #controlsystems #embeddedsystems #softwareengineering #embeddedsoftware #controltheory #coding
Thanks for sharing..Excellent Work
By scilab ode() in-built differential equation function 'y=ode(y0,x0,x,f)' deff('yprim=f(t,v)','yprim=(1000 -196- v*v) /1000'); t0=0; tf=140; dt=0.001; t=t0:dt:tf; v0=0; v=ode(v0,t0,t,f); plot(t,v),xgrid xlabel('$Time(s)$','fontsize',4) ylabel('$Velocity$','fontsize',4) title('$y\prime=(1000-196-v^2)/1000,\quad y(0)=0$','fontsize',4)
If you want to see Runge-Kutta RK4 in action, check out this application: https://x-engineer.com/ It simulates an electric vehicle on a drive cycle.
Nice. I haven't used this method since the university. But it's great to refresh it again.
Thanks for sharing
I use Runge- Kutta method to solve my system of ODE’s on daily basis at work. More stable then Euler method.
Next step - N body simulation using Runge Kutta :)
Nice example One of the best method used in simulation
It's one of the best methods used in simulation; keep up the good work.
Product Manager - Transmission Lines at Svenska kraftnät
3yAdam Lind, M.Sc., P.Eng. …how about a walk down the memory lane? ;)