Search code examples
pythonmatplotlibmath

Exponential growth function doesn't match example plot


I've attempted to replicate the figure 4.4.6 in this 4.4: The Logistic Equation chapter

enter image description here

However, my plot doesn't match the above example, as the exponential function is pushed over to the right. I double-checked my work, but unsure where I went wrong. Any hints?

enter image description here

import numpy as np
import math
import matplotlib.pyplot as plt

x = np.linspace(-25,25,100)
# P(t)=\dfrac{1,072,764e^{0.2311t}}{0.19196+e^{0.2311t}}
a = [1072764*np.exp(0.2311*x) / (0.19196+np.exp(0.2311*x)) for x in x]
b = 900000*np.exp(0.2311*x) # P(t)=900000e^{(0.2311x)}
plt.axhline(y=1072764, color='r', linestyle='--', label = 'P=1,072,764')
plt.ticklabel_format(style='plain') 
plt.axis([-20, 20, 0, 1400000]) # let's zoom in to see this better

plt.plot(x, a, 'darkblue', label = '$P_0=900,000$') # plotting x, a separately 
plt.plot(x, b, 'turquoise', label = 'Exponential Growth') # plotting x, b separately 
plt.xlabel("x")
plt.ylabel("y")
plt.legend(loc='lower right')
# plt.savefig('plots', dpi=300, bbox_inches='tight')
plt.show()

Solution

  • The figure that you are trying to replicate shows an exponential curve that matches value and slope at a point xref (here, -20). Such an exponential curve would have the form enter image description here

    where Lref and L' are value and slope at the reference point. These latter can be found exactly if you know the formula of your logistic curve L(x), but to a perfectly adequate approximation may be deduced from your already-computed numerical data.

    Thus:

    import numpy as np
    import math
    import matplotlib.pyplot as plt
    
    x = np.linspace(-20,20,100)
    a = [1072764*np.exp(0.2311*x) / (0.19196+np.exp(0.2311*x)) for x in x]
    
    xref, Lref, Lprime = x[0], a[0], ( a[1] - a[0] ) / ( x[1] - x[0] )
    b = Lref * np.exp( (Lprime/Lref) * ( x - xref ) )
    
    plt.axhline(y=1072764, color='r', linestyle='--', label = 'P=1,072,764')
    plt.ticklabel_format(style='plain') 
    plt.axis([-20, 20, 0, 1400000]) # let's zoom in to see this better
    
    plt.plot(x, a, 'darkblue', label = '$P_0=900,000$') # plotting x, a separately 
    plt.plot(x, b, 'turquoise', label = 'Exponential Growth') # plotting x, b separately 
    plt.xlabel("x")
    plt.ylabel("y")
    plt.legend(loc='lower right')
    # plt.savefig('plots', dpi=300, bbox_inches='tight')
    plt.show()
    

    produces output enter image description here