def integrate(self, theta0, tf=100, h=0.01): """Numerical integation of the Kuramoto Model with odeint (tau=0) or ddeint (tau>0). Parameters ---------- theta0 : ndarray of shape (N) Initial condition. If tau>0, (lambda t: theta0 - t * self.w) is used as initial condition. tf : float The end of the integration interval. It starts at t=0. h : float Numerical integration step. Returns ------- t : ndarray Time discretization points, t = [0, h, ..., tf-h, tf]. theta : ndarray of shape (len(t), N) Numerical solution for each time in t. """ t = np.arange(0, tf + h / 2, h) if self.tau == 0: theta = odeint(self, theta0, t, hmax=h) else: theta0_hist = lambda t: theta0 + t * self.w theta = ddeint(self, theta0_hist, t) return t, theta
def evolution(network_type, N, beta, seed, arguments, d1, d2, d3, d=None): """TODO: Docstring for evolution. :arg1: TODO :returns: TODO """ A, A_interaction, index_i, index_j, cum_index = network_generate( network_type, N, beta, seed, d) beta_eff, _ = betaspace(A, [0]) weight = beta / beta_eff A = A * weight g = lambda t: np.ones(N) * 5 t = np.arange(0, 100, 0.01) dyn_all = ddeint(mutual_multi_delay, g, t, fargs=(d1, d2, d3, N, index_i, index_j, A_interaction, cum_index, arguments)) plt.plot(t, np.mean(dyn_all, 1), alpha=alpha) plt.subplots_adjust(left=0.18, right=0.98, wspace=0.25, hspace=0.25, bottom=0.18, top=0.98) plt.xticks(fontsize=ticksize) plt.yticks(fontsize=ticksize) plt.xlabel('$t$', fontsize=fs) plt.ylabel('$\\langle x \\rangle$', fontsize=fs) plt.show()
def test_variable_delay(): def model(Y, t): return -Y(t - 3 * cos(Y(t))**2) tt = linspace(0, 30, 2000) yy = ddeint(model, lambda t: 1, tt) assert abs(yy[-1] - 0.716) < 0.01
def model8(x, ts, P): x0 = x[0] k = x[1] a = x[2] T = x[3] def model(Y, t): ynow = Y(t)[0] ythen = Y(t - T)[0] active = ynow - ythen rate = (k * active / (1.0 + a * t)) * (P - ynow) / P return np.array([rate, 0.0]) def values_before_zero(t): if t < -T: return np.array([0.0, 0.0]) elif t < 0.0: return np.array([x0 * (T + t) / T, 0.0]) else: return np.array([x0, 0.0]) ys = ddeint(model, values_before_zero, ts) return ys[:, 0]
def test_sine_example(): def model(Y, t): return Y(t - 3 * pi / 2) # Model tt = linspace(0, 50, 10000) # Time start, time end, nb of pts/steps g = sin # Expression of Y(t) before the integration interval yy = ddeint(model, g, tt) # Solving assert abs(yy[-1] - (-0.63)) < 0.01
def test_lotka_volterra_example(): def model(Y, t, d): x, y = Y(t) xd, yd = Y(t - d) return array([0.5 * x * (1 - yd), -0.5 * y * (1 - xd)]) g = lambda t: array([1, 2]) tt = linspace(2, 30, 20000) dd = [0, 0.2] expected_values = array([[0.502, 1.640], [4.576, 0.577]]) for d, expected_value in zip(dd, expected_values): print("Computing for d=%.02f" % d) yy = ddeint(model, g, tt, fargs=(d, )) err = yy[-1] - expected_value assert err.dot(err.T) < 0.001
def delayed_flows(system, dynamic_state = None, L = None, fs = None, SampleSize = None, parameters = None, InitialConditions = None): import numpy as np run = True if run == True: if system == 'mackey_glass': #This requires installation of ddeint (pip install ddeint) from ddeint import ddeint #setting simulation time series parameters if fs == None: fs = 5 if SampleSize == None: SampleSize = 1000 if L == None: L = 400 t = np.linspace(0, L, int(L*fs)) #setting system parameters if parameters != None: if len(parameters) != 4: print('Warning: needed 4 parameters. Defaulting to periodic solution parameters.') parameters = None else: gamma, τ, B, n = parameters[0], parameters[1], parameters[2], parameters[3] if parameters == None: if dynamic_state == 'periodic': n = 7.75 if dynamic_state == 'chaotic': n = 9.65 τ = 2.0 B = 2.0 gamma = 1.0001 def mackey_glass(X, t, d): x = X(t) xd = X(t-d) return B*(xd/(1+xd**n)) - gamma*x fsolve = 50 tt = np.linspace(0, L, int(L*fsolve)) g = lambda t: np.array([1, 1]) d = τ states = ddeint(mackey_glass, g, tt, fargs=(d,)).T ts = [((states[0])[::int(fsolve/fs)])[-SampleSize:]] t = tt[::int(fsolve/fs)][-SampleSize:] return t, ts
def make_hill(a, time_grid): " Solve DDE and plot a hill " b = 0.10 + math.sqrt(0.02 + math.pow(a, 0.75)) * 0.135 g = 0.06 derivatives = lambda Y, t: model(Y, t, a, b, g) yy = ddeint(derivatives, values_before_zero, time_grid) infected = yy.T[1] stopped = [i < 0.5 * infected[0] for i in infected] if any(stopped): stop_ndx = stopped.index() else: stop_ndx = len(infected) - 1 infected = infected[:stop_ndx] position = [math.log(i) for i in infected] velocity = moving_average(np.gradient(position), 10) acceleration = moving_average(np.gradient(velocity), 10) hill_x, hill_y = hill(acceleration) return hill_x, hill_y
def predict_cell_concentration(self, simulation_time, temperature_profile, initial_concentration=1.0, plot=False) -> float: self.temperature_profile = temperature_profile tt = linspace(0, simulation_time, 1000) # simulation_data = ddeint(self.model, self.values_before_zero, tt) simulation_data = ddeint( func=self.model, g=lambda t: np.array([initial_concentration, 1.0]), tt=tt) if plot: fig, ax = subplots(1, figsize=(8, 4)) ax.plot(tt, [x[0] for x in simulation_data], label="cell concentration") fig.legend(loc='upper center', borderaxespad=2.0) fig.show() return simulation_data[-1][0] # return last value
def solve(days_on=c.days_on, lam_up=c.lam_up, lam_down=c.lam_down, func_form=c.func_form, e_dose=None, p_dose=None, missed_start=c.missed_start, days_missed=c.days_missed): """ Params: days_on (float) number of days taking hormonal birth control lam_up (float) if func_form is "soft_step", lam_up is lam_up param for soft_step() lam_down (float) if func_form is "soft_step", lam_down is lam_down param for soft_step() if func_form is "exp_decay", lam_down is the lam param for exp_decay() if func_form is "lin_decay", lam_down is the intercept param for lin_decay() func_form (string) indicates which functional form is being used to represent hormone dosing e_dose (float) default value is None, if e_dose = None, e_dose = c.e_dose p_dose (float) default value is None, if p_dose = None, p_dose = c.p_dose Returns: tt (ndarray) yy (ndarray) """ params = c.Params() print(f"missed days: {days_missed}") print(f"missed start: {missed_start}") if e_dose is not None: params.e_dose = e_dose if p_dose is not None: params.p_dose = p_dose tt = np.linspace(0, c.upper_bound, c.num_samples) yy = ddeint(derivs, initial_conditions, tt, fargs=(params, days_on, lam_up, lam_down, func_form, missed_start, days_missed)) return tt, yy
""" Lotka volterra DDE """ from pylab import * from ddeint import ddeint def model(Y, t, d): x, y = Y(t) xd, yd = Y(t - d) return array([0.5 * x * (1 - yd), -0.5 * y * (1 - xd)]) g = lambda t: array([1, 2]) tt = linspace(2, 30, 20000) fig, ax = subplots(1, figsize=(4, 4)) for d in [0, 0.2]: print("Computing for d=%.02f" % d) yy = ddeint(model, g, tt, fargs=(d, )) # WE PLOT X AGAINST Y ax.plot(yy[:, 0], yy[:, 1], lw=2, label='delay = %.01f' % d) show()
""" Reproduces the sine function using a DDE """ from pylab import * from ddeint import ddeint model = lambda Y,t : Y(t - 3*pi/2) # Model tt = linspace(0,50,10000) # Time start, time end, nb of pts/steps g=sin # Expression of Y(t) before the integration interval yy = ddeint(model,g,tt) # Solving fig, ax = subplots(1,figsize=(4,4)) ax.plot(tt,yy) show()
""" Lotka volterra DDE """ from pylab import * from ddeint import ddeint def model(Y,t,d): x,y = Y(t) xd,yd = Y(t-d) return array([0.5*x*(1-yd), -0.5*y*(1-xd)]) g = lambda t : array([1,2]) tt = linspace(2,30,20000) fig, ax = subplots(1,figsize=(4,4)) for d in [0, 0.2]: print( "Computing for d=%.02f"%d ) yy = ddeint(model,g,tt,fargs=(d,)) # WE PLOT X AGAINST Y ax.plot(yy[:,0], yy[:,1], lw=2, label='delay = %.01f'%d) show()
from numpy import * import matplotlib.pyplot as plt samples = 1000 def model(Y, t): return -Y(t - 3 * cos(Y(t))**2) def history(t): return 1 ts = linspace(0, 150, samples) ys = ddeint(model, history, ts) ts1 = linspace(0, 150, 5 * samples) ys1 = ddeint(model, history, ts1) ts2 = linspace(0, 150, 10 * samples) ys2 = ddeint(model, history, ts2) fig, ax = plt.subplots() plt.xlabel("y(t)") plt.ylabel("t") plt.title('Simple DDE with varying sample size') l1, = ax.plot(ts, ys) l2, = ax.plot(ts1, ys1) l3, = ax.plot(ts2, ys2)
return array([10000.0, 1.0]) def model(Y, t): x, y = Y(t) tot = x + y return array([ 0.04*x - 0.01*x/tot, 0.06*y - 0.01*y/tot ]) tt0 = linspace(0, 6*1e1, 1000) tt1 = linspace(0, 6*1e2, 1000) yy0 = ddeint(model, values_before_zero, tt0) yy1 = ddeint(model, values_before_zero, tt1) fig, (ax0, ax1) = subplots(2, figsize=(8, 6)) ax0.plot(tt0, [x[0] for x in yy0], label="slower growing, quantitatively more") ax0.ticklabel_format(axis='y', style='sci', scilimits=(2,3)) ax0.plot(tt0, [x[1] for x in yy0], label="faster growing, quantitatively less") ax0.set_xlabel('Time [min]') ax0.set_ylabel('Number of Phages') ax0.legend(loc='upper left', borderaxespad=2.0) ax1.plot(tt1, [x[0] for x in yy1], label="slower growing, quantitatively more") ax1.plot(tt1, [x[1] for x in yy1], label="faster growing, quantitatively less")
# Perturbed 1. K = 1.3 T = 1.9 L = 0.4 #Perturbed 2. #K = 1.1 #T = 1.3 #L = 0.45 tt = linspace(0., 20., 2000) y_raw = ddeint(raw_sys, [g1], tt, fargs=( K, T, L, )) y_cl = ddeint(cl_sys, [g1], tt, fargs=( K, T, L, )) print("KM evaluation start!") y_it2fpid_KM = ddeint(model_KM, [g1, g2], tt, fargs=( K, T, L,
""" DDE with parameters. """ from pylab import * from ddeint import ddeint model = lambda Y, t: -Y(t - 3 * cos(Y(t))**2) tt = linspace(0, 30, 2000) yy = ddeint(model, lambda t: 1, tt) fig, ax = subplots(1, figsize=(4, 4)) ax.plot(tt, yy) show()
# Mackey-Glass chaotic series ### derivatives = lambda x, t, tau, beta, gamma, n: beta * x(t - tau) / (1 + pow( x(t - tau), n)) - gamma * x(t) tau = 17 beta = 0.2 gamma = 0.1 nn = 10 hist = lambda t: 1.2 if abs(t) < eps else 0.0 delta_t = 0.5 t_start = 0 t_end = 200 t_num = int((t_end - t_start) / delta_t) tt = np.linspace(t_start, t_end, t_num) yy = ddeint(derivatives, hist, tt, fargs=(tau, beta, gamma, nn)) # plt.figure(1) # plt.plot(tt, yy, lw=2) # plt.xlabel('t') # plt.ylabel('y') # plt.grid(True) # plt.figure(2) # plt.plot(yy[tau:len(yy)], yy[0:len(yy)-tau], lw=2) # plt.xlabel('y(t)') # plt.ylabel('y(t-tau)') # plt.grid(True) # plt.show() def f(f_hist, tau, beta, gamma, n, delta_t, sqrtQ):
""" Reproduces the sine function using a DDE """ from pylab import * from ddeint import ddeint model = lambda Y, t: Y(t - 3 * pi / 2) # Model tt = linspace(0, 50, 10000) # Time start, time end, nb of pts/steps g = sin # Expression of Y(t) before the integration interval yy = ddeint(model, g, tt) # Solving fig, ax = subplots(1, figsize=(4, 4)) ax.plot(tt, yy) show()
""" DDE with parameters. """ from pylab import * from ddeint import ddeint model = lambda Y, t: -Y(t - 3 * cos(Y(t)) ** 2) tt = linspace(0, 30, 2000) yy = ddeint(model, lambda t: 1, tt) fig, ax = subplots(1, figsize=(4, 4)) ax.plot(tt, yy) show()
#!/usr/bin/env python from pylab import * from ddeint import ddeint delta = 3 * pi / 2 model = lambda y, t: y(t - delta) # model tt = linespace(0, 50, 10000) # starting time, ending time, number of ponts f = cos #expression of Y(t) before the integration interval yy = ddeint(model, f, tt) # solve the equation #plotting plot(tt, yy, c='r', label="$y(t)$") plot(tt, cos(tt), c='b', label="$cos(t)$") set_ylim(ymax=2) #make room for the legend legend()
def foo(t): if t < 10.0: return 0.0 elif 10.0 <= t < 10.0 * np.pi: return 1.0 else: return 10.0 * sin(t * 0.5) def values_before_zero(t): return array([0.0, 0.0]) def model(Y, t): x, y = Y(t) x_tau, y_tau = Y(t - tau) return array([foo(t), x_tau]) tt = linspace(0, 100, 1000) yy = ddeint(model, values_before_zero, tt) fig, ax = subplots(1, figsize=(8, 4)) ax.plot(tt, [x[0] for x in yy], label="trace1") ax.plot(tt, [x[1] for x in yy], label="trace2") fig.legend(loc='upper center', borderaxespad=2.0) fig.show()
ynow = Y(t) active = ynow[0] - Y(t - T0)[0] pactive = Y(t - T1)[0] - Y(t - T1 - T0)[0] g = math.log(1.0 + pactive) dcases = (k * active / (1.0 + ynow[1])) * (1.0 - ynow[0] / P) dpolicy = a * g - b * ynow[1] return np.array([dcases, dpolicy]) ts = np.linspace(0.0, 1800.0, 5 * 1800 + 1) ys = ddeint(model0, values_before_zero, ts) # Considered multiple channels for various periods of incubation... but seems to get monstrously complicated quickly. fig = plt.figure(figsize=(15, 6)) plt.subplot(1, 2, 1) plt.plot(ts, ys[:, 0], color='blue') plt.plot(ts[140:], ys[140:, 0] - ys[:-140, 0], color='red') plt.yscale('symlog') plt.grid(True) plt.subplot(1, 2, 2) plt.plot(ts, ys[:, 1]) plt.grid(True)
d=0.1 P=1e7 a=0.01 # Needs to return derivatives # Need two channels 'cos something fails in ddeint otherwise def model0(Y,t): ynow=Y(t)[0] ythen=Y(t-T)[0] active=ynow-ythen+impulse(t,d) rate=(k*active/(1.0+a*t))*(P-ynow)/P print t,ynow,ythen,rate return np.array([rate,0.0]) ts=np.linspace(0.0,180.0,1800) ys=ddeint(model0,values_before_zero0,ts)[:,0] # Considered multiple channels for various periods of incubation... but seems to get monstrously complicated quickly. plt.plot(ts,ys,color='blue') plt.plot(ts,ys-np.concatenate([np.zeros((14,)),ys[:-14]]),color='red') plt.yscale('symlog') plt.grid(True) plt.show()
def test_simple_model(self): g = lambda t: np.array([1 + t, 2 - t]) # 'history' at t<0 tt = np.linspace(0, 30, 20000) # times for integration d = 0.5 # set parameter d yy = ddeint(model, g, tt, fargs=(d, )) # solve the DDE ! self.assertEqual(yy.shape[0], 20000)
0 if c_pnn < 0 else new_phage.burst_size * diffn - new_phage.infection_rate(c_host_b, c_pnn) - out_b(t) * c_pnn - new_phage.death_rate * c_pnn + in_lib(t) * R_pnn ]) def initial_conditions(t): return array([ new_host.c0, s0, 10**9, s0 * 5 * 10**3, 0.0, 0.0, original_phage.c0, new_phage.c0 ]) for elem in q_h_inf_poo: print(elem) data = ddeint(model, initial_conditions, xs, fargs=(d, )) plt.figure(figsize=(16, 16)) plt.subplot(3, 3, 1) plt.plot(xs, [data[t][0] for t in range(len(xs))], label="reactor A") plt.xlabel('time [min]') plt.ylabel('concentration [#bacteria/mL]') plt.title('Host Concentration A over Time') plt.legend() plt.subplot(3, 3, 2) plt.plot(xs, [data[t][1] for t in range(len(xs))], label="reactor A") plt.xlabel('time [min]') plt.ylabel('concentration [g/mL]') plt.title('Nutrient A over Time')
dsdt = Lambda - mu*S - beta_e*np.exp(-((m1*I+m2*(Q+H))/n))*(I+P+effi*(H+Q)+pro*E)*S/n + theta*Rdr dedt = 2500000*np.exp(-5000*t**2)*t + beta_e*np.exp(-((m1*I+m2*(Q+H))/n))*(I+P+effi*(H+Q)+pro*E)*S/n - (mu+xi)*E - (k+ sigma)*Ede """inital condition of 250 import cases""" dqdt = k*Ede + k1*Idi2 - (xi + mu)*Q - alpha*Qdq didt = be*sigma*Ede - mu*I - (gamma_e1 + mu_d)*Idi1 - (phi + k1)*Idi2 dpdt = (1 - be)*sigma*Ede - mu*P - (gamma_e2 + mu_d)*Pdp2 - (k1 + phi)*Pdp dhdt = alpha*Qdq + phi*Idi2 + (phi + k1)*Pdp - mu*H - (gamma_e3 + mu_d1)*Hdh drdt = gamma_e1*Idi1 + gamma_e2*Pdp2 + gamma_e3*Hdh + xi*(Q + E) - mu*R - theta*Rdr """balance equations""" return array([dsdt,dedt,dqdt,didt,dpdt,dhdt,drdt]) g = lambda t : array([n,0,0,0,0,0,0]) # initial value nmax = 2000 tt = np.linspace(0,40,nmax) #time yy = ddeint(model,g,tt,fargs=(delaye,delayR,delayi1,delayi2,delayP,delayP2,delayQ,delayH,)) # solving the ODEs yy[np.where(yy<0)] = 0 fig, ax = subplots(1, figsize=(4, 4)) heal, = ax.plot(tt, yy[:,2]+yy[:,5],c='peru', lw=2) #plot the quarantine and hospitalizations syndrom_heal, = ax.plot(tt,yy[:,2]+yy[:,3]+yy[:,4]+yy[:,5],c='r',lw=2) # plot the quarantine, hospitalizations and the rest of illed patients all_, = ax.plot(tt, yy[:,1]+yy[:,2]+yy[:,3]+yy[:,4]+yy[:,5],c='m', lw=2) # all unrecovered patients scatter = plt.scatter(time, data, c='c') # actual data plt.text(0, yy[nmax-1,2]/0.8, r'$R_0=%.2f$''\n' r'$R_c \approx %.2f \rightarrow %.2f$'%(R0,Rc,Rc_prime))
def model(Y, t, tau1, tau2, tau3): S, H, A, D = Y(t) S1, H1, A1, D1 = Y(t - tau1) S2, H2, A2, D2 = Y(t - tau2) S3, H3, A3, D3 = Y(t - tau3) print('------------------------') print(S, H, A, D) print(S1, H1, A1, D1) print(S2, H2, A2, D2) print(S3, H3, A3, D3) print('------------------------') dSdt = -beta * S * (H + A) #succeptibles dHdt = beta * S * (H + A) - k1 * H1 + k2 * (H2 + A2) #HIV infected dAdt = k1 * H1 - delta * A3 #AIDS infected dDdt = delta * A3 #dead return array([dSdt, dHdt, dAdt, dDdt]) g = lambda t: array([S0, H0, A0, D0]) #initial values tt = linspace(11, 40, 96000) fig, ax = subplots(1, figsize=(4, 4)) yy = ddeint(model, g, tt, fargs=(tau1, tau2, tau3)) ax.plot(tt, yy[:, 0], color='r', label='S') ax.plot(tt, yy[:, 1], color='g', label='H') ax.plot(tt, yy[:, 2], color='b', label='A') ax.plot(tt, yy[:, 3], color='purple', label='D') ax.figure.savefig("figure1.jpeg")
def __init__(self): # simulation time and resolution of samples self.dt = 0.1 self.xs = np.linspace(0, 120, 120 / self.dt) self.fluxA = 0.0 self.fluxB = 0.0 self.new_host = Host( c0=10**5, g_max=0.036, # lit: 0.012 yield_coeff=0.000000000001, half_sat=0.00000125, death_rate=0.001, t_dep=self.temperature_dependency_new_host, ) self.original_phage = Phage( c0=10**9, adsorption_rate=0.000000000001, burst_size=100, death_rate=0.00272, ) self.new_phage = Phage( c0=10**6, adsorption_rate=0.0000000001, burst_size=100, death_rate=0.00272, ) self.s0 = 0.0000025 # stock concentration of nutrient (g/mL) #0.0000025 self.R_pnn = 1 / 1000 # fraction of new phage in library self.myinterpolator = scipy.interpolate.interp1d( np.array([0 - 1, 0]), # X np.array([0, 1]).T, # Y kind="linear", bounds_error=False, fill_value=0) self.d = 13.3 # lysis time delay self.dd = 13 # dt *=10 self.q_h_inf_poo = deque([0]) self.q_h_inf_pnn = deque([0]) for i in range(100): self.q_h_inf_poo.append(0) self.q_h_inf_pnn.append(0) data = ddeint(self.model, self.initial_conditions, self.xs, fargs=(self.d, )) plt.figure(figsize=(16, 16)) plt.subplot(3, 3, 1) plt.plot(self.xs, [data[t][0] for t in range(len(self.xs))], label="reactor A") plt.xlabel('time [min]') plt.ylabel('concentration [#bacteria/mL]') plt.title('Host Concentration A over Time') plt.legend() plt.subplot(3, 3, 2) plt.plot(self.xs, [data[t][1] for t in range(len(self.xs))], label="reactor A") plt.xlabel('time [min]') plt.ylabel('concentration [g/mL]') plt.title('Nutrient A over Time') plt.legend() plt.subplot(3, 3, 3) plt.plot(self.xs, [data[t][2] for t in range(len(self.xs))]) plt.xlabel('time [min]') plt.ylabel('concentration [#bacteria/mL]') plt.title('Host Concentration B over Time') plt.subplot(3, 3, 4) plt.plot(self.xs, [data[t][3] for t in range(len(self.xs))]) plt.xlabel('time [min]') plt.ylabel('concentration [g/mL]') plt.title('Nutrient B over Time') plt.subplot(3, 3, 5) plt.plot(self.xs, [data[t][4] for t in range(len(self.xs))]) plt.xlabel('time [min]') plt.ylabel('concentration [#bacteria/mL]') plt.title('Host infected OO over Time') plt.subplot(3, 3, 6) plt.plot(self.xs, [data[t][5] for t in range(len(self.xs))]) plt.xlabel('time [min]') plt.ylabel('concentration [#bacteria/mL]') plt.title('Host infected NN over Time') plt.subplot(3, 3, 8) plt.plot(self.xs, [data[t][6] for t in range(len(self.xs))]) plt.xlabel('time [min]') plt.ylabel('concentration [#phage/mL]') plt.title('Phage OO over Time') plt.subplot(3, 3, 9) plt.plot(self.xs, [data[t][7] for t in range(len(self.xs))]) plt.xlabel('time [min]') plt.ylabel('concentration [#phage/mL]') plt.title('Phage NN over Time') plt.subplot(3, 3, 7) plt.plot(self.xs, [data[t][6] for t in range(len(self.xs))], label="original") plt.plot(self.xs, [data[t][7] for t in range(len(self.xs))], label="new") plt.xlabel('time [min]') plt.ylabel('concentration [#phage/mL]') plt.title('Phage over Time') plt.legend() plt.subplots_adjust(wspace=0.4, hspace=0.4) plt.show()
tau3 = np.pi / 2 - 0.1 #Model def model(Y, t, tau): return -Y(t - tau) #History Function def history(t): return 1 #Simulate the DDE with 3 different delays ts = np.linspace(0, 100, 5000) ys1 = ddeint(model, history, ts, fargs=(tau1, )) ys2 = ddeint(model, history, ts, fargs=(tau2, )) ys3 = ddeint(model, history, ts, fargs=(tau3, )) #Plotting 3 Subplots in 1 major plot fig, ax = plt.subplots(3, 1, constrained_layout=True) #Set x-axis and title plt.xlabel('t') fig.suptitle(r'Solution to $\frac{dy}{dt} = -y(t-\tau)$') #Plot each simulation on the subplots l1 = ax[0].plot(ts, ys1, color='red') l2 = ax[1].plot(ts, ys2, color='orange') l3 = ax[2].plot(ts, ys3) #Set title of each subplot ax[0].set_title(r'$\tau = \frac{\pi}{2} + 0.1$') ax[1].set_title(r'$\tau = \frac{\pi}{2}$')
x = f(t) xd = f(t - d) dxdt = x * (-beta + (1 + beta) * x - xd * x) return dxdt fs = 22 ticksize = 20 legendsize = 15 alpha = 0.8 lw = 3 marksize = 8 g = lambda t: np.array([0.8, 0.8]) t = np.arange(0, 100, 0.01) beta = 0.2 d = 2.25 dyn_all = ddeint(allee_effect, g, t, fargs=(d, beta)) plt.plot(t, dyn_all, alpha=alpha) plt.subplots_adjust(left=0.18, right=0.98, wspace=0.25, hspace=0.25, bottom=0.18, top=0.98) plt.xticks(fontsize=ticksize) plt.yticks(fontsize=ticksize) plt.xlabel('$t$', fontsize=fs) plt.ylabel('$\\langle x \\rangle$', fontsize=fs) plt.show()