Home > Mobile >  Lotka-Volterra difference equation array-trouble
Lotka-Volterra difference equation array-trouble

Time:10-19

I am trying to write a program that graphically plots the Lotka-Volterra interaction between two species (fox and rabbit) using two difference equations.

The two equations in question are:

enter image description here

The initial state for rabit is supposed to be R[0] = 100 and for fox F[0] = 20

Usually, when I'm encountered with numerically solving difference equations in python, I write something like this:

import numpy as np

N = 15
x = np.zeros(N 1, int)

x[0] = 1
x[1] = 1

for n in range(2, N 1):
    x[n] = x[n-1]   x[n-2]
    print(f"x[{n}] = {x[n]}")

I thought something like the strategy above also could work for the 'Lotka-Volterra-difference-equation' problem as well.

So I tried the following code and got a few unpleasant surprises:

import numpy as np
import matplotlib.pyplot as plt

N = 500
a = 0.4
b = 0.1
c = 0.005
e = 0.2

R = np.zeros(N 1)
F = np.zeros(N 1)
t = np.linspace(0,500,num=10_000)

R[0] = 100
F[0] = 20

for n in range(2,N):
    # Rabbit
    R[n] = R[n-1]   a*R[n-1] - c*R[n-1] * F[n-1]
    #Fox
    F[n] = F[n - 1]   e * c * R[n - 1] * F[n - 1] - b * F[n - 1]
    print(R[n],F[n])

plt.plot(t,R[n])
plt.show()

The problems with the code is:

1. I don't produce any other out print than

0.0 0.0
0.0 0.0
0.0 0.0
0.0 0.0
0.0 0.0

2. It gives a lot of error messages if you try to plot it using matplotlib/pyplot.

File "C:/Users/(...)lotka_volterra.py", line 36, in <module>
    plt.plot(t,R[n])
  File "C:\Users\(...)\lib\site-packages\matplotlib\pyplot.py", line 3019, in plot
    return gca().plot(
  File "C:\Users\(...)\lib\site-packages\matplotlib\axes\_axes.py", line 1605, in plot
    lines = [*self._get_lines(*args, data=data, **kwargs)]
  File "C:\Users\(...)\lib\site-packages\matplotlib\axes\_base.py", line 315, in __call__
    yield from self._plot_args(this, kwargs)
  File "C:\Users\(...)\lib\site-packages\matplotlib\axes\_base.py", line 501, in _plot_args
    raise ValueError(f"x and y must have same first dimension, but "
ValueError: x and y must have same first dimension, but have shapes (10000,) and (1,)

Is there anyone who is kind enough to help me out with this problem? And perhaps also provide a solution?

All help is appreciated and welcomed.

CodePudding user response:

As pointed out by @Andy in his comment on your post, you set only the 0th index of both R and F, but your loop starts at index 2 and only looks back 1, meaning it never uses the 0th index of either array. Therefore, all subsequent equations will use only zeroes and thus every element of R and F are only being set to 0's, as you see in (1). To fix this, start your for loop at 1 and go to N:

for n in range(1,N):
    # etc

The plotting error is coming from the fact that your X-coordinates are from an array of 10,000 points while your y-coordinates are being chosen as only 1 from 500. You want both to be size 500, so you should define t as follows:

t = np.arange(0,N)
plt.plot(t, R)

Let me know if you have any other questions.

David

  • Related