<jupyter_start><jupyter_text>Linear Systems of Differential EquationsLet $y = (y_1(t),..., y_n(t))$ and $M = M(t)$ matrix with dimensions $n \times n$. The linear system of differential equations:\begin{equation}\frac{dy}{dt} = M y\end{equation}with initial point values $y(t = t_0) = y_0$ can be solved nummericaly using integrators (e.g. Runge-Kutta). But it has analytical solution using matrix exponent:\begin{equation}y(t) = e^{\int_{t_0}^t M(t) dt} y_0\end{equation}The simple script *diff_exp.py* implements both ways for obtaining solutions, and here is the comparison of the so solutions. First two examples cover the case where $M$ is constant matrix.<jupyter_code>M = np.array([[0,1], [1,-2]])
y0 = np.array([[1],[2]])
ti = 2
tf = 5
dt = 0.01
y, t = sol_lin_system_ode_c(y0, ti, tf, dt, M)
plt.plot(t, y[0])
plt.plot(t, y[1])
plt.show()
y0 = np.array([1, 2])
x, t = lin_system_ivp_c(y0, ti, tf, dt, M)
plt.plot(t, x[0])
plt.plot(t, x[1])
plt.show()
M = np.array([[0, 1, 0, -1], [1,-2, 5, 0], [0, 1, 2, 3], [-1, -2, 0, -5]])
y0 = np.array([[1],[2],[5],[0]])
ti = 2
tf = 5
dt = 0.01
y, t = sol_lin_system_ode_c(y0, ti, tf, dt, M)
plt.plot(t, y[0])
plt.plot(t, y[1])
plt.plot(t, y[2])
plt.plot(t, y[3])
plt.show()
y0 = np.array([1, 2, 5, 0])
x, t = lin_system_ivp_c(y0, ti, tf, dt, M)
plt.plot(t, x[0])
plt.plot(t, x[1])
plt.plot(t, x[2])
plt.plot(t, x[3])
plt.show()<jupyter_output><empty_output><jupyter_text>Let's compare two solutions:<jupyter_code>plt.plot(t, x[0]-y[0])
plt.plot(t, x[1]-y[1])
plt.plot(t, x[2]-y[2])
plt.plot(t, x[3]-y[3])
plt.show()<jupyter_output><empty_output><jupyter_text>And the relative errors: (*Warning* division with $0$ possible)<jupyter_code>plt.plot(t, np.abs(x[0]-y[0])/np.abs(y[0]))
plt.plot(t, np.abs(x[1]-y[1])/np.abs(y[1]))
plt.plot(t, np.abs(x[2]-y[2])/np.abs(y[2]))
plt.plot(t, np.abs(x[3]-y[3])/np.abs(y[3]))
plt.show()<jupyter_output>/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:4: RuntimeWarning: invalid value encountered in true_divide
  after removing the cwd from sys.path.<jupyter_text>Next example covers the case where $M$ is function of $t$.<jupyter_code>M = lambda t: np.array([[-t, 1], [np.exp(-t), -t**2]])
y0 = np.array([[1], [3]])
ti = 0
tf = 3
dt = 0.001
y, t = sol_lin_system_ode_l(y0, ti, tf, dt, M)
plt.plot(t, y[0])
plt.plot(t, y[1])
plt.show()
y0 = np.array([1, 3])
x, t = lin_system_ivp_l(y0, ti, tf, dt, M)
plt.plot(t, x[0])
plt.plot(t, x[1])
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2244237767893247, 'fraction_numerical': 0.044884755357864944, 'mean_word_length': 5.108641975308642, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '581712', 'license': 'MIT', 'n_tokens_mistral': 1215, 'n_tokens_neox': 1140, 'n_words': 269, 'path': 'Linear systems of differential equations/differential_equations.ipynb', 'repo_name': 'PhyProg/Scientific-Computing', 'size': '2473'}