I'm trying to fit function to a data set of an experiment using python. I can get a really good approximation and the fit looks pretty good, but the error given for the parameters is incredibly high and I'm not sure how to fix this.
The function looks like this: Function
The data consist of the a time data set and a y data set. The variable "ve" is a linear velocity function, that's why in the code it is replaced with "a*x+b". Now the fit looks really good and theoretically the function should fit the data, but the error is crazily high. The code is the following:
import operator
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from lmfit import Model
from numpy import log, linspace, random
from matplotlib import colors as mcolors
from scipy.optimize import curve_fit
data6 = pd.read_csv('2594.csv')
x=data6.iloc[:18,0]
y=data6.iloc[:18,1]
def func(x, a, b, mo, q):
return (4.9+a*x+b)*x+(a*x+b)*((mo/q)-x)*log(1-(q*x)/mo)-0.5*9.8*x*x
popt, pcov = curve_fit(func,x,y,bounds=((0, -100, 0, 0), (1000, 1000, 1, 1)))
plt.plot(x, func(x, *popt), 'g--', label='fit: a=%5.3f, b=%5.3f, mo=%5.3f,
q=%5.3f' % tuple(popt))
plt.plot(x,y,':', label='Daten')
plt.grid(True)
plt.legend(loc='upper left')
plt.xlabel("t [s]")
plt.ylabel("z [m]")
plt.title('Anpassung vor Zeitpunkt T', )
plt.savefig('fit1.pdf')
plt.show()
Here is the fit for this line of code: Fit1
and the covariance Matrix:
[[ 3.66248820e+09 2.88800781e+09 -5.59803683e+06 -4.01121935e+05]
[ 2.88800781e+09 2.27731332e+09 -4.44058731e+06 -3.17108449e+05]
[-5.59803683e+06 -4.44058731e+06 2.43805434e+05 7.83731345e+03]
[-4.01121935e+05 -3.17108449e+05 7.83731345e+03 2.65778118e+02]]
I also tried the following fit mode but I become errors of over 1400%:
fmodel = Model(func)
result = fmodel.fit(y, x=x, a=14, b=3.9, mo=0.8, q=0.002)
This is the fit report:
a: 926.607518 +/- 182751.047 (19722.59%) (init = 14)
b: 737.755741 +/- 143994.520 (19517.91%) (init = 3.9)
mo: 0.27745681 +/- 27.5360933 (9924.46%) (init = 0.8)
q: 0.00447098 +/- 0.60437392 (13517.72%) (init = 0.002)
And this is the resulting fit: Fit2 I would really appreciate some help. If possible a simple guide on how to minimize the error of the function!
The data looks like this:
x=[0.0333 0.0667 0.1 0.133 0.167 0.2 0.233 0.267 0.3 0.333
0.367 0.4 0.433 0.467 0.5 0.533 0.567 0.6 ]
y=[0.104 0.249 0.422 0.6 0.791 1. 1.23 1.47 1.74 2.02 2.33 2.64
2.99 3.34 3.71 4.08 4.47 4.85 ]
Thank you!