2014-09-26 112 views
-1

我很新的编程和尝试分析物理课的数据。到目前为止,我有:NameError弹出曲线_fit

import numpy as np 
import matplotlib.pyplot as plt 
from scipy import * 
from scipy.optimize import curve_fit 
import matplotlib.cm as cm 
from matplotlib import colors 

def curve_fit_custom(f, xdata, ydata, p0=None, sigma=None, **kw): 
    """ 
    Pass all arguments to curve_fit, which uses non-linear least squares 
    to fit a function, f, to data. Calculate the uncertaities in the 
    fit parameters from the covariance matrix. 
    """ 
    (popt, pcov) = curve_fit(f, xdata, ydata, p0, sigma, **kw) 

    if sigma is None: 
     chi2 = sum(((f(xdata,*popt)-ydata))**2) 
    else: 
     chi2 = sum(((f(xdata,*popt)-ydata)/sigma)**2) 
    dof = len(ydata) - len(popt) 
    rchi2 = chi2/dof 
    print 'results of general_fit:' 
    print ' chi squared = ', chi2 
    print ' degrees of freedom = ', dof 
    print ' reduced chi squared = ', rchi2 

    # The uncertainties are the square roots of the diagonal elements 
    punc = zeros(len(popt)) 
    for i in arange(0,len(popt)): 
     punc[i] = sqrt(pcov[i,i]) 
    return popt, punc, rchi2, dof 

(turnsmm, voltage) = loadtxt('Lab2 mm vs. voltage.csv',delimiter=',', unpack=True, usecols=[0,1]) 

def P(P0, Pmax, x, x0, w): 
    return P0+(Pmax/2.0)(1.0-math.erf((sqrt(2.0)(x-x0))/w)) 

yuncertainty=0.01 
yuncertaintyvector=ones(len(turnsmm))*yuncertainty 

pguess = [18, 8, 3, 5] 

(popt, punc, rc, d) = curve_fit_custom(P, turnsmm, voltage, pguess, yuncertaintyvector) 

但我得到这个错误信息的最后一部分:

TypeError         Traceback (most recent call last) 
<ipython-input-103-bd94f34028f4> in <module>() 
     1 pguess = [18, 8, 3, 5] 
     2 
----> 3 (popt, punc, rc, d) = curve_fit_custom(P, turnsmm, voltage, pguess, yuncertaintyvector) 

<ipython-input-98-6c1057de6870> in curve_fit_custom(f, xdata, ydata, p0, sigma, **kw) 
    12  fit parameters from the covariance matrix. 
    13  """ 
---> 14  (popt, pcov) = curve_fit(f, xdata, ydata, p0, sigma, **kw) 
    15 
    16  if sigma is None: 

/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/scipy/optimize/minpack.pyc in curve_fit(f, xdata, ydata, p0, sigma, absolute_sigma, **kw) 
    553  # Remove full_output from kw, otherwise we're passing it in twice. 
    554  return_full = kw.pop('full_output', False) 
--> 555  res = leastsq(func, p0, args=args, full_output=1, **kw) 
    556  (popt, pcov, infodict, errmsg, ier) = res 
    557 

/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/scipy/optimize/minpack.pyc  in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag) 
    367  if not isinstance(args, tuple): 
    368   args = (args,) 
--> 369  shape, dtype = _check_func('leastsq', 'func', func, x0, args, n) 
    370  m = shape[0] 
    371  if n > m: 

/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/scipy/optimize/minpack.pyc in _check_func(checker, argname, thefunc, x0, args, numinputs, output_shape) 
    18 def _check_func(checker, argname, thefunc, x0, args, numinputs, 
    19     output_shape=None): 
---> 20  res = atleast_1d(thefunc(*((x0[:numinputs],) + args))) 
    21  if (output_shape is not None) and (shape(res) != output_shape): 
    22   if (output_shape[0] != 1): 

/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/scipy/optimize/minpack.pyc in _weighted_general_function(params, xdata, ydata, function, weights) 
    447 
    448 def _weighted_general_function(params, xdata, ydata, function, weights): 
--> 449  return weights * (function(xdata, *params) - ydata) 
    450 
    451 

<ipython-input-101-c072ef1a8e7e> in P(P0, Pmax, x, x0, w) 
     1 def P(P0, Pmax, x, x0, w): 
----> 2  return P0+(Pmax/2.0)(1.0-math.erf((sqrt(2.0)(x-x0))/w)) 

TypeError: 'numpy.float64' object is not callable 

我该如何解决这个问题?

回答

1

它看起来像你错过了上线的算术运算符

P0+(Pmax/2.0)(1.0-math.erf((sqrt(2.0)(x-x0))/w)) 

如果你想在Python两个数相乘,你必须明确地使用运营商,例如*。 Python不会将诸如(4-1)(4)之类的表达式计算为12;你必须写(4-1)*(4)

(Pmax/2.0)是一个浮点数,并且通过将括号直接放在它后面,Python认为你正在试图将它作为一个可调用对象(就像一个函数),它不是。

也许行应该是这样的:

P0 + (Pmax/2.0) * (1.0 - math.erf((sqrt(2.0)*(x-x0))/w))