0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Python]状態空間の時間応答を計算するスクリプト(2入力系)

Last updated at Posted at 2024-08-03

・ 時間応答はStep応答やインテンシャル応答ではない、任意の入力に対する応答。lsimを使用して計算する。
・ 状態空間表現(状態空間モデル)はssを使用する。

a01_Contorol_test2.ipynb
from control.matlab import*
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

# システム行列
A = np.array([[0, 1], [-4, -5]])
B = np.array([[0, 1], [1, 0]])
C = np.array([[1, 0], [0, 1]])
D = np.array([[0, 0], [0, 0]])

# 状態空間モデル
sys = ss(A, B, C, D)

# シミュレーション時間
t = np.linspace(0, 5, 1024)

# 正弦波入力と余弦波入力
freq1 = 1.0
freq2 = 0.1
amp1 = 1.0
amp2 = 0.5
u1 = amp1 * np.sin(2 * np.pi * freq1 * t)
u2 = amp2 * np.cos(2 * np.pi * freq2 * t)

# 同時入力
u_combined = np.vstack((u1, u2)).T  # Transpose to match the expected shape

# lsimで応答計算
x, _, _ = lsim(sys, U=u_combined, T=t)

# グラフ表示
plt.figure(figsize=(10, 6))

plt.subplot(2, 1, 1)
plt.plot(t, x[:, 0], label="State 1")
plt.plot(t, x[:, 1], label="State 2")
plt.xlabel("Time")
plt.ylabel("State Variables")
plt.title("System Response to Sinusoidal and Cosine Inputs (States)")
plt.legend()
plt.grid(True)

plt.subplot(2, 1, 2)
plt.plot(t, u1, label="Input 1 (sin input)")
plt.plot(t, u2, label="Input 2 (cos input)")
plt.xlabel("Time")
plt.ylabel("Input Signals")
plt.title("Input Signals (Sinusoidal and Cosine)")
plt.legend()
plt.grid(True)

plt.tight_layout()
plt.show()

実行結果
出力例2.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?