def test(b): """ Runs test on SIR model, with variying beta """ beta = b #0.0005 or 0.0001 # Infection rate v = 0.1 # Prob. of recovery per dt S0 = 1500 # Init. No. of suspectibles I0 = 1 # Init. No. of infected R0 = 0. # Init. No. of resistant U0 = [S0, I0, R0] # Initial conditions T = 60 # Duration, days dt = 0.5 # Time step length in days n = T / dt # No. of solve steps f = RHS(v, dt, T, beta) # Get right hand side of equation solver = ForwardEuler(f) # Select ODE solver method solver.set_initial_condition(U0) time_points = np.linspace(0, 60, n + 1) u, t = solver.solve(time_points) S = u[:, 0] # S is all data in array no 0 I = u[:, 1] # I is all data in array no 1 R = u[:, 2] # R is all data in array no 2 plot(t, S, t, I, t, R, xlabel='Days', ylabel='Persons', legend=['Suspectibles', 'Infected', 'Resistant'], hold=('on'))
def test(b): """ Runs test on SIR model, with variying beta """ beta = b #0.0005 or 0.0001 # Infection rate v = 0.1 # Prob. of recovery per dt S0 = 1500 # Init. No. of suspectibles I0 = 1 # Init. No. of infected R0 = 0. # Init. No. of resistant U0 = [S0, I0, R0] # Initial conditions T = 60 # Duration, days dt = 0.5 # Time step length in days n = T/dt # No. of solve steps f = RHS(v, dt, T, beta) # Get right hand side of equation solver = ForwardEuler(f) # Select ODE solver method solver.set_initial_condition(U0) time_points = np.linspace(0, 60, n+1) u, t = solver.solve(time_points) S = u[:,0] # S is all data in array no 0 I = u[:,1] # I is all data in array no 1 R = u[:,2] # R is all data in array no 2 plot(t, S, t, I, t, R, xlabel='Days', ylabel='Persons', legend=['Suspectibles', 'Infected', 'Resistant'], hold=('on'))
def test_Cooling(): t1 = 15; Ts = 20; T0 = 95; T1 = 92 h = estimate_h(t1, Ts, T0, T1) cool = Cooling(h, 20) dTdt = ForwardEuler(cool) t0 = (0, 15) #time points U0 = 95 dTdt.set_initial_condition(U0) u, t = dTdt.solve(t0) tol = 1e-10 expected = 92 computed = u[1] assert np.abs(expected - computed) < tol
def term_velocity_skydiver(): fb = TermFluidBody(0.79, 1003.0, 0.9, 0.08, 0.6) termvel = fb.term_velocity() nvals = range(10, 10000, 100) numerical_termvels = [] for N in nvals: fe = ForwardEuler(fb) fe.set_initial_condition(0.0) tvals = numpy.linspace(0.0, 30.0, N) numerical_termvel = fe.solve(tvals, terminate=getEPSCheck(N)) numerical_termvel = numerical_termvel[0][-1] numerical_termvels.append(numerical_termvel) print 'skydiver terminal velocity exact %.7f numerical %.7f', termvel, numerical_termvel plot(nvals, [termvel for x in nvals]) plot(nvals, numerical_termvels) show()
def term_velocity_ball(): r = 0.11 m = 0.43 V = 4.0/3.0*pi*r**3 sigma_b = m / V fb = TermFluidBody(sigma=1000.0, sigma_b=sigma_b, A=pi*r**2, V=V, C_D=0.2) termvel = fb.term_velocity() nvals = range(10, 10000, 100) numerical_termvels = [] for N in nvals: fe = ForwardEuler(fb) fe.set_initial_condition(0.0) tvals = numpy.linspace(0.0, 30.0, N) numerical_termvel = fe.solve(tvals, terminate=getEPSCheck(N)) numerical_termvel = numerical_termvel[0][-1] numerical_termvels.append(numerical_termvel) print 'ball terminal velocity exact %.7f numerical %.7f', termvel, numerical_termvel plot(nvals, [termvel for x in nvals]) plot(nvals, numerical_termvels) show()
def analytic(t): anal = np.zeros(len(t)) for i in range(len(t)): anal[i] = 0.2 * np.exp(0.1 * t[i]) return anal #time time = np.linspace(0, 20, 200) time2 = np.linspace(0, 20, 8) n = 200 OwO = ForwardEuler(f) OwO.set_initial_condition( u0) #OwO ideen skammlost stjaalet av min venn Morten Berg u, t = OwO.solve(time) u2, t2 = OwO.solve(time2) a = analytic(t) plt.plot(t, a, label="analytic") plt.plot(t, u, label=f"n = {200}", ls="--") plt.plot(t2, u2, label=f"n = {8}") plt.ylabel("u") plt.xlabel("time") plt.legend() plt.show()
S, I, _ = u return np.asarray([ -self.beta(t) * ((S * I) / (self.initial_conditions[0] + self.initial_conditions[1] + self.initial_conditions[2])), self.beta(t) * ((S * I) / (self.initial_conditions[0] + self.initial_conditions[1] + self.initial_conditions[2])) - self.nu(t) * I, self.nu(t) * I ]) if __name__ == '__main__': sir = SIR(0.14, 0.94, 15000, 400, 350) solver = ForwardEuler(sir) solver.set_initial_conditions(sir.initial_conditions) time_steps = np.linspace(0, 60, 10001) u, t = solver.solve(time_steps) plt.plot(t, u[:, 0], label='susceptible') plt.plot(t, u[:, 1], label='infected') plt.plot(t, u[:, 2], label='recovered') plt.legend() plt.show()
def f(u, t): x, vx, y, vy = u g = 9.81 return [vx, 0, vy, -g] # Initial condition, start at the origin: x = 0 y = 0 # velocity magnitude and angle: v0 = 5 theta = 80 * np.pi / 180 vx = v0 * np.cos(theta) vy = v0 * np.sin(theta) U0 = [x, vx, y, vy] solver = ForwardEuler(f) solver.set_initial_condition(U0) time_points = np.linspace(0, 1.0, 101) u, t = solver.solve(time_points) # u is an array of [x,vx,y,vy] arrays, plot y vs x: x = u[:, 0] y = u[:, 2] plt.plot(x, y) plt.title('The trajectory of the ball') plt.xlabel('x') plt.ylabel('y') plt.show()
self.a = a def __call__(self, u, t): a = self.a return -a * u #b) a = log(2) / 5600.0 U0 = 1.0 aprox = Decay(a) number_of_points = 20000 / 500 + 1 t_points = np.linspace(0, 20000, number_of_points) solver = ForwardEuler(aprox) solver.set_initial_condition(U0) u, t = solver.solve(t_points) #c) exact_value = np.zeros(len(t_points)) for i in range(number_of_points): exact_value[i] = exp(-a * t_points[i]) plt.plot(t, exact_value, "b", label="Exact decay") plt.plot(t, u, "r", label="Forward Euler aproximation") plt.plot(t, u, "ro") plt.ylabel("fraction of particles that remain") plt.xlabel("time [years]") plt.legend(loc="best") plt.show()
def analytic(t): anal = np.zeros(len(t)) for i in range(len(t)): anal[i] = t[i] * np.cos(t[i]) return anal if __name__ == "__main__": y0 = -5 * np.cos(-5) tp = np.linspace(-5,5,20) FE = ForwardEuler(f) FE.set_initial_condition(y0) yFE, tFE = FE.solve(tp) ME = midpoint_euler(f) ME.set_initial_condition(y0) y, t = ME.solve(tp) a = analytic(tp) plt.plot(t, a, label = "analytic") plt.plot(t, y, label = "Euler midpoint", ls = "--") plt.plot(tFE, yFE, label = "Forward Euler", ls = "-.") plt.xlabel("time") plt.ylabel("Euler Midpoint Method") plt.legend()
new_u[0] = self.dV(self.V_s(t), u[0]) for i in range(len(u) - 1): new_u[i + 1] += self.dV(u[i], u[i + 1]) return new_u if __name__ == "__main__": import sys sys.path.append("../") from ODESolver import ForwardEuler axon = Axon(N=100) solver = ForwardEuler(axon) solver.set_initial_conditions(axon.initial_conditions) dt = 1e-6 T = 1e-2 num_time_steps = int(T/dt) time_steps = np.linspace(0, T, num_time_steps) u, t = solver.solve(time_steps) from matplotlib import pyplot as plt from matplotlib import cm, colors new_cmap = colors.LinearSegmentedColormap.from_list("", cm.get_cmap("Greens")(np.linspace(0.4, 1))) for i in range(0, num_time_steps, 1000): plt.plot(
rho = 1 delta_I = lambda t: 0 if t < 4 else (0.014 if t > 4 and t < 28 else 0.05) delta_S = lambda t: 0 if t < 28 else 0.007 # beta = 0.012 # alpha = 0.0016 # sigma = 2 # rho = 1 # delta_I = 0.014 # delta_S = 0.0 S0 = 60 I0 = 0 Z0 = 1 R0 = 0 zombie_model = SIZR(sigma, beta, rho, delta_S, delta_I, alpha, S0, I0, Z0, R0) solver = ForwardEuler(zombie_model) solver.set_initial_conditions(zombie_model.initial_conditions) time_steps = np.linspace(0, 33, 1001) u, t = solver.solve(time_steps) plt.plot(t, u[:, 0], label="Susceptible humans") plt.plot(t, u[:, 1], label="Infected humans") plt.plot(t, u[:, 2], label="Zombies") plt.plot(t, u[:, 3], label="Dead") plt.legend() plt.show()
Q0 = 0 # Initial electric charge, in Colomb R = 1e8 # Resistance in Ohm C = 2e-8 # Capasitance in Farad timepoints = 101 seconds = 10 time_array = linspace(0, seconds, timepoints) V0 = 8 # Voltage of battery when on def Q_der(Q,t): # Definition of Q'(t) return (V0 - Q/C)/R # __Exercise a__ def Q_exact(t): # Q(t) exact (only for charge-up). return (Q0 - C*V0)*exp(-t/(R*C)) + C*V0 circuit_FE = ForwardEuler(Q_der) circuit_FE.set_initial_condition(Q0) Q_FE, t = circuit_FE.solve(time_array) circuit_RK4 = RungeKutta4(Q_der) circuit_RK4.set_initial_condition(Q0) Q_RK4, t = circuit_RK4.solve(time_array) plt.plot(t, Q_FE, label='$Q(t)$, Forward Euler') plt.plot(t, Q_RK4, label='$Q(t)$, Runge Kutta 4') many_time_steps = linspace(0, seconds, 1001) # For analytical solution. plt.plot(many_time_steps, Q_exact(many_time_steps), label='$Q(t)$, exact') plt.xlabel('Time [seconds]') plt.ylabel('Electric charge [colomb]') plt.title('Charging capasitor') plt.legend()