#! /usr/bin/env python3 # def pendulum_euler ( ): #*****************************************************************************80 # ## pendulum_euler() solves the pendulum ODE using euler_system(). # from euler_system import euler_system from pendulum_conserved import pendulum_conserved from pendulum_dydt import pendulum_dydt import matplotlib.pyplot as plt import numpy as np print ( '' ) print ( 'pendulum_euler:' ) print ( ' Solve the pendulum ODE using euler_system()' ) t0 = 0.0 tstop = 20.0 tspan = np.array ( [ t0, tstop ] ) y0 = np.array ( [ np.pi / 3.0, 0.0 ] ) n = 400 t, y = euler_system ( pendulum_dydt, tspan, y0, n ) # # Time plot. # plt.clf ( ) plt.plot ( t, y, linewidth = 3 ) plt.grid ( True ); plt.xlabel ( '<-- Time -->' ) plt.ylabel ( '<-- Theta, dThetadt -->' ) plt.legend ( [ 'Theta', 'dThetadt' ] ) plt.title ( 'pendulum, euler, time plot' ) filename = 'pendulum_euler_time.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) plt.show ( ) plt.close ( ) # # Conservation plot. # h = pendulum_conserved ( t, y ) plt.clf ( ) plt.plot ( t, h, 'c-', lineWidth = 3 ) plt.plot ( [ t0, tstop ], [ 0.0, 0.0 ], 'k-', linewidth = 3 ) plt.grid ( 'on' ) plt.xlabel ( '<-- Time -->' ) plt.ylabel ( '<-- Energy(t) -->' ) plt.title ( 'pendulum, euler, conservation plot' ) filename = 'pendulum_euler_conservation.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) plt.show ( ) plt.close ( ) return if ( __name__ == "__main__" ): pendulum_euler ( )