的问题是,你最初的猜测是从实际的解决方案很远:数据我目前使用的设置。如果添加打印语句中chisqfunc()像print (a,b),并重新运行你的代码,你会得到这样的:
(0, 0)
(1.4901161193847656e-08, 0.0)
(0.0, 1.4901161193847656e-08)
这意味着minimize只在这些点计算函数。
,如果你现在尝试在这3对值来评估chisqfunc(),你会发现它们完全匹配,例如
print chisqfunc((0,0))==chisqfunc((1.4901161193847656e-08,0))
True
这是因为四舍五入的浮动点算术的。换句话说,在评估stress - model时,变量stress的数量级比model大得多,并且结果被截断。
然后可以尝试bruteforcing它,提高浮点精度,在用loadtxt加载数据之后编写。 minimize失败,与result.success=False,但与一个有用的消息
由于精度损失不一定达到所需的错误。
一种可能性是再提供一个更好的初始猜测,所以,在减法stress - model的model部分是相同的数量级,另重新调整数据,因此该解决方案将是更接近你初步猜测(0,0)。
它是MUCH更好,如果你只是重新缩放数据,使得例如无量纲相对于一定的应力值(如yelding /该材料的开裂)
这是装配件的一个例子,将最大测量的应力用作应力等级。有从您的代码非常少的变化:
import numpy
import scipy.optimize as opt
filename = 'data.csv'
data = numpy.loadtxt(open(filename,"r"),delimiter=",")
stress = data[:,0]
strain = data[:,1]
err_stress = data[:,2]
smax = stress.max()
stress = stress/smax
#I am assuming the errors err_stress are in the same units of stress.
err_stress = err_stress/smax
def chisqfunc((a, b)):
model = a + b*strain
chisq = numpy.sum(((stress - model)/err_stress)**2)
return chisq
x0 = numpy.array([0,0])
result = opt.minimize(chisqfunc, x0)
print result
assert result.success==True
a,b=result.x*smax
plot(strain,stress*smax)
plot(strain,a+b*strain)
你的线性模型相当不错的,即你的材料具有这个范围的变形的非常线性的行为(什么材料也无妨?):