import matplotlib.pyplot as plt
from scipy import *
from scipy import integrate
from scipy.integrate import odeint
import numpy as np
from IPython.display import Image
%matplotlib inline
def LotkaVolterra(z,t,a,b,c,d):
x,y=z
dydt = [a*x-b*x*y,-c*y+d*x*y]
return dydt
A=2.0/3.0
B=4.0/3.0
C=1
D=1
y0 = [1.5, 0.5]
t = np.linspace(0, 20, 1001)
sol = odeint(LotkaVolterra, y0, t, args=(A,B,C,D))
fig = plt.figure(num=1,figsize=(10,10))
plt.plot(t, sol[:, 0], 'b', label='x(t) = Prey')
plt.plot(t, sol[:, 1], 'g', label='y(t) = Predator')
plt.legend(loc='best')
plt.xlabel('t')
plt.grid()
fig = plt.figure(num=1,figsize=(10,10))
ax=fig.add_subplot(111)
xmin=0;xmax=2;ymin=0;ymax=1;
gridx=30;gridy=30;
x=np.array(range(100))/100.;x=xmin+(xmax-xmin)*x;
plt.xlim([xmin,xmax])
plt.ylim([ymin,ymax])
plt.xlabel(r"$x$",fontsize=18)
plt.ylabel(r"$y$",fontsize=18)
plt.tick_params(axis='both', which='major', labelsize=16)
A=2.0/3.0
B=4.0/3.0
C=1
D=1
#Vector field
X,Y = np.meshgrid( np.linspace(xmin,xmax,gridx),np.linspace(xmin,xmax,gridy) )
dX = A*X-B*X*Y
dY = -C*Y + D*X*Y
#Normalize arrows
N = np.sqrt(dX**2+dY**2)
U, V = dX/N, dY/N
ax.quiver( X,Y,U, V,minshaft=5,width=0.005,headwidth=0,color="black")
# Ploting function
#c=-0.5
#f=c*exp(np.arcsin(x))
ax.plot(sol[:,0],sol[:,1])
sol2 = odeint(LotkaVolterra, [1,0.6],t, args=(A,B,C,D))
ax.plot(sol2[:,0],sol2[:,1])
sol3 = odeint(LotkaVolterra, [1,0.95],t, args=(A,B,C,D))
ax.plot(sol3[:,0],sol3[:,1])
plt.show()
Image("duffing.png",width=600)
def LotkaVolterra(z,t,a,b,c,d):
x,v=z
dzdt = [v,-a*v-b*x-c*x**3+d*np.cos(t)]
return dzdt
A=0.2
B=-1
C=1
D=0.3
z0 = [1.5, 0.5]
z1 = [1.5002,0.5]
t = np.linspace(0,200, 1001)
sol1 = odeint(LotkaVolterra, z0, t, args=(A,B,C,D))
fig = plt.figure(num=1,figsize=(10,10))
plt.plot(t, sol1[:, 0], 'b', label='x(t)')
plt.legend(loc='best')
plt.xlabel('t')
plt.ylabel('x')
plt.figure(num=1,figsize=(10,10))
plt.show()
t = np.linspace(0, 55, 1001)
sol = odeint(LotkaVolterra, z0, t, args=(A,B,C,D))
sol2 = odeint(LotkaVolterra, z1, t, args=(A,B,C,D))
fig = plt.figure(num=1,figsize=(10,10))
plt.plot(t, sol[:, 0], 'b', label='x(t)')
plt.plot(t, sol2[:, 0], 'r', label='x(t)')
plt.legend(loc='best')
plt.xlabel('t')
plt.ylabel('x')
plt.figure(num=1,figsize=(10,10))
plt.show()
fig = plt.figure(num=1,figsize=(10,10))
plt.plot(sol[:, 0],sol[:,1], 'b', label='x(t)')
plt.plot(sol2[:, 0],sol2[:,1], 'r', label='x(t)')
plt.legend(loc='best')
plt.xlabel('x')
plt.ylabel('y')
plt.figure(num=1,figsize=(10,10))
plt.show()
Image("Sierpinski_triangle1.png",width=1000)