Introduction
Have you ever wondered how to turn a differential equation into a functional simulation? Understanding this approach is key to using simulation software such as Xcos or Simulink effectively. To illustrate this, I’ll walk you through the example of modeling a pendulum subjected to various forces.
The Model: Pendulum with Friction and Torque
Let’s consider a pendulum model with the following attributes:
- The rod is rigid, massless, and non-extensible.
- The object at the end can be considered a point mass.
In this example, we’re looking at a pendulum with both viscous angular friction and a motor applying torque to the joint.

Understanding the Dynamical System through Newton’s Second Law
Newton’s second law for rotational movement states that the sum of the torques equals the moment of inertia times the angular acceleration:
![]()
Here,
stands for angular acceleration.
Due to the constraints of the circular path, only the projection of the gravitational force (
) on the tangential axis contributes to the torque. Therefore, the equation becomes:
![]()
For this model,
as the rod has no mass and the object is a point mass.
Solving the Differential Equation
Now comes the most crucial stepāsolving for the highest-level derivative,
:
![]()
Having this equation at hand, you’re now ready to implement it into a simulation software like Xcos or Simulink.
Using Integrators in the Dynamic System
The next step involves feeding
through two integrators in your simulation software. This will produce
and
, which can be fed back into the equation for
.
This “trick” that leverages integrators to solve the differential equation iteratively is a cornerstone of this modelling approach.

Simulation Results
The model in Xcos was simulated using a 3 Nm torque step applied to the system.

For this model, the following values were used:
Conclusion
Understanding the power of differential equations in creating simulations of dynamical systems, like a pendulum, opens up a plethora of possibilities in control engineering. By solving for the highest-level derivative and using integrators effectively, you can model complex dynamical systems following a standard approach.
So the next time you are grappling with how to model a dynamic system, remember this basic yet powerful approach!
If you want to test the example, you can find the Scilab/Xcos source files here: https://github.com/simorxb/Pendulum-model.



This unfortunately does not work out due to some errors.
First: Why does the pendulum work on when you constantly apply a 3Nm torque? So you put in energy continously to keep up the movement, and yet the pendulum stops moving.
Furthermore, if you make 1:1 your example it does not show such results as you have. Maybe you have left ciritcal steps out?
Hi Alexander,
thanks for your comment!
The reason why the pendulum stops is friction (coefficient k). The pendulum stabilises where the torque provided (3 Nm) equals m*g*l*sin(theta). Considering that m*g*l = ~5 Nm, it is expected that the pendulum stabilises between 0 and 90 deg.
If you repeat the simulation with k = 0 (no friction) the pendulum would keep oscillating forever.
What kind of difference do you get when you make a 1:1 example? I have just tested the files and they give the correct result. I have also added a link to the GitHub repository with these Scilab/Xcos files so that you can use them to test the result.
Hello Simone,
Thank you for sharing, it’s very interesting. I’m a beginner with MPC and I want to adapt your code and implement it for a cooling system. I have two inputs and one output, and I have a trained neural network model that I use for predictions, but I’m facing some issues. Could you please give me some advice on this?
Thanks in advance.
Hi Nacer, sure, I can try. What’s your question? What issues are you facing?
I am sorry, I just saw your response. Thank you very much for replying. However, I have a system that has two inputs (a command and a disturbance) and one output. To model my system, I trained a neural network that takes a window (d_window, u_window, y_window) to predict y(k+1). Then, I use this prediction to calculate the cost in order to obtain the optimal command. I tried to follow the same steps as you, but it is not working! Can you help me?
My question is: how should I apply this command to get the corresponding output, given that I don’t have a real system on which I can test?
I have already posted my question on Stack Overflow with the code I wrote : https://stackoverflow.com/questions/78736180/predictive-control-model-using-an-already-trained-neural-network-model-and-scipy?noredirect=1#comment138824175_78736180