LoginSignup
lotkav
@lotkav

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

ロトカヴォルテラ方程式競争系の解軌道

解決したいこと

縦軸が個体数x,y、横軸時間tのグラフを
縦軸が個体数x、横軸が個体数yのグラフで表したいのですが
どう変えればいいのかがわかりません。
for文、座標設定ができていないのかなと思っています。

S__11984899.jpg

上の画像のようなグラフを出したいです。
解決方法を教えていただきたいです。

以下のコードのパラメータは画像の左下図で設定しています。

該当するソースコード

import numpy as np
import matplotlib.pyplot as plt

dt = 0.05
t0 = 0
te = 800

ts = np.arange(t0, te + dt, dt)

a = 3.0
b = 1.0
c=2.0
d=2.0
e=1.0
f=1.0
x = 2.0
y = 2.0

xs, ys  = [], []

for t in ts:
    xs.append(x)
    ys.append(y)

    x += (a -b* xs[-1] - c * ys[-1] ) * xs[-1] * dt
    y += (d - e * xs[-1] -f* ys[-1] ) * ys[-1] * dt

    print(f"(x, y, ) = ({x:.3e}, {y:.3e}) ({t:.2e} sec)\r", end=" ")


fig, ax = plt.subplots()

ax.plot(ts, xs, label="x")
ax.plot(ts, ys, label="y")


ax.set_xlabel("t")
ax.set_ylabel("x, y")
ax.legend()

plt.show()




自分で試したこと

import numpy as np
import matplotlib.pyplot as plt

dt = 0.05
t0 = 0
te = 800

ts = np.arange(t0, te + dt, dt)

a = 3.0
b = 1.0
c=2.0
d=2.0
e=1.0
f=1.0
x = 0.3
y = 0.3

xs, ys  = [], []

for t in ts:
    xs.append(x)
    ys.append(y)

    x += (a -b* xs[-1] - c * ys[-1] ) * xs[-1] * dt
    y += (d - e * xs[-1] -f* ys[-1] ) * ys[-1] * dt

    print(f"(x, y, ) = ({x:.3e}, {y:.3e}) ({t:.2e} sec)\r", end=" ")


fig, ax = plt.subplots()

ax.plot(xs, ys, label="x")
ax.plot(xs, ys, label="y")


ax.set_xlabel("x")
ax.set_ylabel("y")
ax.legend()

plt.show()

image.png
少し変えてみて試したところ
このような結果になってしまいます

ご教授いただければ幸いです。
よろしくお願いいたします。

0

1Answer

テキストの画像のようなグラフを作るには初期値ごとに計算が必要です。

ロトカヴォルテラ.png

import numpy as np
import matplotlib.pyplot as plt

t0, te, dt = 0, 800, 0.05
ts = np.arange(t0, te + dt, dt)

# パラメータ
a = 3.0
b = 1.0
c = 2.0
d = 2.0
e = 1.0
f = 1.0

# 初期値の組のリスト
init = [
    (2, 2),
    (2, 1.3),
    (0.2, 0.5),
    (0.2, 0.25)
]

fig, ax = plt.subplots()

for x, y in init:
    xs, ys = [], []

    for t in ts:
        xs.append(x)
        ys.append(y)

        x += (a - b * xs[-1] - c * ys[-1] ) * xs[-1] * dt
        y += (d - e * xs[-1] - f * ys[-1] ) * ys[-1] * dt

        print(f"(x, y) = ({x:.3f}, {y:.3f}) ({t:.2f} sec)\r", end=" ")

    ax.plot(xs, ys, label=f"(x0, y0) = ({xs[0]:.1f}, {ys[0]:.1f})")

ax.set_xlabel("x")
ax.set_ylabel("y")
ax.legend()

plt.show()
0

Comments

  1. @lotkav

    Questioner
    @bizzpaperさん ご回答ありがとうございます。

    先日に引き続きご教授いただきありがとうございます。
    初期値ごとに計算するにはこのようにfor文を書けばいいのですね。

    初心者なので勉強になることばかりです。少しずつ頑張っていきます。
    ありがとうございました!
  2. お力添えできたようで何よりです。

    Pythonに限らず、for文は規模が大きくなっていくほど重要になります。
    今のうちから処理の構造をきちんと考え、for文などを活用して簡潔に書くクセをつけておくとよいでしょう。

    引き続きがんばってください!

Your answer might help someone💌