Прикладные дифференциальные уравнения

Занятие 4

Илья Щуров

Факультет компьютерных наук, Прикладная математики и информатика, 2021-22 учебный год

Страница курса

In [1]:
import numpy as np
import plotly.graph_objects as go
import matplotlib.pyplot as plt
In [2]:
def matrix_exp(A, degree=100):
    n = A.shape[0]
    assert A.shape == (n, n)
    X = np.eye(n)
    terms = [X]
    for i in range(1, degree):
        X = (X @ A) / i
        terms.append(X)
    return np.array(terms).sum(axis=0)
In [3]:
matrix_exp(np.array([[1, 0], 
                     [0, 1]]), degree=100)
Out[3]:
array([[2.71828183, 0.        ],
       [0.        , 2.71828183]])
In [4]:
A = np.array([[0, 1, 0],
              [0, 0, 1],
              [-1, 0, 0]])
In [5]:
def trajectory(A, x0, delta_t, steps):
    exp_Adt = matrix_exp(A * delta_t)
    traj = [x0]
    for i in range(steps - 1):
        traj.append(exp_Adt @ traj[-1])
    return np.array(traj)
In [6]:
tr_n = 50
initial_values = [np.random.uniform(-0.1, 0.1, 3) for i in range(tr_n)]
trajectories = [
    trajectory(A, initial_value, 0.1, 30) 
    for initial_value in initial_values
] + [
    trajectory(-A, initial_value, 0.1, 30) 
    for initial_value in initial_values
]
go.Figure(
    [
        go.Scatter3d(x=traj[:, 0], y=traj[:, 1], z=traj[:, 2], 
                     mode="lines")
        for traj in trajectories
    ]
)