我正在尝试使用非线性最小二乘法来使用模型方程重现曲线以得到某个" beta"值。 y和x实验数据是两个相同大小的1D numpy数组,即" a"和" angle_plot"分别。我正在使用的代码产生错误:" '浮动'对象不可调用"。我的代码出了什么问题?谢谢
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))
答案 0 :(得分:0)
您在(4*np.pi)
和(1+beta*(3/2*np.cos(x)**2-1/2))
之间错过了一个乘法运算符(我推测)。
应该是:
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
如果没有*
您有效地尝试将1/(4*np.pi)
称为np.cos
等功能。