Solution (2.16)#

Solution to Exercise 2.16

To model the equations we can follow the same structure we used in the workshop.

# import necessary libraries
import numpy as np
import matplotlib.pyplot as plt

# set up variables and arrays
n = 300
k1 = 0.2
k2 = 0.4
k3 = 0.1
k4 = 0.3

X = np.zeros(n)
Y = np.zeros(n)

# initialise variables (not strictly necessary here!)
X[0] = 0
Y[0] = 0

# implement equations
for i in range(n - 1):
    X[i+1] = X[i] + k1 - k2*X[i] + k3*(X[i]**2)*Y[i]
    Y[i+1] = Y[i] + k4*X[i] - k3*(X[i]**2)*Y[i]

# plot so we can see what happens
plt.figure(figsize=(3,3))
plt.plot(X, label="X")
plt.plot(Y, label="Y")
plt.xlabel("Time (seconds)")    
plt.ylabel("Concentration")
plt.legend()
    
plt.figure(figsize=(3,3))
plt.plot(X, Y)
plt.xlabel("X concentration")
plt.ylabel("Y concentration")
Text(0, 0.5, 'Y concentration')
../_images/27c6321bcb021a0afad4ad9c2ba99c7ad146babb11e4a024815e96151cac1225.png ../_images/8cab4cca2fc32e42a14190fcf275ccd4f93d0292b5d46577db7f437889276701.png

Note that how we have used plt.plot(X, label="X") and plt.legend() to add a legend to the plot.

Experiment with \(k_1\)#

We can use np.linspace to test a few different values for \(k_1\). Let’s try the interval \([0, 0.3]\).

for k1 in np.linspace(0.1, 0.3, 10):

    X = np.zeros(n)
    X[0] = 0
    Y = np.zeros(n)
    Y[0] = 0

    for i in range(n - 1):
        X[i+1] = X[i] + k1 - k2*X[i] + k3*X[i]**2*Y[i]
        Y[i+1] = Y[i] + k4*X[i] - k3*X[i]**2*Y[i]

    plt.figure(figsize=(3,3))
    plt.plot(X, Y)
    plt.xlabel("X concentration")
    plt.ylabel("Y concentration")
    plt.title(k1) # add title to each figure so we can see what the value is

    plt.figure(figsize=(3,3))
    plt.plot(X)
    plt.plot(Y)
    plt.xlabel("Time (seconds)")
    plt.ylabel("Concentrations")
    plt.title(k1) # add title to each figure so we can see what the value is
../_images/9b00139817f95c9750aa686fe3b327dcc685d013e9319b389ed2df147e538753.png ../_images/1b13c1f85af068066c5a7852e8fe19a26689c5b70043449cf04f5fbc7c7d1ce5.png ../_images/d92ded4e2fa62cedb21455e3e50bae1ac1bd1822ec816ce7b505a307eca20607.png ../_images/243f5287ab9f8bfd7a2258094aa7b873abf2611bd2e0256a81926e55aa38a812.png ../_images/89f5ff43e8a078be527bd828f4bd35a99450a0eb3e9199a2e15b7ed5347b8c7d.png ../_images/4553bc0b30cc0e473fe8b8ba93f936171d4361ebce243f65d5f03f284937f9d7.png ../_images/cd06def0c2b4f958da4f0b41c4d7fc4bba8b80f25127aa8402b0aac9635333fd.png ../_images/32e946b95b54bce802fcd10cddc6176a1245f62624732150d4b3a508bdad2d96.png ../_images/1c645f3f3cd38c4307479dea9ab14905ab7d0d3ee237c0e94332c20a763aac1b.png ../_images/ecc80a3547834953196265d3e22f36e2a3793f2887c0faf80f1b51619dc2e368.png ../_images/61381a465f6f3e44e25ce5e09e162481566d9689e56bed74a99b662ba1231404.png ../_images/c06eda860c275245cab916d0c832ac9d6a4fa1a146dbab6697b444b4d7085c43.png ../_images/ef73d6a6aa318d93c4b6fdcc06f59b855951941b686dca3236a41f22c7c1a173.png ../_images/3dd27a7de1d7e354fa4d8e47188c137ac6690a0d9c3ed37224bb2c6ec358d9f6.png ../_images/262da56fbc9bf06c8be471ec55d2ae1ea26b5323f81bfdfd6f9c837c47f1c3bc.png ../_images/42fcade20609c59ff9aaaeea364d7852c8d212bd6f876c7c39e0ce41cc829557.png ../_images/6687ceddac1e87a44dad4f3085b7eb986ff39e1af5519aff2286c89be9363fb9.png ../_images/9e92118189e7202c63d50e18bb5acbad891a39a245542665c95fc94a14be5ed1.png ../_images/52e234a9ba6ee3f09a13f109e5b3cbf91b2c76470bc8a01ff588b6894292424b.png ../_images/ae79bd6a407d4413a01d807e54245eb3dba44701c75ff4badd620430ef3a7109.png

We see oscillations first appear when \(k_1 \approx 0.079\), these continue until \(k_1 \approx 0.25\) when \(X\) stops oscillating.