Seite 1 von 1

Lotka-Volterra DGL Plot liefert nicht das richtige Ergebnis

Verfasst: Sonntag 1. August 2021, 23:59
von Physiker2020
Hallo liebes Forum,

ich bin neu in Python unterwegs (für das Studium sinnvoll) und möchte lernen DGLs mit Python zu lösen. Dazu habe ich das Lotka-Volterra Modell nach dieser Anleitung https://www.youtube.com/watch?v=2f5aRTBmm10 auf YouTube nachgebaut. Allerdings erhalte ich einen völlig anderen Plot, der auch wenig sinnvoll ist... Was mache ich falsch/anders?

Code: Alles auswählen

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from scipy.integrate import odeint

y0 = [1000,100]

t = np.linspace(0,100,num=1000)

alpha = 0.1
beta = 0.4
delta = 0.1
gamma = 0.4

params = [alpha, beta, gamma, delta, gamma]

def sim(variables, t, params):
    x = variables[0]
    y = variables[1]

    alpha = params[0]
    beta = params[1]
    delta = params[2]
    gamma = params[3]

    dxdt = alpha * x - beta * x * y
    dydt = delta * x * y - gamma * y

    return([dxdt,dydt])

y = odeint(sim, y0, t, args=(params,))

f,(ax1,ax2) = plt.subplots(2)

line1, = ax1.plot(t,y[:,0], color="b")
line2, = ax2.plot(t,y[:,1], color="r")

plt.show()
Als Graph erhalte einmal eine Konstante und einmal einen scheinbaren exponentiellen Zerfall, aber das ist absoluter Schwachsinn inhaltlich.... Die Modellgleichungen passen auch, habe ich irgendwas falsch eingestellt? Ich stehe gerade echt auf den Schlauch...

Vielen Dank für jede Unterstützung!

VG Physiker2020

Re: Lotka-Volterra DGL Plot liefert nicht das richtige Ergebnis

Verfasst: Montag 2. August 2021, 08:19
von Sirius3
Statt einzelne Parameter aus dem Tuple herauszupflücken, benutzt man Tuple-Unpacking, oder übergibt gleich die Parameter einzeln.
`return` ist keine Funktion, die Klammern daher sehr verwirrend.

Code: Alles auswählen

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from scipy.integrate import odeint

def sim(variables, t, alpha, beta, delta, gamma):
    x, y = variables
    dxdt = alpha * x - beta * x * y
    dydt = delta * x * y - gamma * y
    return [dxdt, dydt]

alpha = 0.1
beta = 0.4
delta = 0.1
gamma = 0.4

y0 = [1000,100]
t = np.linspace(0,100,num=1000)
y = odeint(sim, y0, t, args=(alpha, beta, delta, gamma))

f, (ax1, ax2) = plt.subplots(2)
ax1.plot(t,y[:,0], color="b")
ax1.plot(t,y[:,1], color="r")
ax2.plot(y[:,0],y[:,1], color="r")
plt.show()
Welche Differentialgleichung möchtest Du überhaupt lösen? Deine Ableitungen sind ja gar nicht von t abhängig.

Re: Lotka-Volterra DGL Plot liefert nicht das richtige Ergebnis

Verfasst: Montag 2. August 2021, 08:27
von einfachTobi
Ich rate dazu solve_ivp() zu verwenden, weil mehr Möglichkeiten. In der Doku gibt es unten auch ein Beispiel zur Lotka-Volterra-Gleichungen.

Re: Lotka-Volterra DGL Plot liefert nicht das richtige Ergebnis

Verfasst: Montag 2. August 2021, 10:03
von rogerb
@Physiker2020,

Du hast den Code falsch abgetippt oder so verändert, dass die Werte keinen Sinn mehr machen.

Der Source Code zum Video:
https://github.com/mikesaint-antoine/Co ... or-prey.py

Der Startwert für die Population ist zu hoch
y0 = [1000,100]

Das passt nicht:

Code: Alles auswählen

params = [alpha, beta, gamma, delta, gamma]
zu dem:

Code: Alles auswählen

alpha = params[0]
beta = params[1]
delta = params[2]
gamma = params[3]
Folge am besten dem Rat von Sirius3 und belasse die Parameter als lokale Variablen.