def test_stiff(): """ Проверяем явные и неявные методы на жёсткой задаче https://en.wikipedia.org/wiki/Van_der_Pol_oscillator Q: почему даже метод Розенброка иногда уменьшает шаг почти до нуля? """ t0 = 0 t1 = 800 * np.pi mu = 1000 y0 = np.array([2., 0.]) f = VanDerPol(y0, mu) fig1, ax1 = plt.subplots() fig2, (ax21, ax22) = plt.subplots(1, 2) fig3, ax3 = plt.subplots() colors = 'rgbcmyk' for i, (method, adapt_type) in enumerate([ (ExplicitEulerMethod(), AdaptType.RUNGE), (ImplicitEulerMethod(), AdaptType.RUNGE), (EmbeddedRosenbrockMethod(coeffs.rosenbrock23_coeffs), AdaptType.EMBEDDED), ]): f.clear_call_counter() ts, ys = adaptive_step_integration(method, f, y0, (t0, t1), adapt_type=adapt_type, atol=1e-6, rtol=1e-3) print( f'{method.name}: {len(ts)-1} steps, {f.get_call_counter()} RHS calls' ) ax1.plot([y[0] for y in ys], [y[1] for y in ys], f'{colors[i]}.--', label=method.name) ax21.plot(ts, [y[0] for y in ys], f'{colors[i]}.--', label=method.name) ax22.plot(ts, [y[1] for y in ys], f'{colors[i]}.--', label=method.name) ax3.plot(ts[:-1], np.array(ts[1:]) - np.array(ts[:-1]), f'{colors[i]}.--', label=method.name) ax1.set_xlabel('x'), ax1.set_ylabel('y'), ax1.legend() fig1.suptitle(f'test_stiff: Van der Pol, mu={mu:.2f}, y(x)') ax21.set_xlabel('t'), ax21.set_ylabel('x'), ax21.legend() ax22.set_xlabel('t'), ax22.set_ylabel('y'), ax22.legend() fig2.suptitle(f'test_stiff: Van der Pol, mu={mu:.2f}, x(t)') ax3.set_xlabel('t'), ax3.set_ylabel('dt'), ax3.legend() fig3.suptitle(f'test_stiff: Van der Pol, mu={mu:.2f}, dt(t)') plt.show()
def test_stiff(): """ Проверяем явные и неявные методы на жёсткой задаче https://en.wikipedia.org/wiki/Van_der_Pol_oscillator Q: почему даже метод Розенброка иногда уменьшает шаг почти до нуля? """ t0 = 0 t1 = 2500 mu = 1000 y0 = np.array([2., 0.]) ode = VanDerPol(y0, mu) fig, axs = plt.subplots(2, 2, figsize=(10, 8)) colors = 'rgbcmyk' for i, (method, adapt_type) in enumerate([ (ExplicitEulerMethod(), AdaptType.RUNGE), (ImplicitEulerMethod(), AdaptType.RUNGE), (EmbeddedRosenbrockMethod(coeffs.rosenbrock23_coeffs), AdaptType.EMBEDDED), ]): ode.clear_call_counter() ts, ys = adaptive_step_integration(method, ode, y0, (t0, t1), adapt_type=adapt_type, atol=1e-6, rtol=1e-3) print( f'{method.name}: {len(ts)-1} steps, {ode.get_call_counter()} RHS calls' ) axs[0, 0].plot([y[0] for y in ys], [y[1] for y in ys], f'{colors[i]}.--', label=method.name) axs[0, 1].plot(ts[:-1], np.diff(ts), f'{colors[i]}.--', label=method.name) axs[1, 0].plot(ts, [y[0] for y in ys], f'{colors[i]}.--', label=method.name) axs[1, 1].plot(ts, [y[1] for y in ys], f'{colors[i]}.--', label=method.name) axs[0, 0].legend(), axs[0, 0].set_title('y(x)') axs[0, 1].legend(), axs[0, 1].set_title('dt(t)') axs[1, 0].legend(), axs[1, 0].set_title('x(t)') axs[1, 1].legend(), axs[1, 1].set_title('y(t)') fig.suptitle(f'test_stiff: Van der Pol, mu={mu:.2f}') fig.tight_layout() plt.show()
def test_stiff(): """ test explicit vs implicit methods on a stiff problem """ t0 = 0 t1 = 800*np.pi mu = 1000 y0 = np.array([2., 0.]) f = VanDerPol(y0, mu) fig1, ax1 = plt.subplots() fig2, (ax21, ax22) = plt.subplots(1, 2) fig3, ax3 = plt.subplots() colors = 'rgbcmyk' for i, (method, adapt_type) in enumerate( [ (ExplicitEulerMethod(), AdaptType.RUNGE), (ImplicitEulerMethod(), AdaptType.RUNGE), (EmbeddedRosenbrockMethod(coeffs.rosenbrock23_coeffs), AdaptType.EMBEDDED), ] ): f.clear_call_counter() ts, ys = adaptive_step_integration(method, f, y0, (t0, t1), adapt_type=adapt_type, atol=1e-6, rtol=1e-3) print(f'{method.name}: {len(ts)-1} steps, {f.get_call_counter()} RHS calls') ax1.plot([y[0] for y in ys], [y[1] for y in ys], f'{colors[i]}.--', label=method.name) ax21.plot(ts, [y[0] for y in ys], f'{colors[i]}.--', label=method.name) ax22.plot(ts, [y[1] for y in ys], f'{colors[i]}.--', label=method.name) ax3.plot(ts[:-1], np.array(ts[1:]) - np.array(ts[:-1]), f'{colors[i]}.--', label=method.name) ax1.set_xlabel('x'), ax1.set_ylabel('y'), ax1.legend() fig1.suptitle(f'test_stiff: Van der Pol, mu={mu:.2f}, y(x)') ax21.set_xlabel('t'), ax21.set_ylabel('x'), ax21.legend() ax22.set_xlabel('t'), ax22.set_ylabel('y'), ax22.legend() fig2.suptitle(f'test_stiff: Van der Pol, mu={mu:.2f}, x(t)') ax3.set_xlabel('t'), ax3.set_ylabel('dt'), ax3.legend() fig3.suptitle(f'test_stiff: Van der Pol, mu={mu:.2f}, dt(t)') plt.show()
def test_stiff(): """ test explicit vs implicit methods on a stiff problem """ t0 = 0 t1 = 8*np.pi mu = 0 y0 = np.array([2., 0.]) f = VanDerPol(y0, mu) colors = 'rgbcmyk' for i, (method, adapt_type) in enumerate( [ (ExplicitEulerMethod(), AdaptType.RUNGE), (ImplicitEulerMethod(), AdaptType.RUNGE), (RungeKuttaMethod(collection.rk4_coeffs), AdaptType.RUNGE), (EmbeddedRosenbrockMethod(collection.rosenbrock23_coeffs), AdaptType.EMBEDDED), ] ): f.clear_call_counter() ts, ys = adaptive_step_integration(method, f, y0, (t0, t1), adapt_type=adapt_type, atol=1e-6, rtol=1e-3) print(f'{method.name}: {len(ts)-1} steps, {f.get_call_counter()} RHS calls') plt.figure(1) plt.plot([y[0] for y in ys], [y[1] for y in ys], f'{colors[i]}.--', label=method.name) plt.figure(2) plt.subplot(1,2,1), plt.plot(ts, [y[0] for y in ys], f'{colors[i]}.--', label=method.name) plt.subplot(1,2,2), plt.plot(ts, [y[1] for y in ys], f'{colors[i]}.--', label=method.name) plt.figure(3) plt.plot(ts[:-1], np.array(ts[1:]) - np.array(ts[:-1]), f'{colors[i]}.--', label=method.name) plt.figure(1) plt.xlabel('x'), plt.ylabel('y'), plt.legend() plt.suptitle(f'test_stiff: Van der Pol, mu={mu:.2f}, y(x)') plt.figure(2) plt.subplot(1,2,1), plt.xlabel('t'), plt.ylabel('x'), plt.legend() plt.subplot(1,2,2), plt.xlabel('t'), plt.ylabel('y'), plt.legend() plt.suptitle(f'test_stiff: Van der Pol, mu={mu:.2f}, x(t)') plt.figure(3) plt.xlabel('t'), plt.ylabel('dt'), plt.legend() plt.suptitle(f'test_stiff: Van der Pol, mu={mu:.2f}, dt(t)') plt.show()