diff --git a/main.typ b/main.typ index b683a31..9f3ac0b 100644 --- a/main.typ +++ b/main.typ @@ -15,7 +15,7 @@ Collection of solutions to programming exercises as part of Introduction to Prog ) #{ - let count = 10; + let count = 11; for week in range(1, count + 1).filter(it => it != 8) { let a = "./week" + str(week) + "/doc.typ" include a diff --git a/week11/Chaos.java b/week11/Chaos.java new file mode 100644 index 0000000..06b5fa9 --- /dev/null +++ b/week11/Chaos.java @@ -0,0 +1,40 @@ +import common.StdDraw; + +class Chaos { + public static void main(String[] args) { + var x = 0.01; + var r = Double.parseDouble(args[0]); + var buffer = new double[200]; + buffer[0] = x; + StdDraw.enableDoubleBuffering(); + + var t = 0; + while(true) { + System.out.format("t = %3d; x = %s\n", t, x); + x = Math.clamp(nextGeneration(x, r), 0, 1); + + var offset = t % buffer.length; + if(offset == 0) { + StdDraw.clear(); + buffer = new double[200]; + } + buffer[offset] = x; + for(var toDraw = 0; toDraw < buffer.length; toDraw++) { + var to = buffer[toDraw]; + StdDraw.line(toDraw / (double)buffer.length, 0, toDraw / (double) buffer.length, to); + } + if(offset % 10 == 0) { + StdDraw.show(); + StdDraw.pause(100); + } + if(offset >= 1 && buffer[offset] == buffer[offset - 1]) { // stabilized value + StdDraw.pause(5000); + } + t += 1; + } + } + + static double nextGeneration(double x, double r) { + return r * x * (1 - x); + } +} \ No newline at end of file diff --git a/week11/common b/week11/common new file mode 120000 index 0000000..60d3b0a --- /dev/null +++ b/week11/common @@ -0,0 +1 @@ +../common \ No newline at end of file diff --git a/week11/doc.typ b/week11/doc.typ new file mode 100644 index 0000000..272e4be --- /dev/null +++ b/week11/doc.typ @@ -0,0 +1,40 @@ +#import "./common/common.typ" : * + +#show: template + += Week 10 + +== Exercise 1.3.45 + +_Chaos_. Write a program to study the following simple model for population growth, which might be applied to study fish in a pond, bacteria in a test tube, +or any of a host of similar situations. We suppose that the population ranges from +$0$ (extinct) to $1$ (maximum population that can be sustained). If the population at +time $t$ is $x$, then we suppose the population at time $t + 1$ to be $r x (1-x)$, where the +argument r, known as the _fecundity parameter_, controls the rate of growth. Start +with a small population—say, $x = 0.01$—and study the result of iterating the model, for various values of $r$. For which values of $r$ does the population stabilize at +$x = 1 - 1/r$ ? Can you say anything about the population when $r$ is $3.5$? $3.8$? $5$? + +#table( + columns: 3, + $3.5$, + $3.8$, + $5$, + $2$, + [Oscillation between 4 values], + [Sustained chaos], + [Dies from overpopulation in 5 ticks], + [Sustained $1-1/r$] +) + +== Exercise 1.4.26 + +_Music shuffling_. You set your music player to shuffle mode. It plays each of +the n songs before repeating any. Write a program to estimate the likelihood that +you will not hear any sequential pair of songs (that is, song 3 does not follow song +2, song 10 does not follow song 9, and so on). + +== Exercise 1.5.32 + +_Clock_. Write a program that displays an animation of the second, minute, +and hour hands of an analog clock. Use the method `StdDraw.pause(1000)` to +update the display roughly once per second.