[Numpy-discussion] scipy curve_fit variable list of optimisation parameters

Siegfried Gonzi siegfried.gonzi at ed.ac.uk
Wed Aug 3 08:53:09 EDT 2016

On 3 Aug 2016, at 13:00, numpy-discussion-request at scipy.org wrote:

> Message: 3
> Date: Tue, 2 Aug 2016 22:50:42 +0100
> From: Evgeni Burovski <evgeny.burovskiy at gmail.com>
> To: Discussion of Numerical Python <numpy-discussion at scipy.org>
> Subject: Re: [Numpy-discussion] scipy curve_fit variable list of
> 	optimisation parameters
> Message-ID:
> 	<CAMRo0ivCS8HqR6Pyo9-wvnhboPGYzaFwfZNmVwE5Q5NTXna9Dg at mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
> You can use `leastsq` or `least_squares` directly: they both accept an
> array of parameters.
> BTW, since all of these functions are actually in scipy, you might
> want to redirect this discussion to the scipy-user mailing list.

Hi all

I found the solution in the following thread:


One has to call curve_fit with 'p0' (giving curve_fit a clue about the unknown number of variables)

I changed func2 to (note the *):

def func2( x, *a ):

       # Bessel function
	tmp = scipy.special.j0( x[:,:] )

	return np.dot( tmp[:,:] , a[:] )

and call it:

N = number of optimisation parameters

popt = scipy.optimize.curve_fit( func2, x, yi , p0=[1.0]*N)

Siegfried Gonzi
Met Office, Exeter, UK
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.

More information about the NumPy-Discussion mailing list