Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions logistic.py
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
# Your code goes here
def f(x,r):
return r*x*(1-x)

def iterate_f(x0,r,it):
iteration_timecourse = []

iteration_timecourse.append(x0)

x = f(x0,r)
iteration_timecourse.append(x)

for i in range(it-1):
x = f(x,r)
iteration_timecourse.append(x)

return iteration_timecourse
17 changes: 14 additions & 3 deletions plot_logistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from logistic import iterate_f


def plot_trajectory(n, r, x0, fname="single_trajectory.png"):
def plot_trajectory(x0,r,n, fname="single_trajectory.png"):
"""
Saves a plot of a single trajectory of the logistic function

Expand All @@ -23,9 +23,9 @@ def plot_trajectory(n, r, x0, fname="single_trajectory.png"):
returns
fig, ax (matplotlib objects)
"""
xs = iterate_f(n, x0, r)
xs = iterate_f(x0, r, n)
fig, ax = plt.subplots(figsize=(10, 5))
ax.plot(list(range(n)), xs)
ax.plot(list(range(n+1)), xs)
fig.suptitle('Logistic Function')

fig.savefig(fname)
Expand Down Expand Up @@ -68,3 +68,14 @@ def plot_bifurcation(start, end, step, fname="bifurcation.png", it=100000,
ax.set_xlabel("r")
fig.savefig(fname)
return fig, ax





x0=0.1
r=1.5
n=20

plot_trajectory(x0, r, n)

Binary file added single_trajectory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 34 additions & 11 deletions test_logistic.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
from numpy.testing import assert_allclose
from logistic import f, iterate_f
import pytest
from math import isclose
# Add here your test for the logistic map

from logistic import f
# def test_f_general():
# cases = [
# (0.1, 2.2, 0.198),
# (0.2, 3.4, 0.544),
# (0.5, 2, 0.5),
# ]
# for x, r, expected in cases:
# result = f(x, r)
# assert_allclose(result, expected)


# def test_f_corner_cases():
# # Test cases are (x, r, expected)
# cases = [
# (0, 1.1, 0),
# (1, 3.7, 0),
# ]
# for x, r, expected in cases:
# result = f(x, r)
# assert_allclose(result, expected)


# @pytest.mark.parametrize('x,r,expected',[(0.1,2.2,0.198),(0.2,3.4,0.544),(0.5,2,0.5),(0,1.1,0),(1,3.7,0)])
# def test_f(x,r,expected):
# result = f(x,r)
# assert isclose(result,expected)

# Add here your test for the logistic map


def test_f_corner_cases():
# Test cases are (x, r, expected)
cases = [
(0, 1.1, 0),
(1, 3.7, 0),
]
for x, r, expected in cases:
result = f(x, r)
assert_allclose(result, expected)
@pytest.mark.parametrize('x,r,it,expected',[(0.1,2.2,1,[0.1,0.198]),(0.2,3.4,4,[0.2,0.544,0.843418,0.449019,0.841163]),(0.5,2,3,[0.5,0.5,0.5,0.5])])
def test_iterate_f(x,r,it,expected):
result = iterate_f(x,r,it)
assert_allclose(result,expected,atol=0.001)