Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
75 changes: 75 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Emacs backups
*~

# Pycharm IDE directory
.idea

# IPython Notebooks
*/.ipynb_checkpoints

# Ipython just for testing
archive_notebooks

# svg files
*.svg

# memo for codeship setting
codeship_settings.txt
59 changes: 59 additions & 0 deletions inagaki/predator_prey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env python

import numpy as np
import scipy.integrate as desol
import matplotlib.pyplot as plt

def predator_prey(f, t, a, b, c, d):
#
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

関数やクラスのコメントは3連のダブルコーテーションで囲う法が良いと思います。
そうするとfunction.__docs__にセットされるので。

https://pep8-ja.readthedocs.io/ja/latest/#id22

# dx/dt = ax - bxy
# dy/dt = cxy - dy
#
# f[0] - x: Population of prey
# f[1] - y: Population of predetor
# t - Time
# a,b,c,d - Control parameters
#
return [a*f[0]-b*f[0]*f[1], c*f[0]*f[1]-d*f[1]]


#model
#eq1 = r"$\frac{dx}{dt} = ax - bxy$"
#eq2 = r"$\frac{dy}{dt} = cxy - dy$"
eq1 = r"$dx/dt = ax - bxy$"
eq2 = r"$dy/dt = cxy - dy$"

#input parameters
a = 1.0
b = 1.0
c = 1.0
d = 1.0
header = r"$a={0:.1f}, b={1:.1f}, c={2:.1f}, d={3:.1f}$".format(a, b, c, d)

#initial condition
f0 = [1.0, 0.1]

#independent variable
nt = 1000
tmax = 30.0
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PythonのスタイルガイドPEP8では、イコールの位置を揃えない、というふうになっています。

https://pep8-ja.readthedocs.io/ja/latest/#id15

dt = tmax/nt
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

** 以外の演算子は両側にスペースで区切ることが推奨されています。

https://pep8-ja.readthedocs.io/ja/latest/#id15

t = dt*np.arange(nt)

f = desol.odeint(predator_prey, f0, t, args=(a,b,c,d))

#plot
prey = f[:,0]
predator = f[:,1]

fig = plt.figure()
ax = fig.add_axes([0.15, 0.1, 0.8, 0.8])
ax.plot(t, prey, color='r', label=r"$x$: prey")
ax.plot(t, predator, color='b', label=r"$y$: predator")
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles, labels, loc='best')
ax.text(21, 2.7, eq1, fontsize=16)
ax.text(21, 2.5, eq2, fontsize=16)
ax.set_xlabel("Time")
ax.set_ylabel("Population")
ax.set_title(header)
plt.show()