""" Exponential growth model with data saving """ import numpy as np steps = 10 t = np.zeros(steps+1) N = np.zeros(steps+1) r = 0.5 t[0] = 0 N[0] = 2 for i in range( steps ): N[i+1] = N[i] + r * N[i] t[i+1] = t[i] + 1 for i in range( steps+1 ): print( f"At time {t[i]}, N is {N[i]:6.4f}" ) # One way to save the data f = open( "ExpGrowth.txt", "w" ) for i in range( steps+1 ): print( f"{t[i]:4.1f} \t {N[i]:7.4f}", file=f ) f.close() # An alternate way to save the data output = np.column_stack( (t,N) ) np.savetxt( "ExpGrowth2.txt", output, fmt="%6.4f")