def simulite(k, fy): t_euler, x_euler = ode.euler( dfun=fy, xzero=k, timerange=t_range, timestep=t_step, ) return x_euler
def main(): t_0 = 0 y_0 = 10.0 # m v_0 = 0.0 # m/s sim_d = [np.array([t_0, v_0, y_0])] #t,v,x sim_d_energy = [fall_energy(y_0, v_0)] sim_a = [np.array([t_0, fall_analytic_velocity(t_0), fall_analytic(t_0, y_0)])] # t,v,x sim_a_energy = [fall_energy(fall_analytic(t_0, y_0), fall_analytic_velocity(t_0))] #print sim_d #print sim_d[0] #print sim_d[0][1:3] # Run the simuation for about two seconds. dt = 0.05 # step size t_max = 1.5 # maximum number of seconds to run. dt_steps = int(t_max / dt) #number of dt increments to perform y_p = 10 for step in range(1, dt_steps + 1): t = step * dt + t_0 tp1 = np.array([t]) sim_d.append(np.append(tp1, ode.euler(fall, sim_d[step - 1][1:3], dt, t))) sim_d_energy.append(fall_energy(sim_d[step][2], sim_d[step][1])) sim_a.append(np.append(tp1, [fall_analytic_velocity(t), fall_analytic(t, y_0)])) sim_a_energy.append(fall_energy(sim_a[step][2], sim_a[step][1])) #Plot the falling objects. Euler vs Analytical functions. sim_d = np.array(sim_d) sim_a = np.array(sim_a) y, = plt.plot(sim_d[:,0], sim_d[:,2], "r") y_anal, = plt.plot(sim_a[:,0], sim_a[:,2], "b") plt.legend([y, y_anal], ["Height", "Analytical Height"]) plt.title("Position Comparison of the Analytical and Euler Systems") plt.xlabel("Time (sec)") plt.ylabel("Height (m)") plt.show() #Plot the energies of the two curves. e_plot_d, = plt.plot(sim_d[:,0], sim_d_energy, "r") e_plot_a, = plt.plot(sim_a[:,0], sim_a_energy, "b") plt.legend([e_plot_d, e_plot_a], ["Energy in discrete simulation", "Energy in analytical simulation"]) plt.title("Energy Comparison of the Analytical and Euler Systems") plt.xlabel("Time (sec)") plt.ylabel("Energy") plt.ylim([97, 102]) plt.show() #Calculate the energy difference e_diff = abs(sim_a_energy[len(sim_a_energy) - 1] - sim_d_energy[len(sim_d_energy) - 1]) print "Energy difference is %0.2fJ" % e_diff return 0
def test_Euler(): t_test = [round(x, 6) for x in oscillator_euler_t] p_test = [round(x, 6) for x in oscillator_euler_x1] v_test = [round(x, 6) for x in oscillator_euler_x2] t_euler_raw, x_euler_raw = ode.euler(oscillator_1st_deriv, xzero=[0, 1], timerange=[0, 5], timestep=0.1) p_euler_raw, v_euler_raw = x_euler_raw t_euler = [round(x, 6) for x in t_euler_raw] p_euler = [round(x, 6) for x in p_euler_raw] v_euler = [round(x, 6) for x in v_euler_raw] assert (t_test, p_test, v_test) == (t_euler, p_euler, v_euler)
def run_example_composite_simpson(): print("\n\n[Example] ODE: Euler") def f(x, y): return y - x ** 2 + 1 a = 0.0 b = 2.0 n = 10 ya = 0.5 print_var("a", a) print_var("b", b) print_var("n", n) print_var("ya", ya) [vx, vy] = ode.euler(f, a, b, n, ya) print_var("vx", vx) print_var("vy", vy)
def test_euler(): t_euler_raw, x_euler_raw = ode.euler(oscillator_1st_deriv, xzero=[0, 1], timerange=[0, 5], timestep=0.1) p_euler_raw, v_euler_raw = x_euler_raw t_euler = [round(x, 6) for x in t_euler_raw] p_euler = [round(x, 6) for x in p_euler_raw] v_euler = [round(x, 6) for x in v_euler_raw] t_ieuler_raw, x_ieuler_raw = zip(*list( ode.Euler( oscillator_1st_deriv, xzero=[0, 1], timerange=[0, 5 ], timestep=0.1))) p_ieuler_raw, v_ieuler_raw = zip(*x_ieuler_raw) t_ieuler = [round(x, 6) for x in t_ieuler_raw] p_ieuler = [round(x, 6) for x in p_ieuler_raw] v_ieuler = [round(x, 6) for x in v_ieuler_raw] assert (t_euler, p_euler, v_euler) == (t_ieuler, p_ieuler, v_ieuler)
def main(): tn = np.linspace(0.0, 2.0, 5) # Grid y0 = np.array([0.5]) # Initial condition y_ef = ode.euler(fun, tn, y0) # Forward Euler y_mp = ode.midpoint(fun, tn, y0) # Explicit Midpoint y_rk = ode.rk4(fun, tn, y0) # Runge-Kutta 4 y_an = tn**2 + 2.0 * tn + 1.0 - 0.5 * np.exp(tn) # Analytical plt.figure(1) plt.plot(tn, y_ef, 'ro-', label='Forward Euler (1st)') plt.plot(tn, y_mp, 'go-', label='Explicit Mid-Point (2nd)') plt.plot(tn, y_rk, 'bx-', label='Runge-Kutta (4th)') plt.plot(tn, y_an, 'k-', label='Analytical Solution') plt.xlabel('t') plt.ylabel('y') plt.legend(loc=2) plt.savefig('ex01_ode_solution.png') plt.show()
def run_example_composite_simpson(): """Run an example 'ODE: Euler'.""" print_func_docstring() def f(x, y): return y - x**2 + 1 a = 0.0 b = 2.0 n = 10 ya = 0.5 print_var("a", a) print_var("b", b) print_var("n", n) print_var("ya", ya) [vx, vy] = ode.euler(f, a, b, n, ya) print_var("vx", vx) print_var("vy", vy)
def main(): xn = np.linspace(0.0, 5.0, 20) # Grid y0 = np.array([0.5]) # Initial condition y_ef = ode.euler(fun, xn, y0) # Forward Euler y_mp = ode.midpoint(fun, xn, y0) # Explicit Midpoint y_rk = ode.rk4(fun, xn, y0) # Runge-Kutta 4 y_an = xn**2 + 2.0 * xn + 1.0 - 0.5 * np.exp(xn) # Analytical for i in range(0, xn.size): print xn[i], y_an[i], y_ef[i, 0], y_mp[i, 0], y_rk[i, 0] plt.figure(1) plt.plot(xn, y_ef, 'ro-', label='Forward Euler (1st)') plt.plot(xn, y_mp, 'go-', label='Explicit Mid-Point (2nd)') plt.plot(xn, y_rk, 'bx-', label='Runge-Kutta (4th)') plt.plot(xn, y_an, 'k-', label='Analytical Solution') plt.xlabel('x') plt.ylabel('y') plt.legend(loc=3) plt.savefig('ex02_ode_solution.png') plt.show()
def main(): # Grid time = np.linspace(0.0, 1.1e-6, 100) # Initial conditions X0 = np.array((0.0, 0.0, 0.0, 0.0, 1.0e6, 0.0)) # Solve ODE X_ef = ode.euler(fun, time, X0) # Forward Euler X_mp = ode.midpoint(fun, time, X0) # Explicit Midpoint X_rk = ode.rk4(fun, time, X0) # Runge-Kutta 4 # for i in range(0,xn.size): # print xn[i], y_an[i], y_ef[i,0], y_mp[i,0], y_rk[i,0] plt.figure(1) plt.plot(X_ef[:, 0], X_ef[:, 1], 'ro-', label='Forward Euler (1st)') plt.plot(X_mp[:, 0], X_mp[:, 1], 'go-', label='Explicit Mid-Point (2nd)') plt.plot(X_rk[:, 0], X_rk[:, 1], 'bx-', label='Runge-Kutta (4th)') plt.xlabel('x [m]') plt.ylabel('y [m]') plt.axis('equal') plt.legend(loc=3) plt.savefig('ex01_particle_ExB.png') plt.show()
analytic solution of x'(t) = x(2-x) dt, x(0) = 1 is x(t) = 2*exp(2t) / (1 + exp(2t)) """ func = lambda x: 2 * x - x**2 init = 1 t_start = 0 step = 0.01 repeat = 200 seed = 198 ts = np.arange(t_start, step * repeat, step) analytic_path = 2 * np.exp(2 * ts) / (1 + np.exp(2 * ts)) approx_path = ode.euler(func, init, t_start, step, repeat) sample_size = 100 # Euler Method rs = np.random.RandomState(seed) random_coef = lambda x: x random_process = lambda x, step: rs.normal(loc=0, scale=math.sqrt(step)) euler_path = np.zeros((sample_size, repeat)) for i in range(sample_size): euler_path[i] = sde_euler(func, init, t_start, step, repeat, random_coef, random_process)[1] euler_average = np.zeros(repeat) for i in range(repeat):
analytic solution of x'(t) = x(2-x) dt, x(0) = 1 is x(t) = 2*exp(2t) / (1 + exp(2t)) """ func = lambda x: 2*x - x**2 init = 1 t_start = 0 step = 0.01 repeat = 200 seed = 198 ts = np.arange(t_start, step*repeat, step) analytic_path = 2*np.exp(2*ts) / (1 + np.exp(2*ts)) approx_path = ode.euler(func, init, t_start, step, repeat) sample_size = 100 # Euler Method rs = np.random.RandomState(seed) random_coef = lambda x: x random_process = lambda x, step: rs.normal(loc=0, scale=math.sqrt(step)) euler_path = np.zeros((sample_size, repeat)) for i in range(sample_size): euler_path[i] = sde_euler(func, init, t_start, step, repeat, random_coef, random_process)[1] euler_average = np.zeros(repeat) for i in range(repeat): euler_average[i] = euler_path[:, i].mean()
def main(): # Initial velocity [m/s] vy0 = 1.0e6 # Larmor pulsation [rad/s] w_L = qe / me * B0 # Larmor period [s] tau_L = 2.0 * np.pi / w_L # Larmor radius [m] r_L = vy0 / w_L # Initial conditions y0 = np.array((r_L, 0.0, 0.0, 0.0, vy0, 0.0)) # Euler Forward time_ef = np.linspace(0.0, tau_L, 10) y_ef = ode.euler(fun, time_ef, y0) # Explicit Midpoint time_mp = np.linspace(0.0, tau_L, 10) y_mp = ode.midpoint(fun, time_mp, y0) # Runge-Kutta 4 time_rk = np.linspace(0.0, tau_L, 10) y_rk = ode.rk4(fun, time_rk, y0) # Amplitude (orbit radius) r_ef = np.sqrt(y_ef[:, 0]**2 + y_ef[:, 1]**2) r_mp = np.sqrt(y_mp[:, 0]**2 + y_mp[:, 1]**2) r_rk = np.sqrt(y_rk[:, 0]**2 + y_rk[:, 1]**2) # Plot 1 - Trajectory plt.figure(1) plt.plot(y_ef[:, 0], y_ef[:, 1], 'ro-', label='Euler-Forward (1st)') plt.plot(y_mp[:, 0], y_mp[:, 1], 'go-', label='Explicit Mid-Point (2nd)') plt.plot(y_rk[:, 0], y_rk[:, 1], 'bx-', label='Runge-Kutta (4th)') plt.axis('equal') plt.xlabel('x [m]') plt.ylabel('y [m]') plt.title('One Larmor Gyration') plt.legend(loc=3) plt.savefig('ex03_ode_larmor_trajectory.png') # Plot 2 - Amplitude percent error plt.figure(2) plt.plot(time_ef / tau_L, ode.error_percent(r_L, r_ef), 'ro', label='Forward Euler (1st)') plt.plot(time_mp / tau_L, ode.error_percent(r_L, r_mp), 'go', label='MidPoint (2nd)') plt.plot(time_rk / tau_L, ode.error_percent(r_L, r_rk), 'bx', label='Runge-Kutta (4th)') plt.xlabel('time / tau_Larmor') plt.ylabel('Percent Amplitude error [%]') plt.title('Percent Amplitude Error over 1 Larmor gyration ') plt.legend(loc=2) plt.savefig('ex03_ode_larmor_error.png') plt.show()
debug("b") debug("n") [xi] = integration.composite_simpson(f, b, a, n) debug("xi") print_running("ODE: Euler method") f = lambda x, y: y - x**2 + 1 a = 0.0 b = 2.0 n = 10 ya = 0.5 debug("a") debug("b") debug("n") debug("ya") [vx, vy] = ode.euler(f, a, b, n, ya) debug("vx") debug("vy") print_running("ODE: Taylor (Order Two) method") f = lambda x, y: y - x**2 + 1 df1 = lambda x, y: y - x**2 + 1 - 2 * x a = 0.0 b = 2.0 n = 10 ya = 0.5 debug("a") debug("b") debug("n") debug("ya") [vx, vy] = ode.taylor2(f, df1, a, b, n, ya)
return dX solve = [[0, 1, 0], [1, 1, 0], [0, 1, 1], [1, 1, 1], [1, j_0 * v / (j_0 * w + c + e - h), 1], [0, -i_0 * v / (-i_0 * w + h), 0], [0, -j_0 * v / (-j_0 * w + h), 1]] solve_modify = [[0, 1, 0], [1, 1, 0], [0, w * v * i_0 / h, 0], [0, 1, 1], [0, w * v * j_0 / h, 1], [1, 1, 1]] dt = 0.1 t_range = [0, 3] t_step = 0.001 X_0 = [0.96, 1, 1] t_euler, x_euler = ode.euler( dfun=fy, xzero=X_0, timerange=t_range, timestep=t_step, ) X = np.array(x_euler) T = np.array(t_euler) variable = [ 'The ratio of government regulators to monitoring', 'The ratio of total contracting to safe investmentratio state safety of supervise', 'ratio state safety of supervise' ] def simulite(k, fy): t_euler, x_euler = ode.euler( dfun=fy,