Python数值积分
我试图最小化函数ellipmodel 在该功能中:Python数值积分,python,matlab,mathematical-optimization,Python,Matlab,Mathematical Optimization,我试图最小化函数ellipmodel 在该功能中: # c[0]=theta_m, c[1]=alpha, c[2]=omega_0, # c[3]=phi, c[4]=const. # pos is the position data def expfit(c,pos): # Determines a fit to the large amplitude decay of the undriven pendulum. min = 150 t = 0.001 * np.
# c[0]=theta_m, c[1]=alpha, c[2]=omega_0,
# c[3]=phi, c[4]=const.
# pos is the position data
def expfit(c,pos):
# Determines a fit to the large amplitude decay of the undriven pendulum.
min = 150
t = 0.001 * np.arange(min,len(pos)+1) # Converts data to seconds.
pos = pos[min:len(pos)]
### options = optimset('MaxIter',5000) # Determines the number of iterations.
dt = t[1] - t[0]
# Minimise the function ellipmodel.
### c = fminsearch('ellipmodel',c(),options,t,pos,dt)
m = (np.sin(np.exp(-c[1]*t)*c[0]/2))**2
# Takes account of change of frequency with amplitude.
w = np.pi*c[2]/2/sym.elliptic_k(m)
phase = np.cumsum(w)*dt
fit = c[0]*np.exp(-c[1]*t)*np.cos(phase+c[3])+c[4] # Uses c values from fmin.
return fit
拟合参数c是可以猜测的,比如c=np.array[1,1,1,1]。pos数组是记录在.txt文件中的数据,我不知道如何上传,但您可以使用pos=exp-arange0,6,0.01*sin0.5*arange0,6,0.01+3等函数进行尝试
我实际上是在把它从Matlab翻译成Python。我想我的答案很接近,但我不太确定上面以a开头的行。可能是@horchler的重复:是的,但是有没有办法修改那里给出的答案以合并ellipmodel函数?当然有办法。它们只是不同的函数名。您使用ellipmodel,另一个答案使用香蕉。我建议阅读文档并尝试其他问题的答案所建议的内容。如果你不能让它工作,那么你可能会有一个不重复的问题,关于什么东西不工作。
# c[0]=theta_m, c[1]=alpha, c[2]=omega_0,
# c[3]=phi, c[4]=const.
# pos is the position data
def expfit(c,pos):
# Determines a fit to the large amplitude decay of the undriven pendulum.
min = 150
t = 0.001 * np.arange(min,len(pos)+1) # Converts data to seconds.
pos = pos[min:len(pos)]
### options = optimset('MaxIter',5000) # Determines the number of iterations.
dt = t[1] - t[0]
# Minimise the function ellipmodel.
### c = fminsearch('ellipmodel',c(),options,t,pos,dt)
m = (np.sin(np.exp(-c[1]*t)*c[0]/2))**2
# Takes account of change of frequency with amplitude.
w = np.pi*c[2]/2/sym.elliptic_k(m)
phase = np.cumsum(w)*dt
fit = c[0]*np.exp(-c[1]*t)*np.cos(phase+c[3])+c[4] # Uses c values from fmin.
return fit