import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
plt.rc('figure',figsize=(14,9))
h=0.067
t=np.arange(0.0,1.32,h)
n=len(t)
x=np.zeros(n)
x[0]=1.0
for i in range(n-1):
x[i+1]=x[i]+h*x[i]*x[i]
plt.plot(t,x)
def F2(y):
z=y*y
return z
def RKF(y,FK,h):
k1 = h * FK(y)
k2 = h * FK(y + k1/2.0)
k3 = h * FK(y + k2/2.0)
k4 = h * FK(y + k3)
return y + ( k1 + 2.0 * k2 + 2.0 * k3 + k4 ) / 6.0
s=[0.0]
y=[1.0]
for i in range(n-1):
if y[i]<100.0:
s.append(s[i]+h)
y.append(RKF(y[i],F2,h))
else: break
plt.plot(s,y)
def FK(y):
N=(y[0]*y[0]+y[1]*y[1])**1.5
g=np.zeros(4)
g[0]=y[2]
g[1]=y[3]
g[2]=-3.0*y[0]/N
g[3]=-3.0*y[1]/N
return g
y=np.array([0,1.0,0,0])
g=FK(y)
print g
def solLV(h,T=80.0,x0=4.0,y0=4.2,u0=-0.40,v0=0.40):
t=np.arange(0.0,T,h)
n=len(t)
x=np.zeros((n,4))
x[0,0],x[0,1],x[0,2],x[0,3]=x0,y0,u0,v0
for i in range(n-1):
x[i+1,:]=x[i,:]+h*FK(x[i,:])
return (t,x)
t,x=solLV(0.01)
plt.plot(x[:,0],x[:,1])
plt.xlim([-10.0,10.0])
plt.ylim([-10.0,10.0])
from IPython.html.widgets import interactive
from IPython.display import display
def visu(k):
t,x=solLV(0.01)
plt.plot(x[:k,0],x[:k,1])
plt.xlim([-8.0,8.0])
plt.ylim([-8.0,8.0])
wdgt=interactive(visu,k=(2000,4000,200))
display(wdgt)
def solRK(h,T=40.0,x0=4.0,y0=4.2,u0=-0.40,v0=0.40):
t=np.arange(0.0,T,h)
n=len(t)
x=np.zeros((n,4))
x[0,0],x[0,1],x[0,2],x[0,3]=x0,y0,u0,v0
for i in range(n-1):
x[i+1,:]=RKF(x[i,:],FK,h)
return (t,x)
t,x=solRK(0.01)
plt.plot(x[:,0],x[:,1])
plt.xlim([-8.0,8.0])
plt.ylim([-8.0,8.0])
def visuRK(k):
t,x=solRK(0.01)
plt.plot(x[:k,0],x[:k,1])
plt.xlim([-8.0,8.0])
plt.ylim([-8.0,8.0])
wdgt=interactive(visuRK,k=(2000,4000,200))
display(wdgt)