Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting system of differential equations in Python

I've just started to use Python to plot numerical solutions of differential equations. I know how to use scipy.odeint to solve and to plot single differential equations, but I have no idea about systems of differential equations. How can I plot the following coupled system?

N' = a * N - (C/(1+C)) * b * N
C' = (C/(1+C)) * N - C + 1

a = 4
b = 7
N(0) = 100
C(0) = 5
like image 395
A.Loc Avatar asked Mar 18 '17 00:03

A.Loc


People also ask

How do you do differential equations in Python?

Differential equations are solved in Python with the Scipy. integrate package using function odeint or solve_ivp. t: Time points at which the solution should be reported. Additional internal points are often calculated to maintain accuracy of the solution but are not reported.

Does Python have an ODE solver?

t is a one-dimensional independent variable (time), S(t) is an n-dimensional vector-valued function (state), and the F(t,S(t)) defines the differential equations. S0 be an initial value for S. The function F must have the form dS=F(t,S), although the name does not have to be F.

How do I import an Odeint function in Python?

The Python code first imports the needed Numpy, Scipy, and Matplotlib packages. The model, initial conditions, and time points are defined as inputs to ODEINT to numerically calculate y(t). An optional fourth input is args that allows additional information to be passed into the model function.


1 Answers

Just define all of the variables as a space vector, then apply integration:

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

def f(s,t):
    a = 4
    b = 7
    n = s[0]
    c = s[1]
    dndt = a * n - (c/(c+1)) * b * n
    dcdt = (c/(c+1)) * n - c + 1
    return [dndt, dcdt]

t = np.linspace(0,20)
s0=[20,5]

s = odeint(f,s0,t)

plt.plot(t,s[:,0],'r--', linewidth=2.0)
plt.plot(t,s[:,1],'b-', linewidth=2.0)
plt.xlabel("t")
plt.ylabel("S[N,C]")
plt.legend(["N","C"])
plt.show()

enter image description here

like image 162
User Avatar answered Sep 28 '22 17:09

User