我是python的新手,希望有人可以帮助我。我有这个代码,我想用它来拟合实验数据:
import numpy as np
import scipy
import pandas as pd
import sys
from scipy.optimize import minimize
fileID = open("testdata.txt", "r+")
(t,Ids_measured) = np.loadtxt(fileID,unpack =True)
try:
all_the_file = fileID.readlines()
finally:
fileID.close()
Ids_final = Ids_measured[-1]
Ids_m = ()
def model_Ids(x0):
Amp = x0[0]
ß = x0[1]
tau = x0[2]
for i in range(len(t)):
Ids_m=(Ids_final - sum(Amp* (np.exp((-(t/ tau))))**ß))
return Ids_m
def objective(x0):
Ids_model = ()
Ids_m = ()
Ids_model =model_Ids(x0)
obj = 0.0
for i in range(len(Ids_measured)):
obj = obj + ((Ids_m[i]-Ids_measured[i])/Ids_measured[i])**2
return obj
x0 = np.zeros(3)
x0[0] = 0.0 # Amp
x0[1] = 0.0 # ß
x0[2] = 0.0 # tau
首先,我想加载一个文本文件并使用数学方程求解Ids_m(建模部分)。由于Ids_m的等式具有一些模型变量,我想稍后改变它以匹配目标函数中定义的目标。问题是当我运行代码时,我通常得到Ids_m的标量而不是t的所有值的向量。因此我有错误:
obj = obj + ((Ids_m[i]-Ids_measured[i])/Ids_measured[i])**2
IndexError: tuple index out of range
有人可以帮我指出这行代码中的错误。我想函数model_Ids(x0)返回一个长度等于t的长度的向量
感谢。