Browse Source

Add pop. growth example

David Peter 2 years ago
parent
commit
f40f388a0a
4 changed files with 32 additions and 9 deletions
  1. 6 5
      book/build.sh
  2. 2 1
      book/src/SUMMARY.md
  3. 16 0
      book/src/example-population_growth.md
  4. 8 3
      examples/population_growth.nbt

+ 6 - 5
book/build.sh

@@ -26,18 +26,19 @@ generate_example() {
     ) > "$out_file"
 }
 
-generate_example medication_dosage "Medication dosage" true
+generate_example acidity "Acidity" true
 generate_example barometric_formula "Barometric formula" true
+generate_example body_mass_index "Body mass index" true
+generate_example factorial "Factorial" false
+generate_example medication_dosage "Medication dosage" true
 generate_example molarity "Molarity" true
 generate_example musical_note_frequency "Musical note frequency" true
-generate_example body_mass_index "Body mass index" true
 generate_example pipe_flow_rate "Flow rate in a pipe" true
+generate_example population_growth "Population growth" true
 generate_example recipe "Recipe" true
-generate_example acidity "Acidity" true
-generate_example factorial "Factorial" false
-generate_example xkcd_687 "XKCD 687" true
 generate_example xkcd_2585 "XKCD 2585" true
 generate_example xkcd_2812 "XKCD 2812" true
+generate_example xkcd_687 "XKCD 687" true
 
 generate_example numbat_syntax "Syntax overview" false
 

+ 2 - 1
book/src/SUMMARY.md

@@ -13,10 +13,11 @@
     - [Medication dosage](./example-medication_dosage.md)
     - [Molarity](./example-molarity.md)
     - [Musical note frequency](./example-musical_note_frequency.md)
+    - [Population growth](./example-population_growth.md)
     - [Recipe](./example-recipe.md)
-    - [XKCD 687](./example-xkcd_687.md)
     - [XKCD 2585](./example-xkcd_2585.md)
     - [XKCD 2812](./example-xkcd_2812.md)
+    - [XKCD 687](./example-xkcd_687.md)
 
 # Numbat language reference
 

+ 16 - 0
book/src/example-population_growth.md

@@ -0,0 +1,16 @@
+<!-- This file is autogenerated! Do not modify it -->
+
+# Population growth
+
+``` numbat
+# Exponential model for population growth
+
+let initial_population = 50_000 people
+let growth_rate = 2% per year
+
+fn predict_population(t: Time) =
+    initial_population × e^(growth_rate·t) // round
+
+print("Population in  20 years: {predict_population(20 years)}")
+print("Population in 100 years: {predict_population(1 century)}")
+```

+ 8 - 3
examples/population_growth.nbt

@@ -1,6 +1,11 @@
+# Exponential model for population growth
+
 let initial_population = 50_000 people
-let growth_rate: 1 / Time = 2 percent per year
+let growth_rate = 2% per year
 
-fn predict_population(time: Time) = initial_population × e^(growth_rate time) // round
+fn predict_population(t: Time) =
+    initial_population × e^(growth_rate·t) // round
 
-assert_eq(predict_population(10 years), 61_070 person)
+print("Population in  20 years: {predict_population(20 years)}")
+print("Population in 100 years: {predict_population(1 century)}")
+assert_eq(predict_population(20 years), 74_591 people)