By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,454 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

fitting data with scipy.optimize.anneal

P: 2
Hi,
I am trying to fit experimetal data with scipy.optimize.anneal, but I think I didn't get the syntax right from reading the documentation, and I can't figure out what's wrong from the error I get.
ydata are the y-values, t are the x-values (experimental data), and I am trying to optimize p0 (Baseline, A1, fwhm, t0, decay1):

Expand|Select|Wrap|Line Numbers
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import math, sys, os
  4. from scipy.optimize import curve_fit, minimize, anneal
  5. from scipy.special import erf
  6.  
  7. p0=np.array([0.25, 1., 7., 15., 11.])
  8. t=np.array([5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0])
  9. ydata=np.array([0.53227518, 0.79152417, 0.96672679, 1., 0.76314581, 0.59492542, 0.48326752, 0.35906952,  0.32363437, 0.27881503])
  10. def func(t, Baseline, A1, fwhm, t0, decay1):
  11.     return Baseline + A1/2.*np.exp(((fwhm/(2.*math.sqrt(np.log(2.))))**2.-(4.*(t-t0)*decay1))/(4.*decay1**2.))*(1.+erf(((t-t0)/(fwhm/(2.*math.sqrt(2.*np.log(2.)))))-((fwhm/(2.*math.sqrt(np.log(2.))))/(2.*decay1))))
  12.  
  13. def diffa(p0, *data):
  14.     ydata,t=data
  15.     return abs(ydata - func(t, Baseline, A1, fwhm, t0, decay1))
  16. res = anneal(diffa, *p0)#, args=ydata)
  17.  
I get following error:
File "./jjfit_g", line 54, in <module>
res = anneal(diffa, *p0)#, args=ydata)
File "/usr/lib64/python2.7/site-packages/scipy/optimize/anneal.py", line 314, in anneal
res = _minimize_anneal(func, x0, args, **opts)
File "/usr/lib64/python2.7/site-packages/scipy/optimize/anneal.py", line 374, in _minimize_anneal
schedule = eval(schedule+'_sa()')
TypeError: unsupported operand type(s) for +: 'numpy.float64' and 'str'

The error happens on the line:
res = anneal(diffa, *p0)#, args=ydata)

How do I fix this?

Thanks.
Mar 30 '14 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.