[SciPy-User] Fitting problem using optimize.fmin_slsqp

guillaume gwenael guillaume.gwenael at neuf.fr
Thu May 19 11:50:39 EDT 2011


Do you mean that it is not possible de fit a data array using fmin_slsqp?

Gwenaël


2011/5/19 Skipper Seabold <jsseabold at gmail.com>

>
> On Thu, May 19, 2011 at 6:07 AM, guillaume gwenael <gwenln44 at gmail.com>
> wrote:
> > Hello,
> > I need to fit analytic data using a constrained least square fit. Here is
> > the script :
> > import numpy as npimport scipy as scp
> >
> > from scipy.optimize import fmin_slsqp
> >
> > def OriginalFunc(xaxis,a,b,c,d,e,f):
> >
> > p1=d*1e3
> >
> > p2=(a*e**2*c)/(p1*f)
> >
> > p3=(a*b*e)/f
> >
> > M=p3*np.sqrt( (1-1j*xaxis*p2)/(-1j*xaxis*p2) )
> >
> > N = ((e*xaxis)/b)*np.sqrt( (1-1j*xaxis*p2)/(-1j*xaxis*p2) )
> >
> > return M,p3,N
> >
> > def FirstOrderLinSyst(num,den,xaxis):
> >
> > j=np.complex(0,1)
> >
> > fos=num/(den-j*xaxis)
> >
> > return fos
> >
> > def PartialFractionExpansion(coefs,ref,xaxis):
> >
> > order=len(coefs)/2
> >
> > num=coefs[:order]
> >
> > den=coefs[order:]
> >
> > SumK=0.
> >
> > for k in range(order):
> >
> > fos=FirstOrderLinSyst(num[k],den[k],xaxis)
> >
> > SumK=SumK+fos
> >
> > approx=ref*SumK
> >
> > return approx
> >
> > def Residuals(fitcoefs,curve,ref,xaxis):
> >
> > diff=np.real(curve)-np.real(PartialFractionExpansion(fitcoefs,ref,xaxis))
> >
> > err=np.sqrt(
> > np.sum(np.power(diff,2),axis=0)/np.sum(np.power(np.real(curve),2),axis=0)
> )
> >
> > return np.array([err],dtype=float)
> >
> > def GetParams4Fitting(init_fitcoefs,curve,ref,xaxis):
> >
> >
> pbounds=[(-np.inf,np.inf),(-np.inf,np.inf),(-np.inf,np.inf),(-np.inf,np.inf),
> >
> > (-np.inf,np.inf),(-np.inf,np.inf),(0,np.inf),(0,np.inf),(0,np.inf),
> >
> > (0,np.inf),(0,np.inf),(0,np.inf)]
> >
> > fitcoefs,fittedfunc,iters,imode,smode=fmin_slsqp(Residuals,
> >
> > init_fitcoefs,
> >
> > args=(curve,ref,xaxis),
> >
> > bounds=pbounds)
> >
> > return fitcoefs,fittedfunc,iters,imode,smode
> >
> > # Parameters
> >
> > a=1.204
> >
> > b=344.4256
> >
> > c=1.41
> >
> > d=10
> >
> > e=np.sqrt(3.5)
> >
> > f=0.5
> >
> > x_min=100
> >
> > x_max=5000
> >
> > step=100
> >
> > xaxis=2*np.pi*np.array([np.arange(x_min,x_max+step,step)])
> >
> > # Oiginal function
> >
> > M,p3,N=OriginalFunc(xaxis,a,b,c,d,e,f)
> >
> > # Initialization of fitting parameters
> >
> > order=6
> >
> > params_k=np.zeros((2*order))
> >
> > # Fitting parameters computing
> >
> >
> fitcoefs,fittedfunc,iters,imode,smode=GetParams4Fitting(params_k,M,p3,xaxis)
> >
> > It returnes the foolwing error:
> > File "C:\Python26\lib\site-packages\scipy\optimize\slsqp.py", line 318,
> in
> > fmin_slsqp g = append(fprime(x),0.0) File
> > "C:\Python26\lib\site-packages\scipy\optimize\optimize.py", line 176, in
> > function_wrapper return function(x, *args) File
> > "C:\Python26\lib\site-packages\scipy\optimize\optimize.py", line 377, in
> > approx_fprime grad[k] = (f(*((xk+ei,)+args)) - f0)/epsilon ValueError:
> > setting an array element with a sequence.
> >
> > Can anyone help me?
>
> Two things I noticed. The error message is because fmin_slsqp expects
> func to return a scalar and then you want fmin_slsqp(...
> full_output=1) to return all of those values.
>
> Skipper
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20110519/d7463eee/attachment.html>


More information about the SciPy-User mailing list