You don't really need Simulink.
Many simulations can be implemented using any programming language.
Let's see an example in C.
In one of my previous posts (link in the first comment) I showed how to obtain the differential equation of a pendulum and how to implement it in Xcos / Simulink.
The picture below shows a recap of the pendulum dynamics and its differential equation:
ddtheta_ddt = (tau - m*g*l*sin(theta) - k*dtheta_dt) / (m*l^2)
If we wanted to implement this equation in Xcos / Simulink we should feed ddtheta_ddt through two integrators to produce dtheta_dt and theta, which would then be used again in the calculation of ddtheta.
We have seen how to do it in the post linked in the first comment.
This “trick“ that uses integrators to solve the differential equation iteratively is one of the basics of this modelling approach.
To implement such an approach in a programming language, we need an iteration (a “while loop“ for example) that runs a number of times equal to the steps that we want to simulate.
To simulate 20 seconds with a time step of 0.1 seconds, we need to iterate 200 times.
In this case, at every iteration, we need to integrate over one time step as follows (T is 0.1 seconds in this example):
✅ ddtheta_ddt = (tau - m*g*l*sin(theta) - k*dtheta_dt)/(m*l^2)
✅ dtheta_dt += ddtheta_ddt*T
✅ theta += dtheta_dt*T
This method is called Forward Euler, and it's one of the simplest approaches. For better accuracy, we could use classic Runge-Kutta for example.
The picture below shows:
✅ The pendulum dynamics and its differential equation
✅ The C code that implements the integration step and the while loop
✅ The simulation result of the response to a 3 Nm torque step applied to the system, both in Xcos (with a 0.01s integration time) and in C (with a 0.1s time step)
Values used:
✅ m = 0.5 kg
✅ l = 1 m
✅ k = 0.5 Nm*s
✅ g = 9.81 m/s^2
We can observe that the qualitative response of both simulations is very similar, with the C code one a little ahead of the Xcos one. This is probably due to the Forward Euler approach.
My opening “you don't really need Simulink“ was a bit of a provocation, as Simulink is a great tool and offers the possibility to easily implement much more complex stuff than this.
Yet, most of the things we do with Simulink could be done otherwise, and I wanted to highlight how a simulation works under the surface.
I'm planning to use this system in future posts to show control examples, implementations of the model in C code using different methods for temporal discretisation and more.
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.
Feel free to ask anything in the comments, I'll do my best to answer.
#controlsystems #embeddedsystems #softwareengineering #embeddedsoftware #coding #controltheory
Many simulations can be implemented using any programming language.
Let's see an example in C.
In one of my previous posts (link in the first comment) I showed how to obtain the differential equation of a pendulum and how to implement it in Xcos / Simulink.
The picture below shows a recap of the pendulum dynamics and its differential equation:
ddtheta_ddt = (tau - m*g*l*sin(theta) - k*dtheta_dt) / (m*l^2)
If we wanted to implement this equation in Xcos / Simulink we should feed ddtheta_ddt through two integrators to produce dtheta_dt and theta, which would then be used again in the calculation of ddtheta.
We have seen how to do it in the post linked in the first comment.
This “trick“ that uses integrators to solve the differential equation iteratively is one of the basics of this modelling approach.
To implement such an approach in a programming language, we need an iteration (a “while loop“ for example) that runs a number of times equal to the steps that we want to simulate.
To simulate 20 seconds with a time step of 0.1 seconds, we need to iterate 200 times.
In this case, at every iteration, we need to integrate over one time step as follows (T is 0.1 seconds in this example):
✅ ddtheta_ddt = (tau - m*g*l*sin(theta) - k*dtheta_dt)/(m*l^2)
✅ dtheta_dt += ddtheta_ddt*T
✅ theta += dtheta_dt*T
This method is called Forward Euler, and it's one of the simplest approaches. For better accuracy, we could use classic Runge-Kutta for example.
The picture below shows:
✅ The pendulum dynamics and its differential equation
✅ The C code that implements the integration step and the while loop
✅ The simulation result of the response to a 3 Nm torque step applied to the system, both in Xcos (with a 0.01s integration time) and in C (with a 0.1s time step)
Values used:
✅ m = 0.5 kg
✅ l = 1 m
✅ k = 0.5 Nm*s
✅ g = 9.81 m/s^2
We can observe that the qualitative response of both simulations is very similar, with the C code one a little ahead of the Xcos one. This is probably due to the Forward Euler approach.
My opening “you don't really need Simulink“ was a bit of a provocation, as Simulink is a great tool and offers the possibility to easily implement much more complex stuff than this.
Yet, most of the things we do with Simulink could be done otherwise, and I wanted to highlight how a simulation works under the surface.
I'm planning to use this system in future posts to show control examples, implementations of the model in C code using different methods for temporal discretisation and more.
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.
Feel free to ask anything in the comments, I'll do my best to answer.
#controlsystems #embeddedsystems #softwareengineering #embeddedsoftware #coding #controltheory