Curve_fit not fitting to SIR model

Viewed 12

My question was to fit an SIR model to data. The two sets of data are xdata, and ydata1 in the code. For some reason the curve fit is coming to a point rather than being rounded at the top. I have tried playing around with the def of fit_odeint, 'popt,pcov', and 'fitted'. With no luck to getting the curve to change, i only have gotten errors when playing around with the code.

N = 763
I0 = 1/N
S0 = N - I0
R0 = 0.0
ydata1 = [3,8, 28,75, 221, 291, 255, 235, 190, 125, 70, 28, 12, 5]
ydata = [y/N for y in ydata1]
xdata = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14']
xdata = [float(t) for t in xdata]

ydata = np.array(ydata, dtype=float)
xdata = np.array(xdata, dtype=float)

def sir_model(y, x, beta, gamma):
    S = -beta * y[0] * y[1] / N
    R = gamma * y[1]
    I = -(S + R)
    return S, I, R

def fit_odeint(x, beta, gamma):
    return odeint(sir_model, (S0, I0, R0), x, args=(beta, gamma))[:,1]

popt, pcov = curve_fit(fit_odeint, xdata, ydata)
fitted = fit_odeint(xdata, *popt)

plt.plot(xdata, ydata, 'o')
plt.plot(xdata, fitted)
plt.show()

The photo below is the graph [1] that I am receiving with my current code. [1]: https://i.stack.imgur.com/kQ7Nn.png

Where am I going wrong for the fit curve?

0 Answers
Related