Welcome to Project Lovelace! We're still super new so there are still tons of bugs to find and improvements to make. If you have any suggestions, complaints, or any comments at all please let us know on Discourse!

Chaos

You will learn about: chaos theory and loops.

Let's look at an example of chaos arising from simplicity in nature: the logistic map. It's a simplified model used to describe population sizes and can be written mathematically as $$ x_{n+1} = rx_n(1 - x_n) $$ where $0 < r < 4$ is a parameter and $0 < x_n < 1$ represents the population size at time step $n$. Starting with an initial population size of $x_0 = 0.5$ and $r$ as an input parameter, return a list of the first 50 values of the logistic map $x_0, x_1, x_2, \dots, x_{49}, x_{50}$. You should see different behavior depending on the value of $r$, but we'll leave you to play around with it and we'll have more to say in the solution notes.


Logistic map with $r=3.89$ using two initial conditions ($x_0=0.50$ and $x_0=0.49$). It shows two big features of chaotic systems. (1) It's sensitive to initial conditions as the two solutions start close to each other but quickly diverge and look completely different after just several iterations. (2) The solutions don't really repeat so they're nonperiodic which also means it's hard to predict them. (Plot generation code is in the solution notes.)


Input: The parameter $r$.

Output: A list containing the first 51 values of the logistic map $(x_0, x_1, x_2, \dots, x_{49}, x_{50})$.


Example 1 (population growth)

Input: 2.81 Output: [0.500000, 0.702500, 0.587272, ⋯, 0.644125, 0.644130, 0.644126]

Example 2 (steady state)

Input: 2 Output: [0.5, 0.5, 0.5, ⋯, 0.5, 0.5, 0.5]

Example 3 (chaotic behavior)

Input: 3.88 Output: [0.5, 0.97, 0.112908, ⋯, 0.940932, 0.215644, 0.656271]


 Difficulty  Timesink
 Maximum runtime 60 s  Max. memory usage 250 MiB
 Function signature logistic_map(r)

Write a function that accepts the input as function parameters and returns the correct output. Make sure to read the description above to produce the correct output in the correct format and use the correct function signature so we can run your code. A good first step is to try reproducing the example(s). Your code must not take longer than the maximum runtime to run and must not use more memory than the allowed limit.




You must be logged in to view your submissions.

Notes

  • The sensitivity to initial conditions exhibited by the logistic map is more commonly known as the butterfly effect. It means that unless you knew the initial conditions exactly, there will be uncertainty in future predictions and is why we can only predict the weather up to roughly two weeks ahead (much less in many cases).
  • If you plot the value the logistic map ends up at for each value of $r$ you get a bifurcation diagram for the logistic map, which matches up beautifully with the Mandelbrot set if you're into that sort of thing.

References

May R.M., Simple mathematical models with very complicated dynamics, Nature 261 (1976)
The logistic map was first introduced as a simple model of population dynamics with very complicated dynamics.

Let us know what you think about this problem! Was it too hard? Difficult to understand? Also feel free to discuss the problem, ask questions, and post cool stuff on Discourse. You should be able see a discussion thread below. Feel free to post your solutions but if you do please organize and document your code well so others can learn from it.