Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Problem combining Scientific (leastSquaresFit) and scipy (odeint)

Reply
Thread Tools

Problem combining Scientific (leastSquaresFit) and scipy (odeint)

 
 
Harold Fellermann
Guest
Posts: n/a
 
      11-21-2009
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.
 
Reply With Quote
 
 
 
 
Colin W.
Guest
Posts: n/a
 
      11-21-2009
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
python,win32com,scipy and vb 6 : no module named scipy vml Python 3 05-03-2007 07:57 AM
ANN: SciPy 2004 Conference - Python for Scientific Computing Travis N. Vaught Python 0 05-06-2004 12:27 PM
Scipy question about scipy.optimize.leastsq Paxcal Python 0 02-11-2004 07:52 PM
[ANN] SciPy 03 Workshop - Scientific Computing with Python Travis N. Vaught Python 0 07-26-2003 10:15 PM
[ANN] SciPy '03 -- The 2nd Annual Python for Scientific Computing Workshop eric jones Python 0 06-27-2003 10:08 PM



Advertisments