def california_migration_scalar_test ( ): #*****************************************************************************80 # ## california_migration_scalar_test() tests california_migration_scalar(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 19 January 2025 # # Author: # # John Burkardt # import matplotlib.pyplot as plt import numpy as np print ( '' ) print ( 'california_migration_scalar_test():' ) print ( ' Test california_migration_scalar().' ) print ( '' ) print ( ' Year CA Pop US pop Total' ) print ( '' ) m = 21 ca = np.zeros ( m ) us = np.zeros ( m ) year = np.linspace ( 1960, 1960 + m - 1, m ) for i in range ( 0, m ): if ( i == 0 ): ca[i] = 16000000 us[i] = 164000000 else: ca[i], us[i] = california_migration_scalar ( ca[i-1], us[i-1] ) print ( ' %4d %9.0f %9.0f %9.0f' \ % ( year[i], ca[i], us[i], ca[i] + us[i] ) ) plt.clf ( ) plt.plot ( year, ca, linewidth = 3 ) plt.plot ( year, us, linewidth = 3 ) plt.grid ( True ) plt.xlabel ( '<-- Year -->' ) plt.ylabel ( '<-- Population -->' ) plt.title ( 'California population scalar' ) plt.legend ( [ 'CA', 'US' ] ) filename = 'california_migration_scalar.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) plt.close ( ) return def california_migration_scalar ( ca, us ): #*****************************************************************************80 # ## california_migration_scalar() approximates next year's California population. # # Discussion: # # For a while in the 1960's, the following statement was approximately true: # # "Every year, 30% of the population of California leaves the state, and # every year, 10% of the population of the other states moves to California." # # 1) The statement sounds nonsensical. Can we write down some equations # that give us numbers we can think about? # # 2) If 30% move out, and 10% move it, does this mean California is # gradually going to have no population at all? # # 3) If this behavior lasts long enough, does the population curve # of California look chaotic, go towards infinity, become negative, # or oscillatory, or does it settle down? # # We can make some simplifying assumptions: # # * Assume that the 30% and 10% values are exact and don't change. # # * The population estimates will be real numbers with fractional parts, # but we'll just accept figures like a population of 10,000.73 # # * Find the populations of California and the US in 1960, and assume # that the total population never changes after that, but just # moves back and forth. # # Our goal will be simply to compute the populations of California and # the US (minus California), for each year, from 1960 onwards, and to # plot the California population, and to compute it long enough that # we feel we understand the pattern, if any. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 19 January 2025 # # Author: # # John Burkardt # # Input: # # real CA, US: the populations of California and the rest of the US # at the end of this year. # # Output: # # real CA_NEXT, US_NEXT: the populations next year. # ca_next = 0.70 * ca + 0.10 * us us_next = 0.30 * ca + 0.90 * us return ca_next, us_next if ( __name__ == "__main__" ): california_migration_scalar_test ( )