Home Ā» The Pendulum Model: Differential Equations into Simulations

The Pendulum Model: Differential Equations into Simulations

Pendulum Model Diagram

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.

Pendulum Model Diagram
Pendulum Model Diagram

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:

     $$\(\sum_i \tau_i = J \ddot{\theta}\)$$

Here, \ddot{\theta} stands for angular acceleration.

Due to the constraints of the circular path, only the projection of the gravitational force (m g \sin{\theta}) on the tangential axis contributes to the torque. Therefore, the equation becomes:

    \[\tau - k \dot{\theta} - m g l \sin{\theta} = J \ddot{\theta}, ~ J = m l^2\\]

For this model, J = m l^2 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, \ddot{\theta}:

    \[\ddot{\theta} = \frac{\tau - k \dot{\theta} - m g l \sin{\theta}}{m l^2}\]

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 \ddot{\theta} through two integrators in your simulation software. This will produce \dot{\theta} and \theta, which can be fed back into the equation for \ddot{\theta}.

This “trick” that leverages integrators to solve the differential equation iteratively is a cornerstone of this modelling approach.

Implementation in Xcos
Implementation in Xcos


Simulation Results

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

Simulation Result
Simulation Result

For this model, the following values were used:

  • $m = 0.5 ~ kg$
  • $l = 1 ~ m$
  • $k = 0.5 ~ N m s$
  • $g = 9.81 ~ m/s^2$

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.

8 thoughts on “The Pendulum Model: Differential Equations into Simulations”

  1. 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?

    1. 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.

  2. 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.

      1. 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?

        1. 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?

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Menu