Aatif Jamshaid Malik
Aatif Jamshaid Malik

Reputation: 71

Solving differential Equation using BOOST Libraries

I have to find next state of my robot which can be found though solving differential equations. In MATLAB i used ode45 function and on c++ i found on internet that i have to use some method like stepper runga kutta dopri5. I tried to understand its implementation and somehow got an idea. Now my states are X,Y and theta and to find next states my differential equations are

Xdot=v*cos(theta)
Ydot=v*sin(theta)
thetadot=w

Now there is a public function stepper.do_step_impl(System,state,...etc) where what i understood is that system represents a function whose parameters could be &state,dstate and t. Now here is my problem. My differential equations have variables v and w which i need in my System function but according to my understanding System function could have only fixed parameters i.e State, dstate and t. How do i put v and w in it? I hope someone understands my question. Kindly help. Below is my code

using namespace boost::numeric::odeint;
typedef std::vector< double > state_type; 

void pdot_w( const state_type &state, state_type &dstate, const double t )
{
    double p_robot_xdot = v*cos(state[2]);
    double p_robot_ydot = v*sin(state[2]);
    double thetadot = w;
    dstate[0] = p_robot_xdot;
    dstate[1] = p_robot_ydot;
    dstate[2] = thetadot;
}

runge_kutta_dopri5<state_type> stepper;
stepper.do_step(pdot_w, state , 0, 0.01 );

Upvotes: 1

Views: 725

Answers (1)

stephematician
stephematician

Reputation: 884

The answer depends on how you want to pass the parameters v and w to the integrator.

One approach in C++11 is to use a lambda. Your function and a call to the stepper would look like this (depending on context, the capture [] may need to be more explicit):

void pdot_w(const state_type &state,
            state_type & dstate,
            const double t,
            const double v,
            const double w) {
    dstate[0] = v * cos(state[2]);
    dstate[1] = v * sin(state[2]);
    dstate[2] = w;
}


runge_kutta_dopri5<state_type> stepper;
/* v = 4 and w = 5 example */
stepper.do_step([](const state_type & state, state_type & d_state, const double t) {
                    return pdot_w(state, d_state, t, 4, 5);
                }, state,
                0,  0.01);

Upvotes: 1

Related Questions