Double pendulum solution using GSL

前端 未结 2 1484
攒了一身酷
攒了一身酷 2020-12-22 06:01

I am learning to use GSL to solve ODE. I wanted to solve double pendulum problem using GSL ODE functions. I decided to use this equations:

2条回答
  •  猫巷女王i
    2020-12-22 06:26

    You could also use C++ with odeint library if you are interested. For the double pendulum system. For matrices I use Eigen and for solving ODEs I use odeint , therefore this is the code for your problem.

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    using namespace boost::numeric::odeint;
    
    
    typedef std::vector< double > state_type;
    
    
    void equations(const state_type &y, state_type &dy, double x)
    {
        const double m1(0.5), m2(0.5),
                     L1(0.1), L2(0.1),
                     g(9.81);
    
        Eigen::MatrixXd M(2, 2), C(2, 1), B(2,1);
    
        /*
          Theta 1 =  y[0]
         dTheta 1 =  y[1] = dy[0]
        ddTheta 1 = dy[1]
    
          Theta 2 =  y[2]
         dTheta 2 =  y[3] = dy[2]
        ddTheta 2 = dy[3]
        */
        double delta(y[0] - y[2]);
    
        M <<      (m1 + m2)*L1, m2*L2*cos(delta),
              m2*L1*cos(delta),            m2*L2;
    
    
        C <<  m2*L1*L2*y[3]*y[3]*sin(delta) + g*(m1 + m2)*sin(y[0]),
             -m2*L1*y[1]*y[1]*sin(delta)    +        m2*g*sin(y[2]);
    
    
        //#####################( ODE Equations )################################
        dy[0] = y[1];
        dy[2] = y[3];
    
        B = M.inverse() * (-C);
    
    
        dy[1] = B(0,0);
        dy[3] = B(1,0);
    
    }
    
    
    int main(int argc, char **argv)
    {
        const double dt = 0.01;
        runge_kutta_dopri5 < state_type > stepper;
        double pi = boost::math::constants::pi();
    
        state_type y(4); 
        // initial values
        y[0] = pi/3.0;  //  Theta 1 
        y[1] = 0.0;     // dTheta 1
        y[2] = pi/4.0; //   Theta 2
        y[3] = 0.0;    //  dTheta 2
    
        for (double t(0.0); t <= 5; t += dt){
    
            cout << fixed << setprecision(2);
            std::cout << "t: " << t << "  Theta 1: " << y[0] << "   dTheta 1: " << y[1]
                      << "  Theta 2: " << y[2] << "  dTheta 2: " << y[3] << std::endl;
    
    
            stepper.do_step(equations, y, t, dt);
        }
    
    }
    

    The results are

    enter image description here

提交回复
热议问题