In [1]:
%matplotlib inline
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
In [2]:
import sys
sys.path.append('../numeric-core')
import numeric
Задача Коши:
\begin{align*} \begin{cases} \frac{dy}{dx} = -z\\ \frac{dz}{dx} = y\\ \end{cases} \end{align*}с начальными условиями $y(0) = 1$, $z(0) = 0$.
Аналитическое решение: $y(x) = cos(x)$, $z(x) = sin(x)$.
In [3]:
def dy(x, y, z):
return -z
def dz(x, y, z):
return y
In [4]:
T = 3.0
y, z = numeric.runge_kutta(dy, dz, 1.0, 0.0, T, 100)
In [5]:
grid = np.linspace(0, T, 100)
plt.plot(y[1], z[1], label='numeric Runge-Kutta')
plt.plot(np.cos(grid), np.sin(grid), label = 'analytic')
plt.legend(loc=3)
Out[5]:
In [6]:
plt.plot(grid, y[1])
plt.plot(grid, np.cos(grid))
Out[6]:
In [7]:
X, Y = np.meshgrid(y[1][::3], z[1][::3])
U = dy(0, X, Y)
V = dz(0, X, Y)
plt.quiver(X, Y, U, V)
plt.plot(y[1], z[1])
Out[7]: