Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Problem combining Scientific (leastSquaresFit) and scipy (odeint) (http://www.velocityreviews.com/forums/t706089-problem-combining-scientific-leastsquaresfit-and-scipy-odeint.html)

 Harold Fellermann 11-21-2009 07:19 PM

Problem combining Scientific (leastSquaresFit) and scipy (odeint)

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.

 Colin W. 11-21-2009 11:22 PM

Re: Problem combining Scientific (leastSquaresFit) and scipy (odeint)

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.

 All times are GMT. The time now is 03:02 PM.