我是编程和尝试分析物理类数据的新手。到目前为止,我有:
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
我该如何解决这个问题?
答案 0 :(得分: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))