1 Replies - 3986 Views - Last Post: 04 April 2016 - 11:36 AM Rate Topic: -----

#1 Omega Force   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 07-October 15

Why doesn't scipy.optimize.curve_fit() work in this small script?

Posted 04 April 2016 - 09:39 AM

Hi,

Could you explain me why doesn't scipy.optimize.curve_fit() work in this small script?

import numpy
import scipy.optimize
f = lambda x, a, b: a*numpy.exp(-b*x)
B = range(100, 401, 50)
Q = [0.04835974, 0.03482475, 0.02467964, 0.02265438, 0.01990916, 0.016777, 0.01558995]
popt, pcov = scipy.optimize.curve_fit(f,B,Q)
print(popt,pcov)


It returns this:

[ 1.  1.] [[ inf  inf]
 [ inf  inf]]
C:/Users/xxx/Documents/Python Scripts/xxx/test1.py:9: DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
  
C:\Users\xxx\Anaconda3\lib\site-packages\scipy\optimize\minpack.py:604: OptimizeWarning: Covariance of the parameters could not be estimated
  category=OptimizeWarning)


I don't see why it doesn't work properly. Q and B have the same length. Maybe it has something to do with the frequent "DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future" error messages that I always get?

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Why doesn't scipy.optimize.curve_fit() work in this small script?

#2 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 407
  • View blog
  • Posts: 1,313
  • Joined: 27-December 13

Re: Why doesn't scipy.optimize.curve_fit() work in this small script?

Posted 04 April 2016 - 11:36 AM

The only thing I can think of: scipy will start setting the two variables, a and b, to the value 1 (one); this will lead to quite dramatic values of f that may throw the calculation to infinite.
Try inserting a start point like a = 0.07 b = 0.004
popt, pcov = scipy.optimize.curve_fit(f,B,Q, p0=[0.07, 0.004])

Note: I don't have scipy currently, so this is not a verified solution.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1