Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <iostream>
#include <boost/numeric/odeint.hpp>       // odeint function definitions

using namespace std;
using namespace boost::numeric::odeint;

// Defining a shorthand for the type of the mathematical state
typedef std::vector< double > state_type;

// System to be solved: dx/dt = -2 x
void my_system( const state_type &x , state_type &dxdt , const double t )
{    dxdt[0] = -2*x[0];   }

// Observer, prints time and state when called (during integration)
void my_observer( const state_type &x, const double t )
{    std::cout  << t << "   " << x[0] << std::endl;   }

// ------  Main
int main()
{
    state_type x0(1); // Initial condition, vector of 1 element (scalar problem)
    x0[0] = 10.0;

    // Integration parameters
    double t0 = 0.0;
    double t1 = 10.0;
    double dt = 0.1;

    // Run integrator
    integrate( my_system, x0, t0, t1, dt, my_observer );
}


What I have tried:

[t, y] = ode45(@vdp1,[0 20],[2; 0]); //in that way here we are not passing time_step i want to use such solver like odeint.h solver
Posted
Updated 10-Jun-22 21:00pm

1 solution

The Matlab documentation under de.mathworks.com/help/matlab/ref/ode45.html states:
The time step chosen by the solver at each step is based on the equation in the system that needs to take the smallest step.
This means the solver can take small steps to satisfy the equation for one initial condition, but the other equations, if solved on their own, would use different step sizes.

tspan — Interval of integration (vector)
Interval of integration, specified as a vector. At a minimum, tspan must be a two-element vector [t0 tf] specifying the initial and final times. To obtain solutions at specific times between t0 and tf, use a longer vector of the form [t0,t1,t2,...,tf]. The elements in tspan must be all increasing or all decreasing.
-If tspan has two elements [t0 tf], then the solver returns the solution evaluated at each internal integration step within the interval.
-If tspan has more than two elements [t0,t1,t2,...,tf], then the solver returns the solution evaluated at the given points.

How Matlab determines the step size is only vaguely explained under "tspan — Interval of integration", that's where the know-how is.

You need enough points to get an acceptable plot result at the end. Matlab points out that by choice limits and the steps may change the outcome. You tend to need more steps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900