dejsdukes
dejsdukes

Reputation: 121

Solving and Graphing a Nonlinear first-order ODE

So far I've been semi-successful in solving and graphing the nonlinear ode dn/dt = n^2-2n-3 for two initial conditions, n(0)=-5 and n(0)=1, but when I add one last line to the graph with the initial condition n(0)=10, everything gets wacky, and the graph doesn't look like what it's supposed or behave like the other two lines.

the code is:

import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate
#import warnings
#warnings.simplefilter('ignore')

def func(N, t):
    return(N**2 - 2*N - 3)

tvec = np.arange(0,11)

s_1 = scipy.integrate.odeint(func, y0=-5,t = tvec)
s_2 = scipy.integrate.odeint(func,y0=1, t=tvec)
s_3 = scipy.integrate.odeint(func, y0 = 10, t=tvec)

%matplotlib inline
plt.plot(tvec,s_1, label="N0=-5")
plt.plot(tvec,s_2, label="N0=1")
plt.plot(tvec, s_3, label="N0=10")
plt.ylim(-5,10)
plt.legend();

the culprit here being s_3.

enter image description here

Any ideas on how to fix this?

Upvotes: 0

Views: 65

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114831

Your differential equation has an unstable equilibrium point at N = 3. Any initial condition larger than 3 results in a solution that blows up in finite time. That's the mathematical statement; numerically, the values will become extremely large, and the ODE solver will eventually start generating nonsense. If any of the "nonsense" values happen to end up being less than 3, the "solution" will then converge to the stable equilibrium at N = -1.

Upvotes: 1

Related Questions