|
| 1 | +"""Usage: |
| 2 | +``` |
| 3 | +plot_trajectory(100, 3.6, 0.1) |
| 4 | +plot_bifurcation(2.5, 4.2, 0.001) |
| 5 | +``` |
| 6 | +""" |
| 7 | +import numpy as np |
| 8 | +from matplotlib import pyplot as plt |
| 9 | + |
| 10 | +from logistic import iterate_f |
| 11 | + |
| 12 | + |
| 13 | +def plot_trajectory(n, r, x0, fname="single_trajectory.png"): |
| 14 | + """ |
| 15 | + Saves a plot of a single trajectory of the logistic function |
| 16 | +
|
| 17 | + inputs |
| 18 | + n: int (number of iterations) |
| 19 | + r: float (r value for the logistic function) |
| 20 | + x0: float (between 0 and 1, starting point for the iteration) |
| 21 | + fname: str (filename to which to save the image) |
| 22 | +
|
| 23 | + returns |
| 24 | + fig, ax (matplotlib objects) |
| 25 | + """ |
| 26 | + xs = iterate_f(n, x0, r) |
| 27 | + fig, ax = plt.subplots(figsize=(10, 5)) |
| 28 | + ax.plot(list(range(n)), xs) |
| 29 | + fig.suptitle('Logistic Function') |
| 30 | + |
| 31 | + fig.savefig(fname) |
| 32 | + return fig, ax |
| 33 | + |
| 34 | + |
| 35 | +def plot_bifurcation(start, end, step, fname="bifurcation.png", it=100000, |
| 36 | + last=300): |
| 37 | + """ |
| 38 | + Saves a plot of the bifurcation diagram of the logistic function. The |
| 39 | + `start`, `end`, and `step` parameters define for which r values to |
| 40 | + calculate the logistic function. If you space them too closely, it might |
| 41 | + take a very long time, if you dont plot enough, your bifurcation diagram |
| 42 | + won't be informative. Choose wisely! |
| 43 | +
|
| 44 | + inputs |
| 45 | + start, end, step: float (which r values to calculate the logistic |
| 46 | + function for) |
| 47 | + fname: str (filename to which to save the image) |
| 48 | + it: int (how many iterations to run for each r value) |
| 49 | + last: int (how many of the last iterates to plot) |
| 50 | +
|
| 51 | +
|
| 52 | + returns |
| 53 | + fig, ax (matplotlib objects) |
| 54 | + """ |
| 55 | + r_range = np.arange(start, end, step) |
| 56 | + x = [] |
| 57 | + y = [] |
| 58 | + |
| 59 | + for r in r_range: |
| 60 | + xs = iterate_f(it, 0.1, r) |
| 61 | + all_xs = xs[len(xs) - last::].copy() |
| 62 | + unique_xs = np.unique(all_xs) |
| 63 | + y.extend(unique_xs) |
| 64 | + x.extend(np.ones(len(unique_xs)) * r) |
| 65 | + |
| 66 | + fig, ax = plt.subplots(figsize=(20, 10)) |
| 67 | + ax.scatter(x, y, s=0.1, color='k') |
| 68 | + ax.set_xlabel("r") |
| 69 | + fig.savefig(fname) |
| 70 | + return fig, ax |
0 commit comments