我一直在努力拟合本文http://arxiv.org/ftp/arxiv/papers/1202/1202.4380.pdf中公式21给出的傅立叶级数解。但是代码并没有正确地最小化chi_squared函数。我查看了scipy.optimize的文档,并设法最小化其他功能。你能否为我的代码建议修改或建议替代方案。
from __future__ import division
import numpy
import scipy.optimize
Z = 0.0314 #m length between the thermocouples
T1_data = numpy.array( [20.0,20.1,20.4,21.0,21.8,22.4,23.1,23.8,25.5,25.2,26.0,26.7,27.3,28.0,28.7,29.3,29.9,30.6,31.2])
T_Theory = numpy.array( [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0])
T2_data = numpy.array( [20.0,20.6,21.7,22.6,23.6,24.4,25.0,25.7,26.5,27.2,27.9,28.6,29.3,30.0,30.7,31.3,31.9,32.6,33.2])
time_diff = 10
f = 100
T1_max = T1_data.max()
T1_data = T1_data/T1_max
T2_max = T2_data.max()
T2_data = T2_data/T2_max
def omega_n(n):
return ((2*n)+1)*numpy.pi / (2*Z)
def phi_n(n):
return numpy.sin(omega_n(n)*Z)
def F_n(n):
return 2/ (Z*omega_n(n))
def theoretical2(dif):
T_Theory[0]=T1_data[0]
for d in range(1,len(T1_data)): # Calculate the theoretical data
for n in range((f+1)): # n is the power of the fourier co-efficient go up to
for p in range(1, d+1):
exp_pow3 = -1 * dif * time_diff * (d-p)* (omega_n(n))**2
T_Theory[d] = T_Theory[d-1] + (T1_data[(p-1)] - T1_data[p])*F_n(n)*phi_n(n)*numpy.exp(exp_pow3)
def chi_squared(dif):
theoretical2(dif)
chi_sum = 0
for c in range ( len(T2_data)):
chi_sum = chi_sum + (T_Theory[c] - T1_data[c])**2
return chi_sum
if __name__ == "__main__":
x0 = 0.0075
diff = scipy.optimize.minimize(chi_squared,x0)
print diff
感谢您查看我的代码。任何帮助将不胜感激。
谢谢你,Alex