首页 > 解决方案 > 仿真中的命令行参数

问题描述

我正在尝试输入命令行参数以从命令行设置 a 和 r 的值。我对python真的很陌生,所以任何帮助都将不胜感激。a = 1/(# 感染天数) r = 疾病的传染性

import matplotlib.pyplot as plt
import numpy as np
import sys


population = 763
Scur = population -1    # number of people susceptible
Icur = 1                # number of people infected
Rcur = 0                # number of people recovered

trans_const = 0.00218   # infectiousness of disease r = kb/N
recov_rate = 0.5        # recovery rate a = 1/(# days infected)
simlength = 20          # number of days in simulation

SIRarray = np.zeros((simlength+1,3)) # using floats as ~% of popn
SIRarray[0,:] = Scur, Icur, Rcur     # record initial values

for i in range(1, simlength+1):
    new_infected = trans_const * Scur * Icur   # = rSI
    new_recovered = recov_rate * Icur          # = aI

    Scur = Scur - new_infected
    Icur = Icur + new_infected - new_recovered
    Rcur = Rcur + new_recovered

    SIRarray[i,:] = Scur, Icur, Rcur

print("SIR Model Simulation") 
print("Scur\t\tIcur\t\tRcur")
print("----------------------------------------")
for i in range(len(SIRarray)):
    print("{0:.2f}\t\t{1:.2f}\t\t {2:.2f}".format(SIRarray[i,0],
                                           SIRarray[i,1], SIRarray[i,2]))
a = int(sys.argv[1])
r = int(sys.argv[1])


plt.plot(SIRarray[:,0], "b")
plt.plot(SIRarray[:,1], "r")
plt.plot(SIRarray[:,2], "g" )
plt.title("SIR model parameters r = " + str(trans_const) + " a = " + str(recov_rate))
plt.xlabel("Number of People")
plt.ylabel("Number of People")
plt.legend(['Susceptible People', 'Infected', 'Recovered', 'y = 4x'],
loc='upper left')
plt.legend(['Susceptible People', 'Infected', 'Recovered', 'y = 4x'],
loc='upper left')
plt.savefig('SIR Model Simulation')
plt.show()

标签: python-3.x

解决方案


推荐阅读