#! /usr/bin/env python3 # from forward_euler import forward_euler from stiff_ode import stiff_ode, stiff_solution, stiff_lambda import matplotlib.pyplot as plt import numpy as np print ( "exercise1:" ) # # Set the value of lambda to 4. # LAMBDA = 0.25 stiff_lambda ( LAMBDA ) # # Solve the ODE. # f_ode = stiff_ode xRange = np.array ( [ 0.0, 2.0 * np.pi ] ) yInit = 0.0 numSteps = 50 x1, y1 = forward_euler ( f_ode, xRange, yInit, numSteps ) # # Get the exact solution. # x2 = np.linspace ( 0.0, 2.0 * np.pi, 101 ) y2 = stiff_solution ( x2 ) # # Plot them together. # plt.plot ( x1, y1, 'b-' ) plt.plot ( x2, y2, 'r-' ) plt.legend ( [ 'forward_euler()', 'Exact' ] ) plt.grid ( True ) plt.title ( 'y = stiff_solution(x), LAMBDA = ' + str ( LAMBDA ) ) filename = 'exercise1.jpg' plt.savefig ( filename ) print ( " Graphics saved as ", filename ) plt.show ( ) plt.close ( )