Hello could you help me answer this question in python? This question continues
ID: 3861816 • Letter: H
Question
Hello could you help me answer this question in python? This question continues from a previous assignment. I have the code it is asking about here:
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import poisson
beta=0.15
gamma=0.015
tmax=2000
def step(n):
r=beta+gamma*n
dt=-np.log(np.random.rand())/r # time increment
xi=beta/r
if (np.random.rand()<xi):
dn=1 #birth
else:
dn=-1 #death
return dn,dt
# three realizations
for i in range (0,3):
n=[0]
t=[0]
nc=0
tc=0
while (tc<tmax):
dn,dt=step(nc)
tc+=dt
nc+=dn
n.append(nc)
t.append(tc)
plt.plot(t,n)
plt.xlabel(r'time')
plt.ylabel(r'number of cells')
plt.axis([0, 2000, 0, 20])
# analytical mean solution
def f(t):
return (1-np.exp(-t*gamma))*beta/gamma
t=np.linspace(0,2000,100)
plt.plot(t,f(t))
plt.show()
plt.clf()
#histogram of final values
nf=[]
for i in range (0,500):
nc=0
tc=0
while (tc<tmax):
dn,dt=step(nc)
tc+=dt
nc+=dn
nf.append(nc)
hist, bin_edges = np.histogram(nf,density=True)
bin_centres = (bin_edges[:-1] + bin_edges[1:])/2
plt.plot(bin_centres, hist, '.-',markersize=15)
n=np.linspace(1,20,20)
plt.plot(n,poisson.pmf(n,beta/gamma))
plt.axis([0, 20, 0, 0.15])
plt.show()
Explanation / Answer
import random import matplotlib.pyplot as plt from math import log, e, ceil, floor,sqrt,pi,exp import numpy as np from numpy import arange,array, empty import pdb from random import randint import copy from operator import add import scipy from scipy.optimize import curve_fit import numpy as np import matplotlib.pyplot as plt import math import scipy def walk(n): time = 0 i = 0 while iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.