Boost logo

Boost Users :

Subject: Re: [Boost-users] odeint - solving an ODE with time-dependent parameter numerically
From: Karsten Ahnert (karsten.ahnert_at_[hidden])
Date: 2014-01-21 10:39:00


>>void ode( state_type const &y , state_type &dydt , double t )
>>{
> > dydt[0] = a1(t) * y[1] - b1(t) * y[2] - c1(t) * y[0];
> > // ...
>>}
>
> I am little bit confused now how to incorporate the RHS of the ode
> system-I guess I need to write RHS of the ode separately.Your suggestion
> would be helpful.

Yes, then the RHS function (ode in the upper case) is passed to the
stepper or integrate functions. Just take a look in the docs if this is
not clear.^

> e.g.
>
> double a1(double t, double c1, double c2)
> {
> //
> return a1;
> }
>
> similarly for a2 and a3. i am confused how I can pass the a1,b1,c1 and
> so on in the ODE function and solve it.
> Suggestions would be helpful.

Do you need to exchange the functions for your system? In my suggestion
I just created a function for coefficient a and and call this function
from the rhs function. Otherwise you can make template paramters of it:

template< typename CoefA , typename CoefB >
struct ode
{
    CoefA m_coef_a;
    CoefB m_coef_b;
    ode( CoefA coef_a , CoefB coef_b )
    : m_coef_a( coef_a ) , m_coef_b( coef_b ) {}
    void operator()( state_type const& x , state_type &dxdt , double t )
const
    {
        dxdt[0] = m_coef_a( t ) * y[1] ...
        ...
    }
};

maybe even with a nice make_ode function that you do not need to
explicitly pass the template parameter

template< typename CoefA , typename CoefB >
ode< CoefA , CoefB > make_ode( CoefA coef_a , CoefB coef_b )
{
    return ode< CoefA , CoefB >( coef_a , coef_b );
}

you can use this factory like

auto my_ode = make_ode( concrete_a , concrete_b );


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net