Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.08 KB | None | 0 0
  1. import numpy as np
  2. from scipy.integrate import odeint
  3. import matplotlib.pyplot as plt
  4. from scipy.optimize import curve_fit
  5. import os
  6. import pandas as pd
  7.  
  8. def func_ode(y,t,qS,qSq,betaV1,betaV2,beta1,beta2,e1,eta1,eta2,etaH,delta2,deltaH,theta2,f1,f2,fH,d):
  9.    
  10.     Sq,S,E1,E2,H,R,D,V=y
  11.  
  12.     dSq=qS*S-qSq*Sq
  13.     dS=qSq*Sq-(betaV1*V+betaV2*V+beta1*E1+beta2*E2)*S
  14.     dE1=(betaV1*V+beta1*E1)*S-(e1+eta1)*E1
  15.     dE2=(betaV2*V+beta2*E2)*S+e1*E1-(eta2+delta2+theta2)*E2
  16.     dH=theta2*E2-(etaH+deltaH)*H # theta is for recovered
  17.     dR=eta1*E1+eta2*E2+etaH*H # eta is for recovered
  18.     dD=delta2*E2+deltaH*H # delta is for death
  19.     dV=f1*E1+f2*E2+fH*H-d*V
  20.  
  21.     dy=[dSq, dS, dE1, dE2, dH, dR, dD, dV]
  22.     return dy
  23.  
  24. def func_y(t,qS,qSq,betaV1,betaV2,beta1,beta2,e1,eta1,eta2,etaH,delta2,deltaH,theta2,f1,f2,fH,d,y_0):
  25.     """
  26.    Solution to the ODE y'(t) = f(t,y,parameters) with initial condition y(0) = y_0
  27.    """
  28.     y = odeint(func_ode, y_0, t, args=(qS,qSq,betaV1,betaV2,beta1,beta2,e1,eta1,eta2,etaH,delta2,deltaH,theta2,f1,f2,fH,d))
  29.     return y.flatten()
  30.  
  31. if __name__ == "__main__"# print(numpy_data)
  32.     # row,col=np.shape(numpy_data)
  33.     # for i in range(row):
  34.     #     for j in range(col):
  35.     #         print(type(numpy_data[i,j]))
  36.     # exit()
  37.  
  38.     ## Parameters (dummy)
  39.     qS, qSq, betaV1, betaV2, beta1, beta2, e1, eta1, eta2, etaH, delta2, deltaH, theta2, f1, f2, fH, d = \
  40.         0, 1e-4, 4e-9, 1e-9, 4e-9, 1e-9, 1/100, 1/21, 1/104, 1/10, 1/200, 1/10400, 1/3.5, 1400, 1000, 1700, 144
  41.  
  42.     # Used Data
  43.     y_0=numpy_data[0,:].tolist()
  44.     numpy_data=numpy_data[1:60,:]
  45.  
  46.     ## Reference Time
  47.     number_of_reference_time,_=np.shape(numpy_data)
  48.  
  49.     ## Solve
  50.     param = qS, qSq, betaV1, betaV2, beta1, beta2, e1, eta1, eta2, etaH, delta2, deltaH, theta2, f1, f2, fH, d, y_0
  51.     t= np.linspace(1,number_of_reference_time,number_of_reference_time)
  52.     popt, cov = curve_fit(func_y, t, numpy_data.flatten(),p0=[param])
  53.  
  54.     qS,qSq,betaV1,betaV2,beta1,beta2,e1,eta1,eta2,etaH,delta2,deltaH,theta2,f1,f2,fH,d = popt
  55.  
  56.     ## Check Result
  57.     result=odeint(func_ode,y_0,t,args=(qS,qSq,betaV1,betaV2,beta1,beta2,e1,eta1,eta2,etaH,delta2,deltaH,theta2,f1,f2,fH,d))
  58.  
  59.     ## Plot
  60.     plt.plot(t, result[:, 0], label='Sq')
  61.     plt.plot(t, result[:, 1], label='S')
  62.     plt.plot(t, result[:, 2], label='E1')
  63.     plt.plot(t, result[:, 3], label='E2')
  64.     plt.plot(t, result[:, 4], label='H')
  65.     plt.plot(t, result[:, 5], label='R')
  66.     plt.plot(t, result[:, 6], label='D')
  67.     plt.plot(t, result[:, 7], label='V')
  68.     plt.legend(loc='best')
  69.     plt.xlabel('t')
  70.     plt.grid()
  71.     plt.show()
  72.     pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement