2016-11-14 38 views
0

使用簡單的SIR模型。 我需要組織打印到終端的值,以我的老師可以輕鬆遵循的方式進行組織。 我被告知要查看numpy.zeros,但我是python的新手,我需要某人來幫助。如何使用numpy零來組織Python數組

import matplotlib.pyplot as plt 
import numpy 
beta = 0.24 
gamma = 0.142857 
Tstart = 0 
Tend = 151 
r = 0 
s = (306.8 * 10**6) 
i = (22 * 10**6) 

def compute_next_day(t,R,I,S): 
    R[t] = gamma * I[t - 1] + R[t - 1] 
    I[t] = (beta * I[t-1] * S[t-1]/(r+i+s)) - gamma * I[t-1] + I[t-1] 
    S[t] = - (beta * I[t-1] * S[t-1]/(r+i+s)) + S[t-1] 
    print S[t-1], I[t-1], R[t-1] 


def compute_entire_period(Tstart, Tend, R, I, S): 
    R[Tstart] = r 
    I[Tstart] = i 
    S[Tstart] = s 
    for t in range(Tstart + 1, Tend): 
     compute_next_day(t, R, I, S) 


R = range(Tstart, Tend) 
I = range(Tstart, Tend) 
S = range(Tstart, Tend) 

def graph(R, I, S): 
    plt.plot(R) 
    plt.plot(I) 
    plt.plot(S) 

plt.ylabel("Population") 
plt.xlabel("Time") 
plt.show() 

(compute_entire_period(Tstart, Tend, R, I, S)) 
(graph(R,I,S)) 

打印到終端的值是S,I,R值...

what currently appears on terminal want to organize like this

+1

我覺得你的問題很不清楚。你到底想做什麼? –

+1

顯示示例 - 您得到了什麼以及您的期望。 – furas

+0

添加了一些圖片,所以你可以看到我要去的 –

回答

0

我沒有足夠的積分發表評論,所以我會「的答案「在評論部分的您的問題,你可以這樣的價值出口到CSV:

import csv 

a = range(2, 10) 
with open("output.csv", "wb") as file: 
    writer = csv.writer(file) 
    writer.writerows(a)