#! /usr/bin/env python3 # def exercise10 ( ): #*****************************************************************************80 # ## exercise10() applies the midpoint method to stiff_ode. # from midpoint import midpoint from stiff_ode import stiff_ode, stiff_lambda, stiff_solution import matplotlib.pyplot as plt import numpy as np print ( "exercise10:" ) LAMBDA = 55.0 stiff_lambda ( LAMBDA ) y_exact = stiff_solution ( 2.0 * np.pi ) numSteps = np.array ( [ 40, 80, 160, 320, 640, 1280, 2560 ] ) e = np.zeros ( 7 ) print ( '' ) print ( ' e[k]' ) print ( '' ) for k in range ( 0, 7 ): f = stiff_ode xRange = np.array ( [ 0.0, 2.0 * np.pi ] ) yInit = 0.0 x, y = midpoint ( f, xRange, yInit, numSteps[k] ) e[k] = np.abs ( y_exact - y[numSteps[k]] ) print ( k, e[k] ) print ( '' ) print ( ' r[k] = e[k] / e[k+1]' ) print ( '' ) r = np.zeros ( 6 ) for k in range ( 0, 6 ): r[k] = e[k] / e[k+1] print ( k, r[k] ) return if ( __name__ == "__main__" ): exercise10 ( )