変数を式に代入する方法について。
D=0
で計算していますが、D=[0,0.01,0.1]
の三つをそれぞれf(x,v,t)
に代入し、1つのグラフで比較するにはどうすればいいですか?
import numpy as np
import matplotlib.pyplot as plt
def f(x, v, t):
M = 1.0
E = 0.38 if (t <= 0.15) else 1.2
J = (2*0.8)/(120*np.pi)
D = 0
return (M-E*np.sin(x)-D*v)/J
t0 = 0.0
t1 = 5.0
N = 500
del_t = (t1-t0)/N # time grid
tpoints = np.arange(t0, t1, del_t)
xpoints = []
vpoints = []
# initial condition
x0 = 0.653
v0 = 0.0
x, v = x0, v0
for t in tpoints:
xpoints.append(x)
vpoints.append(v)
k1v =f(x, v, t)*del_t
k1x = v * del_t
k2v = f(x+k1x/2, v+k1v/2, t+del_t/2 )*del_t
k2x =(v+k1v/2)*del_t
k3v =f (x+k2x/2, v+k2v/2, t+del_t/2 )*del_t
k3x =(v+k2v/2 ) *del_t
k4v = f(x+k3x, v+k3v, t+del_t )*del_t
k4x = (v+k3v )*del_t
v += (k1v + 2 * k2v + 2* k3v + k4v)/6
x += (k1x + 2 * k2x + 2* k3x + k4x)/6
plt.plot (tpoints, xpoints, 'o',label='4th order Runge-Kutta')
plt.xlabel("t", fontsize=24)
plt.ylabel("x(t)", fontsize=24)
plt.show()