title: Double Pendulum returning nan results. Why?
body: <p>When I run this code the double pendulum shows for a split second and then disappears. It seems most of the variables in the formula, num1, num2, etc., start returning nan after that split second when the pendulum disappears. Can anyone see why? I thought it had to do with converting degrees to radians at 180 degrees so I added the if statement in my convert function.</p>
  
  <p>Here is the link to the Double Pendulum motion formula. I broke down the formula into pieces for simplicity's sake. <a href="https://www.myphysicslab.com/pendulum/double-pendulum-en.html" rel="nofollow noreferrer">https://www.myphysicslab.com/pendulum/double-pendulum-en.html</a></p>
  
  <p>This idea came from Coding Train's Double Pendulum video: <a href="https://www.youtube.com/watch?v=uWzPe_S-RVE" rel="nofollow noreferrer">https://www.youtube.com/watch?v=uWzPe_S-RVE</a></p>
  
  <pre><code>#undef __STRICT_ANSI__
  #include "window.h"
  #include &lt;SFML/Graphics.hpp&gt;
  #include &lt;GL/glew.h&gt;
  #include &lt;cmath&gt;
  #include &lt;iostream&gt;
  
  namespace Window
  {
      sf::ContextSettings settings;
  
      sf::RenderWindow window(sf::VideoMode(600, 600), "Window", sf::Style::Close | sf::Style::Titlebar, settings);
  
      void init()
      {
          settings.depthBits = 24;
          settings.majorVersion = 4;
          settings.minorVersion = 6;  //OpenGL 4.6
  
          glewInit();
          glViewport(0,0, 600, 600);
      }
  
      void close()
      {
          window.close();
      }
  
      void update()
      {
          window.display();
      }
  
      void checkForClose()
      {
          sf::Event windowEvent;
          while (window.pollEvent(windowEvent))
          {
              if (windowEvent.type == sf::Event::Closed)
              {
                  close();
              }
          }
      }
  
      bool isOpen()
      {
          return window.isOpen();
      }
  
      double convDeg(double deg)
      {
          if (deg == 180.0)
          {
              return 0.0;
          }
  
          else
          {
              return deg * M_PI / 180.0;
          }
      }
  
      void runLoop()
      {
          double r1 = 100;
          double m1 = 40;
          double a1 = 90;
          double a1_v = 0;
  
          double r2 = 100;
          double m2 = 40;
          double a2 = 30;
          double a2_v = 0;
  
          double x1 = 0;
          double x2 = 0;
          double y1 = 0;
          double y2 = 0;
  
          double g = 0.005;
  
          double num1 = 0;
          double num2 = 0;
          double num3 = 0;
          double num4 = 0;
          double den = 0;
  
          double a1_a = 0;
          double a2_a = 0;
  
          while (window.isOpen())
          {
              num1 = -g * (2 * m1 + m2) * std::sin(convDeg(a1));
              num2 = -m2 * g * std::sin(convDeg(a1-2*a2));
              num3 = -2*std::sin(convDeg(a1-a2))*m2;
              num4 = a2_v*a2_v*r2+a1_v*a1_v*r1*std::cos(convDeg(a1-a2));
              den = r1 * (2*m1+m2-m2*cos(convDeg(2*a1-2*a2)));
              a1_a = (num1 + num2 + num3*num4) / den;
  
              num1 = 2 * std::sin(convDeg(a1-a2));
              num2 = (a1_v*a1_v*r1*(m1+m2));
              num3 = g * (m1 + m2) * std::cos(convDeg(a1));
              num4 = a2_v*a2_v*r2*m2*std::cos(convDeg(a1-a2));
              den = r2 * (2*m1+m2-m2*std::cos(convDeg(2*a1-2*a2)));
              a2_a = (num1*(num2+num3+num4)) / den;
  
              x1 = 300 + (r1 * std::sin((convDeg(a1))));
              y1 = 300 + (r1 * std::cos((convDeg(a1))));
              x2 = x1 + (r2 * std::sin((convDeg(a2))));
              y2 = y1 + (r2 * std::cos((convDeg(a2))));
  
              a1_v += a1_a;
              a2_v += a2_a;
              a1 += a1_v;
              a2 += a2_v;
  
              sf::Vertex pOne[] =
              {
                  sf::Vertex(sf::Vector2f(300,300)),
                  sf::Vertex(sf::Vector2f(x1,y1)),
              };
  
              sf::Vertex pTwo[] =
              {
                  sf::Vertex(sf::Vector2f(x1,y1)),
                  sf::Vertex(sf::Vector2f(x2,y2)),
              };
  
              window.clear();
              window.draw(pOne, 2, sf::Lines);
              window.draw(pTwo, 2, sf::Lines);
  
              update();
              checkForClose();
  
              std::cout &lt;&lt; std::sin(convDeg(a1)) &lt;&lt; "\n";
  
          }
      }
  }
  </code></pre>