#! /usr/bin/env python3 # def sphere_euler ( ): #*****************************************************************************80 # ## sphere_euler() solves the sphere ODE using euler_system(). # from euler_system import euler_system from sphere_conserved import sphere_conserved from sphere_dydt import sphere_dydt import matplotlib.pyplot as plt import numpy as np print ( '' ) print ( 'sphere_euler:' ) print ( ' Solve the sphere ODE using euler_system()' ) t0 = 0.0 tstop = 45.0 tspan = np.array ( [ t0, tstop ] ) y0 = np.array ( [ np.cos ( 0.9 ), 0.0, np.sin ( 0.9 ) ] ) n = 201 t, y = euler_system ( sphere_dydt, tspan, y0, n ) # # Time plot. # plt.clf ( ) plt.plot ( t, y, linewidth = 3 ) plt.grid ( True ); plt.xlabel ( '<-- Time -->' ) plt.ylabel ( '<-- Position -->' ) plt.legend ( [ 'P', 'Q', 'R' ] ) plt.title ( 'sphere, euler, time plot' ) filename = 'sphere_euler_time.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) plt.show ( ) plt.close ( ) # # Conservation plot. # P = sphere_conserved ( t, y ) plt.clf ( ) plt.plot ( t, P, 'c-', lineWidth = 3 ) plt.plot ( [ t0, tstop ], [ 0.0, 0.0 ], 'k-', linewidth = 3 ) plt.grid ( 'on' ) plt.xlabel ( '<-- Time -->' ) plt.ylabel ( '<-- P(t) -->' ) plt.title ( 'sphere, euler, conservation plot' ) filename = 'sphere_euler_conservation.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) plt.show ( ) plt.close ( ) return if ( __name__ == "__main__" ): sphere_euler ( )