def go(): xlf, vlf = 1.5, 0.0 xe, ve = 1.5, 0.0 xrk2, vrk2 = 1.5, 0.0 t, h = 0.0, 0.1 tt = [] xlf_array, vlf_array = [], [] xe_array, ve_array = [], [] xrk2_array, vrk2_array = [], [] energy_lf, energy_e, energy_rk2 = [], [], [] while t < 4 * np.pi: xlf_array.append(xlf) vlf_array.append(vlf) energy_lf.append(0.5 * m * vlf**2 + 0.5 * m * w**2 * xlf**2) xlf, vlf = ode.leapfrog(oscillator, xlf, vlf, t, h) xe_array.append(xe) ve_array.append(ve) energy_e.append(0.5 * m * ve**2 + 0.5 * m * w**2 * xe**2) xe, ve = ode.Euler(oscillator_rk, [xe, ve], t, h) xrk2_array.append(xrk2) vrk2_array.append(vrk2) energy_rk2.append(0.5 * m * vrk2**2 + 0.5 * m * w**2 * xrk2**2) xrk2, vrk2 = ode.rk2(oscillator_rk, [xrk2, vrk2], t, h) tt.append(t) t += h plt.figure(1) plt.plot(xlf_array, vlf_array, '--', label='Leapfrog') plt.plot(xe_array, ve_array, label='Euler') plt.plot(xrk2_array, vrk2_array, '.', label='RK2') plt.xlabel('x (m)') plt.ylabel('v (m/s)') plt.legend(loc='best') plt.show() plt.figure(2) plt.semilogy(tt, energy_lf, '--', label='Leapfrog') plt.semilogy(tt, energy_e, label='Euler') plt.semilogy(tt, energy_rk2, '.', label='RK2') plt.xlabel('Time (s)') plt.ylabel('Total Energy (J)') plt.legend(loc='best') plt.grid() plt.show()
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)
import sys, os sys.path.append(os.path.join(os.path.dirname(os.getcwd()), 'copads')) import ode initial_nuclei = 10000.0 decay_constant = 0.2 def decay(t, y): return -decay_constant * y[0] print('Solving using Euler method......') for i in [x for x in ode.Euler([decay], 0.0, [initial_nuclei], 0.1, 50.0)]: print(','.join([str(x) for x in i])) print('') print('Solving using RK4 method......') for i in [x for x in ode.RK4([decay], 0.0, [initial_nuclei], 0.1, 50.0)]: print(','.join([str(x) for x in i])) print('') print('Solving using RK3 method......') for i in [x for x in ode.RK3([decay], 0.0, [initial_nuclei], 0.1, 50.0)]: print(','.join([str(x) for x in i])) print('') print("Solving using Heun's method......") for i in [x for x in ode.Heun([decay], 0.0, [initial_nuclei], 0.1, 50.0)]: print(','.join([str(x) for x in i])) print('')
def exact_sol(t): return 1000 * np.exp(-lamb * t) t = 0 h = 0.5 y_euler, y_rk2, y_rk4, y_rk45n = 1000, 1000, 1000, 1000 tt, exsol = [], [] nuclear_euler, nuclear_rk2, nuclear_rk4, nuclear_rk45n = [], [], [], [] abserr_euler, abserr_rk2, abserr_rk4, abserr_rk45n = [], [], [], [] clf_euler, clf_rk2, clf_rk4, clf_rk45n = [], [], [], [] while t <= 8: yy_euler = ode.Euler(decay, [y_euler], t, h) yy_rk2 = ode.rk2(decay, [y_rk2], t, h) yy_rk4 = ode.rk4(decay, [y_rk4], t, h) yy_rk45n = ode.RK45n(decay, [y_rk45n], t, h) exs = exact_sol(t) tt.append(t) exsol.append(exs) nuclear_euler.append(yy_euler[0]) nuclear_rk2.append(yy_rk2[0]) nuclear_rk4.append(yy_rk4[0]) nuclear_rk45n.append(yy_rk45n[0]) abserr_euler.append(abs(yy_euler[0] - exs)) abserr_rk2.append(abs(yy_rk2[0] - exs))