최소 자승 풀이
!python -m pip install sympy
!python -m pip install control
Requirement already satisfied: sympy in /usr/local/lib/python3.9/dist-packages (1.12) Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.9/dist-packages (from sympy) (1.3.0) WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv WARNING: You are using pip version 21.2.4; however, version 24.0 is available. You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command. Requirement already satisfied: control in /usr/local/lib/python3.9/dist-packages (0.9.4) Requirement already satisfied: numpy in /usr/local/lib/python3.9/dist-packages (from control) (1.24.4) Requirement already satisfied: scipy>=1.3 in /usr/local/lib/python3.9/dist-packages (from control) (1.8.1) Requirement already satisfied: matplotlib in /usr/local/lib/python3.9/dist-packages (from control) (3.5.2) Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.9/dist-packages (from matplotlib->control) (4.33.3) Requirement already satisfied: pyparsing>=2.2.1 in /usr/local/lib/python3.9/dist-packages (from matplotlib->control) (3.0.9) Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.9/dist-packages (from matplotlib->control) (9.1.1) Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.9/dist-packages (from matplotlib->control) (1.4.2) Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.9/dist-packages (from matplotlib->control) (2.8.2) Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.9/dist-packages (from matplotlib->control) (21.3) Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.9/dist-packages (from matplotlib->control) (0.11.0) Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.9/dist-packages (from python-dateutil>=2.7->matplotlib->control) (1.16.0) WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv WARNING: You are using pip version 21.2.4; however, version 24.0 is available. You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
import sympy
import control as ct
import numpy as np
import matplotlib.pyplot as plt
a, b = sympy.symbols('a b')
A = np.asarray([
[3, 1],
[2, 1],
[1 ,1],
[2, 1],
[3, 1]
])
B = np.asarray([
[4],
[3],
[3],
[4],
[5]
])
np.linalg.pinv(A)@B
array([[0.78571429], [2.07142857]])
np.linalg.inv(A.transpose()@A)@A.transpose()@B
array([[0.78571429], [2.07142857]])