-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFitting.py
44 lines (30 loc) · 1.21 KB
/
Fitting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
x_data = np.array([40,50,60,70,80,160,250])
#y_data = np.array([30859.375,30156.25,29531.25,29062.5,28750,28066.406,2597.66])
y_data = np.array([0,2.278481013,4.303797468,5.82278481,6.835443038,9.050633722,10.56962106])
params = np.array([1,1])
#fit = np.polyfit(np.log(x_data), y_data)
#print(fit)
def funcinv(x, a, b, c):
# The inverse funciton
return c*np.power((a+x), b)
# popt - optimal params after least squares
# pcov - estimated covariance of popt
# for more information see docs https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html
popt, pcov = curve_fit(funcinv, x_data, y_data)
print(popt)
print(pcov)
# r squared calculation
# see https://stackoverflow.com/questions/19189362/getting-the-r-squared-value-using-curve-fit
residuals = y_data - funcinv(x_data, *popt)
ss_res = np.sum(residuals**2)
ss_tot = np.sum((y_data-np.mean(y_data))**2)
r_squared = 1 - (ss_res / ss_tot)
print("R^2 : " + str(r_squared))
tStop = 300
t = np.linspace(30, tStop, tStop*10)
# Plot the data on three separate curves for S(t), I(t) and R(t)
plt.plot(x_data,y_data, 'ro', t, funcinv(t, *popt), "b")
plt.show()