OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Curve fitting with given data points

  • Thread starter Thread starter skug
  • Start date Start date
S

skug

Guest
For an exercise I have to fit the parameters a,b,c,d of the following formula: (https://i.sstatic.net/YjjTASGx.png) So that the formula best describes a given data set of temperature and time.

This is my approach (Tried to do the fitting with gaussnewton) so far, but it doesn't work at all:

Code:
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]))

tried gaussnewton but the fit doesn't make sense
<p>For an exercise I have to fit the parameters a,b,c,d of the following formula: (<a href="https://i.sstatic.net/YjjTASGx.png" rel="nofollow noreferrer">https://i.sstatic.net/YjjTASGx.png</a>)
So that the formula best describes a given data set of temperature and time.</p>
<p>This is my approach (Tried to do the fitting with gaussnewton) so far, but it doesn't work at all:</p>
<pre><code>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]))


</code></pre>
<p>tried gaussnewton but the fit doesn't make sense</p>
 

Latest posts

M
Replies
0
Views
1
MOHAMED AMIIN ABDI AADAN
M
Top