# How to change the equation into a code in an nth pendulum?

**URL:** https://discourse.processing.org/t/how-to-change-the-equation-into-a-code-in-an-nth-pendulum/26558
**Category:** Coding Questions
**Created:** [December 28, 2020, 1:55pm UTC](https://discourse.processing.org/t/how-to-change-the-equation-into-a-code-in-an-nth-pendulum/26558 "2020-12-28T13:55:50Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Rafsing](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rafsing/32/10134_2.png) [@Rafsing](https://discourse.processing.org/u/Rafsing)
#### Post date: [December 28, 2020, 1:55pm UTC](https://discourse.processing.org/t/how-to-change-the-equation-into-a-code-in-an-nth-pendulum/26558/1 "2020-12-28T13:55:50Z")

</div>

I want to simulate the nth level pendulum (double, triple and so on). I found the equations for the acceleration of the nth pendulum on this page: [https://www.researchgate.net/publication/336868500\_Equations\_of\_Motion\_Formulation\_of\_a\_Pendulum\_Containing\_N-point\_Masses](https://www.researchgate.net/publication/336868500_Equations_of_Motion_Formulation_of_a_Pendulum_Containing_N-point_Masses) but I am having trouble changing it to code. My main problem is that I don’t know where some of the indexes came from. Here’s the equation I’d like to change into code:

 ![Acceleration_Equation_For_a_Pendulum](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b3df7448feca43525108d740c01617357bf85585.png)  
This is my code:

```auto
 int sigma(int j, int k)
  {
    return int(j<=k);
  }

  int phi(int j, int k)
  {
    return int(!(j==k));
  }

  void calculateAcceleration()
  {
    float dividend = 1, divider = 0;
    float[] copyAcceleration = new float[bob.size()];

    for (int i = 0; i <bob.size() - 1; i++)
    {
      copyAcceleration[i] = angleAcceleration.get(i);
    }

    for (int i = 0; i <bob.size() - 1; i++)
    {
      for (int j = 0; j <bob.size() - 1; j++)
      {
        dividend+= angles.get(j);
      }

      for (int k = 0; k <bob.size() - 1; k++)
      {
        divider += bob.get(k).mass * lenghts.get(bob.size() - 1) * lenghts.get(bob.size() - 1) * sigma(bob.size(), k);
      }

      copyAcceleration[i] = -dividend/divider;
    }

    for (int i = 0; i <bob.size() - 1; i++)
    {
      angleAcceleration.set(i, copyAcceleration[i]);
    }
  }

```

Here’s my whole project: [https://pastebin.com/mAi1aNSL](https://pastebin.com/mAi1aNSL). Can someone help me with this?

---

<div class="post-metadata">

### Author: ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)
#### Post date: [December 28, 2020, 3:14pm UTC](https://discourse.processing.org/t/how-to-change-the-equation-into-a-code-in-an-nth-pendulum/26558/2 "2020-12-28T15:14:37Z")

</div>

Hi @Rafsing,

If I am not mistaken and didn’t commit any mistakes the following code should reproduce the equation

```auto
int n = 3;
float []m = {20, 20, 20};
float []l = {40, 40, 40};
float []angle = {PI/2, PI/2, PI/2};
float []vel = {0,0,0};
float []accel = {0,0,0};

void setup() {
  float g = 1;
  float den = 0;
  float num1 = 0;
  float num2 = 0;
  float num3 = 0;

  for (int j = 0; j < n; j++) {
    //denominator 
    for (int k = 0; k < n; k++) {
      den += m[k] * l[k] * l[k] * (j <= k? 1 : 0);
    }

    
    for (int k = 0; k < n; k++) {
      //first numerator
      num1 = g * l[j] * sin(angle[j]) * m[k] * (j <= k? 1 : 0);

      //second numerator
      float inner_sum = 0;
      // inner sum
      for (int q = k+1; q < n; q++) {
        inner_sum += m[q] * (j <= q? 1 : 0);
      }
      num2 = inner_sum * l[j] * l[k] * sin(angle[j] - angle[k]) * vel[j] * vel[k];
      
      //Third numerator
      //The inner sum is the same as in the num2
      num3 = inner_sum * l[j] * l[k] * (sin(angle[k] - angle[j]) * (vel[j] * vel[k]) * vel[k] + (j != k ? 1 : 0) * cos(angle[j] - angle[k])*accel[k]);
    }
    
    accel[j] = - (num1 + num2 + num3) / den;
  }
  printArray(accel);
}

```

Hope it helps

---

<div class="post-metadata">

### Author: ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)
#### Post date: [December 28, 2020, 4:52pm UTC](https://discourse.processing.org/t/how-to-change-the-equation-into-a-code-in-an-nth-pendulum/26558/3 "2020-12-28T16:52:38Z")

</div>

May I ask, where did you get that equation from?  
I don’t think that is correct…

If you deduced it yourself from the equation below I can assure you that the one you posted is not correct, You can even check with a simple or double pendulum equation  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/92c77655ff4d85604a1c870678fbd89813fe9d9c.png)

For a simple pendulum:  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c8ab1771bed15b3d18e14ebdd2c12b1bcc0f3b86.png)  
Your denominator does not depend on the mass

For a double pendulum:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/8ee1cb7db76516699970bf104b9114bf5884583e.png)  
Your denominator must include cosine functions, which are not present in the equation you posted

---

<div class="post-metadata">

### Author: ![Rafsing](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rafsing/32/10134_2.png) [@Rafsing](https://discourse.processing.org/u/Rafsing)
#### Post date: [December 28, 2020, 6:16pm UTC](https://discourse.processing.org/t/how-to-change-the-equation-into-a-code-in-an-nth-pendulum/26558/4 "2020-12-28T18:16:21Z")

</div>

I derived this equation from the equation number 38:

 ![Równanie _wahadła1](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/c952373a5d0652416028f7775603d550ccf2a038.png)

---

<div class="post-metadata">

### Author: ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)
#### Post date: [December 29, 2020, 8:45am UTC](https://discourse.processing.org/t/how-to-change-the-equation-into-a-code-in-an-nth-pendulum/26558/5 "2020-12-29T08:45:26Z")

</div>

I don’t think that is correct.

The last term also depends on the acceleration. Which if I am not mistaken takes into account the cosine function that is missing you. Notice that for a simple pendulum this last term is zero because j = k, hence phi is 0. But for a double pendulum, when j = 1, and k = 2, then the phi function became 1 and introduces the cosine functions.

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/11b6134878f885b5e1f56ed59bff62f9e7411c8d.png)

---

<div class="post-metadata">

### Author: ![Rafsing](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rafsing/32/10134_2.png) [@Rafsing](https://discourse.processing.org/u/Rafsing)
#### Post date: [December 29, 2020, 12:28pm UTC](https://discourse.processing.org/t/how-to-change-the-equation-into-a-code-in-an-nth-pendulum/26558/6 "2020-12-29T12:28:12Z")

</div>

How would you transform it then?
