I am trying to reproduce a curve with a model equation using non-linearleast square procedure to get out a certain "beta" value. The y and x experimental data are two 1D numpy arrays of the same size, namely "a" and "angle_plot" respectively. The code I am using produce an error: " 'float' object is not callable ". What is wrong with my code? Thank you
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import leastsq
a = array([ 0.04022493, 0.04287536, 0.03983657, 0.0393201 , 0.03810298,
0.0363814 , 0.0331144 , 0.03074823, 0.02795767, 0.02413816,
0.02180802, 0.01861309, 0.01632699, 0.01368056, 0.01124232,
0.01005323, 0.00867196, 0.00940864, 0.00961282, 0.00892419,
0.01048963, 0.01199101, 0.01533408, 0.01855704, 0.02163586,
0.02630014, 0.02971127, 0.03511223, 0.03941218, 0.04280329,
0.04689105, 0.04960554, 0.05232003, 0.05487037, 0.05843364,
0.05120701])
angle_plot = array([ 0. , 0.08975979, 0.17951958, 0.26927937, 0.35903916,
0.44879895, 0.53855874, 0.62831853, 0.71807832, 0.80783811,
0.8975979 , 0.98735769, 1.07711748, 1.16687727, 1.25663706,
1.34639685, 1.43615664, 1.52591643, 1.61567622, 1.70543601,
1.7951958 , 1.88495559, 1.97471538, 2.06447517, 2.15423496,
2.24399475, 2.33375454, 2.42351433, 2.51327412, 2.60303391,
2.6927937 , 2.78255349, 2.87231328, 2.96207307, 3.05183286,
3.14159265])
def residual(vars, x, data):
beta = vars[0]
model = 1/(4*np.pi)(1+beta*(3/2*np.cos(x)**2-1/2))
return data-model
vars = [0.2]
out = leastsq(residual, vars, args=(angle_plot, a))
You're missing a multiplication operator (I presume) between (4*np.pi)
and (1+beta*(3/2*np.cos(x)**2-1/2))
.
It should be:
def residual(vars, x, data):
beta = vars[0]
model = 1/(4*np.pi)*(1+beta*(3/2*np.cos(x)**2-1/2))
return data-model
Without the *
you're effectively trying to call 1/(4*np.pi)
as a function such as np.cos
.