def test_adaptive_order(): """ Проверяем сходимость Q: почему наклон линии (число в скобках) соответствует порядку метода? """ t0, t1 = 0, 2 * np.pi y0 = np.array([1., 1.]) ode = Harmonic(y0, 1, 1) methods = ( (ExplicitEulerMethod(), AdaptType.RUNGE), (RungeKuttaMethod(coeffs.rk4_coeffs), AdaptType.RUNGE), (RungeKuttaMethod(coeffs.dopri_coeffs), AdaptType.RUNGE), (EmbeddedRungeKuttaMethod(coeffs.dopri_coeffs), AdaptType.EMBEDDED), ) tols = 10.**-np.arange(3, 9) plt.figure(figsize=(9, 6)) for i, (method, adapt_type) in enumerate(methods): print(method.name) fcs = [] errs = [] for tol in tols: ode.clear_call_counter() ts, ys = adaptive_step_integration(method=method, ode=ode, y_start=y0, t_span=(t0, t1), adapt_type=adapt_type, atol=tol, rtol=tol * 1e3) err = np.linalg.norm(ys[-1] - ode[t1]) fc = ode.get_call_counter() print( f'{method.name} with {adapt_type.name}: {fc} RHS calls, err = {err:.5f}' ) errs.append(err) fcs.append(fc) x = np.log10(fcs) y = -np.log10(errs) k, b = np.polyfit(x, y, 1) plt.plot(x, k * x + b, 'k:') plt.plot(x, y, 'p', label=f'{method.name} {adapt_type} ({k:.2f})') plt.title('test_adaptive_order: check RHS evals') plt.xlabel('log10(function_calls)') plt.ylabel('accuracy') plt.legend() plt.show()
def test_adaptive_order(): """ test adaptive algorithms convergence """ t0, t1 = 0, 2 * np.pi y0 = np.array([1., 1.]) f = Harmonic(y0, 1, 1) methods = ( (ExplicitEulerMethod(), AdaptType.RUNGE), (RungeKuttaMethod(coeffs=collection.rk4_coeffs), AdaptType.RUNGE), (EmbeddedRungeKuttaMethod(coeffs=collection.dopri_coeffs), AdaptType.EMBEDDED), ) tols = 10.**-np.arange(3, 9) plt.figure() for i, (method, adapt_type) in enumerate(methods): print(method.name) fcs = [] errs = [] for tol in tols: f.clear_call_counter() ts, ys = adaptive_step_integration(method=method, func=f, y_start=y0, t_span=(t0, t1), adapt_type=adapt_type, atol=tol, rtol=tol * 1e3) err = np.linalg.norm(ys[-1] - f[t1]) fc = f.get_call_counter() print(f'{method.name}: {fc} RHS calls, err = {err:.5f}') errs.append(err) fcs.append(fc) x = np.log10(fcs) y = -np.log10(errs) k, b = np.polyfit(x, y, 1) plt.plot(x, k * x + b, 'k:') plt.plot(x, y, 'p', label=f'{method.name} ({k:.2f})') plt.suptitle('test_adaptive_order: check RHS evals') plt.xlabel('log10(function_calls)') plt.ylabel('accuracy') plt.legend() plt.show()
def test_multi_step(): """ Проверяем методы Адамса Q: сравните правые графики для обоих случаев и объясните разницу """ y0 = np.array([0., 1.]) t0 = 0 t1 = np.pi dt = 0.1 f = Harmonic(y0, 1, 1) ts = np.arange(t0, t1 + dt, dt) exact = f[ts].T for one_step_method in [ RungeKuttaMethod(collection.rk4_coeffs), ExplicitEulerMethod(), ]: fig, (ax1, ax2) = plt.subplots(1, 2) ax1.plot(ts, [e[0] for e in exact], 'k', label='Exact') for p, c in adams_coeffs.items(): f.clear_call_counter() t_adams, y_adams = adams(f, y0, ts, c, one_step_method=one_step_method) n_calls = f.get_call_counter() print( f'{p}-order multi-step with one-step {one_step_method.name}: {n_calls} function calls' ) err = get_accuracy(exact, y_adams) label = f"Adams's order {p}" ax1.plot(t_adams, [y[0] for y in y_adams], '.--', label=label) ax2.plot(t_adams, err, '.--', label=label) ax1.legend(), ax1.set_title('y(t)') ax2.legend(), ax2.set_title('accuracy') fig.suptitle( f'test_multi_step\none step method: {one_step_method.name}') fig.tight_layout() plt.show()
def test_one_step(): """ Проверяем методы Эйлера и Рунге-Кутты """ y0 = np.array([0., 1.]) t0 = 0 t1 = np.pi ode = Harmonic(y0, 1, 1) for dt in [0.1, 0.01]: ts = np.arange(t0, t1 + dt, dt) exact = ode[ts].T fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4)) ax1.plot(ts, [e[0] for e in exact], 'k', label='Exact') colors = 'rgbcmyk' for i, method in enumerate([ ExplicitEulerMethod(), ImplicitEulerMethod(), RungeKuttaMethod(collection.rk4_coeffs), RungeKuttaMethod(collection.dopri_coeffs), ]): ode.clear_call_counter() _, y = fix_step_integration(method, ode, y0, ts) n_calls = ode.get_call_counter() print( f'One-step {method.name}: {len(y)-1} steps, {n_calls} function calls' ) ax1.plot(ts, [_y[0] for _y in y], f'{colors[i]}.--', label=method.name) ax2.plot(ts, get_accuracy(exact, y), f'{colors[i]}.--', label=method.name) ax1.legend(), ax1.set_title('y(t)') ax2.legend(), ax2.set_title('accuracy') fig.suptitle(f'test_one_step, dt={dt}') fig.tight_layout() plt.show()
def test_multi_step(): """ test Adams method Q: compare the right plot for both cases and explain the difference """ y0 = np.array([0., 1.]) t0 = 0 t1 = 1. dt = 0.1 f = Harmonic(y0, 1, 1) ts = np.arange(t0, t1 + dt, dt) exact = f[ts].T for one_step_method in [ RungeKuttaMethod(collection.rk4_coeffs), ExplicitEulerMethod(), ]: _, (ax1, ax2) = plt.subplots(1, 2) ax1.plot(ts, [e[0] for e in exact], 'k', label='Exact') for p, c in adams_coeffs.items(): f.clear_call_counter() t_adams, y_adams = adams(f, y0, ts, c, one_step_method=one_step_method) n_calls = f.get_call_counter() print( f'{p}-order multi-step with one-step {one_step_method.name}: {n_calls} function calls' ) err = get_accuracy(exact, y_adams) label = f"Adams's order {p}" ax1.plot(t_adams, [y[0] for y in y_adams], '.--', label=label) ax2.plot(t_adams, err, '.--', label=label) ax1.set_xlabel('t'), ax1.set_ylabel('y'), ax1.legend() ax2.set_xlabel('t'), ax2.set_ylabel('accuracy'), ax2.legend() plt.suptitle( f'test_multi_step\none step method: {one_step_method.name}') plt.show()
def test_one_step(): """ test Euler and RK methods """ y0 = np.array([0., 1.]) t0 = 0 t1 = np.pi / 2 dt = 0.1 f = Harmonic(y0, 1, 1) ts = np.arange(t0, t1 + dt, dt) exact = f[ts].T _, (ax1, ax2) = plt.subplots(1, 2) ax1.plot(ts, [e[0] for e in exact], 'k', label='Exact') colors = 'rgbcmyk' for i, method in enumerate([ ExplicitEulerMethod(), ImplicitEulerMethod(), RungeKuttaMethod(collection.rk4_coeffs), RungeKuttaMethod(collection.dopri_coeffs), ]): f.clear_call_counter() _, y = fix_step_integration(method, f, y0, ts) n_calls = f.get_call_counter() print( f'One-step {method.name}: {len(y)-1} steps, {n_calls} function calls' ) ax1.plot(ts, [_y[0] for _y in y], f'{colors[i]}.--', label=method.name) ax2.plot(ts, get_accuracy(exact, y), f'{colors[i]}.--', label=method.name) ax1.set_xlabel('t'), ax1.set_ylabel('y'), ax1.legend() ax2.set_xlabel('t'), ax2.set_ylabel('accuracy'), ax2.legend() plt.suptitle('test_one_step') plt.show()