Oct-19-2017, 05:47 PM
Trying to run this code, but getting the error: must be str not int?
Need some help here
Just to mention that i have tried to chabge the inputs to int, but then I get the error that only length-1 arrays can be converted to Python scalars
Need some help here
Just to mention that i have tried to chabge the inputs to int, but then I get the error that only length-1 arrays can be converted to Python scalars
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
N_0 = input ( 'Give initial population size N_0: ' ) r = input ( 'Give net growth rate r: ' ) dt = input ( 'Give time step size: ' ) N_t = input ( 'Give number of steps: ' ) from numpy import linspace, zeros t = linspace( 0 , (N_t + 1 ) * dt, N_t + 2 ) N = zeros(N_t + 2 ) N[ 0 ] = N_0 for n in range (N_t + 1 ): N[n + 1 ] = N[n] + r * dt * N[n] from math import exp import matplotlib.pyplot as plt numerical_sol = 'bo' if N_t < 70 else 'b-' plt.plot(t, N, numerical_sol, t, N_0 * exp(r * t), 'r-' ) plt.legend([ 'numerical' , 'exact' ], loc = 'upper left' ) plt.xlabel( 't' ); plt.ylabel( 'N(t)' ) filestem = 'growth1_%dsteps' % N_t plt.savefig( '%s.png' % filestem); plt.savefig( '%s.pdf' % filestem) |