问题描述
我是pyhton的新手,我认为我没有在问题中正确说明我的问题。因此,我将尽力描述问题,但要使其易于理解
我想对二维函数z = z(x,y)进行拟合,即给定一组{x,y}数据和具有z值的矩阵,计算该函数的拟合参数模型功能,我想我的数据如下。
问题在于,在返回模型函数的最终表达式之前,我必须进行使用y变量的中间计算。让我给你看一些代码。
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
from scipy.special import voigt_profile
import scipy.optimize as opt
'''Some parameters which origin is not essential for my question'''
sigma=5000
gamma=2e6
omega = 2*np.pi*555e3
hbar = 1.054571817*1e-34
k_B = 1.380649e-23
m_a = 1.66053904e-27
mass = 39.9625909*m_a
e = 1.602176634e-19 # [C] Elementary charge.
Z=1
beta=mass*omega*omega/(4*Z*e)
rho=2.3844361485301192e-31
def model_func(x,y,A,B):
'''In this function is where I use the y variable to calculate the maximum value of an array and then
do some intermediate calculations with it'''
n_max = -np.log(0.005)*k_B*y/(hbar*omega)
n = np.linspace(0,n_max,int(n_max/5) + 1)
xx,nn= np.meshgrid(x,n)
dw_z= (np.sqrt(omega**2 - (rho/mass)*(16*beta*beta))-omega)/(2*np.pi)
F = voigt_profile(xx-nn*dw_z,sigma,gamma)
F = np.sum(F,axis=0)
F_max = np.max(F)
# print('maximun value was calculated')
# Beer law
return A * np.exp(-B*F/F_max)
现在我创建了一些带有噪声的数据,以便我们可以拟合一些东西,因变量的数据存储在Z矩阵中
#independent variables
x=np.linspace(0,10,200)
y=np.linspace(0,0.5,10)
Z=np.zeros((len(y),len(x)))
for i in range(len(y)):
Z[i,:]=model_func(x,y[i],0.1)+np.random.normal(scale=0.2,size=len(x))
要能够使用curve_fit,我必须在1d数组中排列2d数组。这就是接下来的代码行所要做的。
#preparation of the data for the fitting
def unravel(data,*args):
x,y=data
arr = np.zeros(x.shape)
arr += model_func(x,*args)
return arr
X,Y=np.meshgrid(x,y)
xravel=np.vstack((X.ravel(),Y.ravel()))
guess=[0.5,0.5]
parameters,covs=opt.curve_fit(unravel,xravel,Z.ravel(),p0=guess)
我运行时收到此错误消息,而不是像数学中那样运行该函数(一对一),它占用了整个y数组,因此当我计算n_max时,我得到的是数组而不是标量,即不能做int(n_max / 5)的原因。
File "C:\Users\Usuario\Documents\Universidad\Master\Mainz\fit script\Sin título0.py",line 60,in <module>
parameters,p0=guess)
File "C:\Users\Usuario\Nueva carpeta\lib\site-packages\scipy\optimize\minpack.py",line 763,in curve_fit
res = leastsq(func,p0,Dfun=jac,full_output=1,**kwargs)
File "C:\Users\Usuario\Nueva carpeta\lib\site-packages\scipy\optimize\minpack.py",line 388,in leastsq
shape,dtype = _check_func('leastsq','func',func,x0,args,n)
File "C:\Users\Usuario\Nueva carpeta\lib\site-packages\scipy\optimize\minpack.py",line 26,in _check_func
res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
File "C:\Users\Usuario\Nueva carpeta\lib\site-packages\scipy\optimize\minpack.py",line 463,in func_wrapped
return func(xdata,*params) - ydata
File "C:\Users\Usuario\Documents\Universidad\Master\Mainz\fit script\Sin título0.py",line 54,in unravel
arr += model_func(x,*args)
File "C:\Users\Usuario\Documents\Universidad\Master\Mainz\fit script\Sin título0.py",line 32,in model_func
n = np.linspace(0,int(n_max/5) + 1)
TypeError: only size-1 arrays can be converted to Python scalars
我正在寻找有关如何解决此问题的线索,或者寻找其他适合此方法的方法。
在此先谢谢大家。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)