так, чтобы формула лучше всего описывала заданный набор данных о температуре и времени.
Это мой подход (пытался выполнить подгонку с помощью гауссньютона), но он вообще не работает:
Код: Выделить всё
import numpy as np
import matplotlib.pyplot as plt
# Laden der gemessenen Temperaturen
_, T_measured = np.genfromtxt("4_temperatures.txt", unpack=True)
t_measured = np.arange(T_measured.shape[0]) # Anzahl der Tage seit der ersten Messung
def T(t, p):
a, b, c, d = p
return (a * np.sin((t-b)/c) + d)
def DT(t, p):
a, b, c, d = p
return np.array([
np.sin((t-b)/c),
-a * (t-b) * np.cos((t-b)/c) / c**2,
a * np.cos((t-b)/c),
np.ones_like(t)
]).T
def gn(x0, T, DT, tol): #gaussnewton
x = x0
for i in range(1000):
s = np.linalg.lstsq(DT(t_measured, x), T_measured - T(t_measured, x), rcond=None)[0]
x = x - s
if np.linalg.norm(s) < tol * np.linalg.norm(x):
return x
if __name__ == "__main__":
p0 = [0.8, -1, 50, 0] # Startwerte, guess [a, b, c, d]
p = gn(p0, T, DT, 10**(-14)) # Anpassung des Konvergenzkriteriums
fit = T(t_measured, p)
plt.figure()
plt.plot(t_measured, T_measured, label='data')
#plt.plot(t_measured, T(t_measured, p0), label='Anfangsschätzung')
plt.plot(t_measured, fit, label='bester fit')
plt.legend()
plt.xlabel('Tage seit erster Messung')
plt.ylabel('Temperatur')
plt.savefig("4_mythenquai.png")
# Ausgabe der Parameter
print("Startwerte:")
print("a = {:.3f}".format(p0[0]))
print("b = {:.3f}".format(p0[1]))
print("c = {:.3f}".format(p0[2]))
print("d = {:.3f}".format(p0[3]))
print("\nBeste Anpassung:")
print("a = {:.3f}".format(p[0]))
print("b = {:.3f}".format(p[1]))
print("c = {:.3f}".format(p[2]))
print("d = {:.3f}".format(p[3]))
очень плохо подходит
Данные: введите здесь описание изображения
p>
Данные: https://sharetxt.live/txtstackoverflow
Blockquote
Подробнее здесь: https://stackoverflow.com/questions/786 ... ata-points