Problem combining Scientific (leastSquaresFit) and scipy (odeint)

Discussion in 'Python' started by Harold Fellermann, Nov 21, 2009.

  1. Hi,

    I need to perform leastSquaresFit of a model that is given by a
    differential equation for which there seems to be no analytic
    solution. So, I am trying to solve the ODE numerically (using
    scipy.integrate.odeint) within the function I provide to
    leastSquaresFit as a model:

    def func(L, t, a, k) :
    return -k * L * (1 - ( 1 - a*L**(-1./3) )**3.)

    def model((k, L0, a), t) :
    solution = odeint( func, array(L0[0]), array([0,t]), args=(a,k) )
    return L0 - solution[1][0]

    params, chisq = leastSquaresFit(model, params, data)

    Unfortunately, this approach runs into an error (ValueError: shape
    mismatch: objects cannot be broadcast to a single shape) that seems to
    stem from the fact that leastSquaresFit is based on automatic
    derivation (DerivVar), and according to the manual "the function [that
    defines the model] may only use the mathematical functions known to
    the module FirstDerivatives".

    What is a good solution or workaround to this problem which appears to
    be quite a standard situation to me?

    Thanks for any help, harold.
     
    Harold Fellermann, Nov 21, 2009
    #1
    1. Advertising

  2. Harold Fellermann

    Colin W. Guest

    Harold Fellermann wrote:
    > Hi,
    >
    > I need to perform leastSquaresFit of a model that is given by a
    > differential equation for which there seems to be no analytic
    > solution. So, I am trying to solve the ODE numerically (using
    > scipy.integrate.odeint) within the function I provide to
    > leastSquaresFit as a model:
    >
    > def func(L, t, a, k) :
    > return -k * L * (1 - ( 1 - a*L**(-1./3) )**3.)
    >
    > def model((k, L0, a), t) :
    > solution = odeint( func, array(L0[0]), array([0,t]), args=(a,k) )
    > return L0 - solution[1][0]
    >
    > params, chisq = leastSquaresFit(model, params, data)
    >
    > Unfortunately, this approach runs into an error (ValueError: shape
    > mismatch: objects cannot be broadcast to a single shape) that seems to
    > stem from the fact that leastSquaresFit is based on automatic
    > derivation (DerivVar), and according to the manual "the function [that
    > defines the model] may only use the mathematical functions known to
    > the module FirstDerivatives".
    >
    > What is a good solution or workaround to this problem which appears to
    > be quite a standard situation to me?
    >
    > Thanks for any help, harold.

    You might consider using numpy.

    Colin W.
     
    Colin W., Nov 21, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. eric jones
    Replies:
    0
    Views:
    682
    eric jones
    Jun 27, 2003
  2. Travis N. Vaught
    Replies:
    0
    Views:
    342
    Travis N. Vaught
    Jul 26, 2003
  3. FLChamp

    odeint function in SciPy

    FLChamp, Apr 27, 2005, in forum: Python
    Replies:
    5
    Views:
    2,329
    Robert Kern
    May 3, 2005
  4. vml
    Replies:
    3
    Views:
    704
  5. Johannes Bauer
    Replies:
    4
    Views:
    517
    James Kanze
    Jul 23, 2008
Loading...

Share This Page