我必须计算材料的腐烂率。我在5年内对材料进行了5次测量。下面是我的方法:-
我已经添加了初始和测试不确定性,如使用蒙特卡洛法的噪声。
最初,我假设速率是线性的,使用np.linalg.lstsq我已经计算了速率,基本上是直线的斜率。
现在我要假设速率是加速曲线来计算速率,材料损失越大,衰减越大。
这类似于:-
np.random.seed(0)
x_data = np.linspace(0, 5, num=50).reshape(-1,1)
# function something in the form of y = variable^x, taking variable value as
#1.1 below
var = 1.1
y_data = np.power(var, x_data)
plt.figure(figsize=(6, 4))
plt.scatter(x_data, y_data)
plt.show()
我查看了np.polyfit和curve_fit选项,但仍在尝试计算出速率。
答案 0 :(得分:0)
使用 scipy.optimize 中的 curve_fit 方法。拟合的合适模型是指数衰减函数。但是,curve_fit方法将估计任何函数的参数。
from scipy.optimize import curve_fit
import numpy as np
from matplotlib import pyplot
def decay(t,b,c):
# A = initial amount
# B = decay constant
# t = time
# C = additive constant
x0 = 100
return x0 * np.exp(-b * t) + c
t1 = [10,20,70,100,110] # experimental time data
y1 = [80,75,78,44,46] # experimental final amount data
# use curve_fit to estimate B (decay paramter) and C (additive paramter)
params = curve_fit(decay,t1, y1)
pyplot.plot(t1,y1) # actual
pyplot.plot(t1,[decay(i,*params) for i in t1]) # predicted
pyplot.show()
print(params)