I am writing a python code to create a curve fit o a circle and plot it along with the original data. I followed examples here:https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html but cannot understand why this error is coming up in my code, saying the array is not callable. What does it mean? The last block of code after the comment "curvefit" is where the error occurs. The error is as follows: (the array here is the result of calcCircleFunction() )
runfile('/untitled25.py', wdir='C:/XYZsara/testing/testing stj file') r= 5 [10. 9.53518102 7.69656593 8.85865225 11.77599647 14.26300842 16.59986154 18.86270235 21.08280172 23.27574271 25.45026401 27.6116804 29.76342361 31.90781435 34.04648108 36.18060165 38.31104984 40.43848797 42.56342747 44.68626971] Traceback (most recent call last): File "\untitled25.py", line 46, in <module> popt = curve_fit(circle, xTraj,yTraj) #array of curve fit version of circles File "\anaconda3\lib\site-packages\scipy\optimize\minpack.py", line 686, in curve_fit args, varargs, varkw, defaults = _getargspec(f) File "\anaconda3\lib\site-packages\scipy\_lib\_util.py", line 298, in getargspec_no_self sig = inspect.signature(func) File "\anaconda3\lib\inspect.py", line 3083, in signature return Signature.from_callable(obj, follow_wrapped=follow_wrapped) File "\anaconda3\lib\inspect.py", line 2833, in from_callable follow_wrapper_chains=follow_wrapped) File "\anaconda3\lib\inspect.py", line 2208, in _signature_from_callable raise TypeError('{!r} is not a callable object'.format(obj)) TypeError: array([10. , 9.53518102, 7.69656593, 8.85865225, 11.77599647, 14.26300842, 16.59986154, 18.86270235, 21.08280172, 23.27574271, 25.45026401, 27.6116804 , 29.76342361, 31.90781435, 34.04648108, 36.18060165, 38.31104984, 40.43848797, 42.56342747, 44.68626971]) is not a callable object from random import random from scipy.optimize import fsolve, curve_fit import numpy as np import matplotlib.pyplot as plt xi = 0 xf = 40 yi = 0 radius = 5 numPoints = 20 xdata = np.linspace(xi,xf,numPoints) def calcCircleFunction(x): #calculate the function of circle in 2D [a,b] = calcCenters(vars) print("r=",radius) circle = np.sqrt(abs((x-a)**2-radius**2)) + b return circle def calcCenters(vars): a, b = fsolve(solve_ab, [1,1]) return [a,b] def solve_ab(vars): a,b = vars f1 = (xi-a)**2 + (yi-b)**2 - radius**2 f2 = (xi-a)**2 + ((yi+2*radius)-b)**2 - radius**2 f = [f1,f2] return f circle = calcCircleFunction(xdata) print(circle) """curvefit""" xTraj = np.linspace(xi,xf,numPoints) yTraj = circle + 0.01*random() #with noise #print(yTraj) popt = curve_fit(circle, xTraj,yTraj) #array of curve fit version of circles plt.plot(xTraj, yTraj, 'b-') #plots the originral trajecory plt.plot(xdata, calcCircleFunction(xdata, *popt), 'r-') plt.show()
curve_fit()is supposed to be a function.circleis an array.callableis a general Python term forfunction, and similar constructs that can becalled, e.g.func(...).calcCircleFunction(xdata)called the function with thexdataargument, returning an array. No furthercallingis possible. Don't skip too much of Python basics. And keep the function reference (e.g.scipy) handy.