pBook‎ > ‎Solver:: Integrator:: C++‎ > ‎

Example Kin Sim

posted Dec 1, 2014, 10:08 PM by Javad Taghia   [ updated Dec 2, 2014, 8:26 PM ]
//============================================================================

//============================================================================
#include <iostream>
#include <timing.h>
#include <boost\regex.hpp>
#include <boost\array.hpp>
#include <boost\numeric\odeint.hpp>
#include <string>
typedef std::vector<double> state_type;
const double len = 1.7;
// the rhs of x' = f(x)
void harmonic_oscillator(const state_type &x,
state_type &dxdt, const double t)
{
dxdt[0] =  x[3] * cos(x[2]);
dxdt[1] =  x[3] * sin(x[2]);
dxdt[2] = x[3] / len * tan(x[4]);
dxdt[3] = 0;
dxdt[4] = 0;
}

template <class vectype>
std::vector<vectype> createRange(vectype start, vectype end, vectype step )
{
    std::vector<vectype> result;
    for (; start < end; start += step) {
        result.push_back(start);
    }
    return result;
}


int main() {
state_type x(5);
x[0] = 0.0; // xt
x[1] = 0.0; // yt
x[2] = 0.0; // tHeta
x[3] = 1; // vf
x[4] = 0; // dElta_t
std::vector<double> t = createRange<double>(0, 10, 0.01);
//for (double i=0; i<=10; i+=0.1)
//t.push_back(i);

std::cout << x[0] << "  " << x[1] << "  " << x[2] << std::endl;
double t_old = 0;
for (auto i: t)
{
size_t steps = boost::numeric::odeint::
integrate(harmonic_oscillator, x, t_old, i, 0.001);
t_old = i;
std::cout << x[0] << "  " << x[1] << "  " << x[2] << std::endl;
}
return 0;
}


with array


#include <global.h>
#include <generalFunctions.h>
#include <stateSpaceModel.h>
typedef  boost::array<double, 5> state_type;
const double len = 1.7;


int main() {
ashacontrol::stateModel<state_type, double > model(len);
state_type x;
x[0] = 0.0; // xt
x[1] = 0.0; // yt
x[2] = 0.0; // tHeta
x[3] = 1; // vf
x[4] = 0; // dElta_t
std::vector<double> t = ashacontrol::createRange<double>(0, 10, 0.01);
std::cout << x[0] << "  " << x[1] << "  " << x[2] << std::endl;
double t_old = 0;
for (auto i: t)
{
size_t steps = boost::numeric::odeint::
integrate(model, x, t_old, i, 0.001);
t_old = i;
std::cout << x[0] << "  " << x[1] << "  " << x[2] << std::endl;
}

   getchar();

ashacontrol::CStopwatch timer1;
splab::Timing splabtimer2;
timer1.start();
splabtimer2.start();
std::string line;
boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );
while (std::cin)
{
std::cout<<"hi"<<std::endl;
std::getline(std::cin, line);
boost::smatch matches;
if (boost::regex_match(line, matches, pat))
std::cout << matches[2] << std::endl;
while(1)
{
std:: cout << "time is: " <<  timer1.currentTime()<< std:: endl; // prints !!!Hello World!!!
std:: cout << "time is 2 : "  << splabtimer2.readOnline() << std:: endl;
std:: cout << "diff in time is: " << timer1.currentTime() -
splabtimer2.readOnline() << std::endl;
}
}

return 0;
}

Based my experience Array works better. In a complicated program vector failed in runtime with and exception regarding iterator.
ą
12.png
(19k)
Javad Taghia,
Dec 2, 2014, 8:26 PM
Comments