2025.5.25 線形システムの過渡特性・定常特性
P-制御を適用した一次遅れシステムの過渡特性と定常特性を調べる。
参考:2024.3.4 Euler法によるばね-マス-ダンパ系のシミュレーション
状態ベクトル
$ \bf{x}(t) = \left[\begin{array}{c} y(t) \\ \dot{y}(t) \end{array} \right]
とし、$ yは台車の変位とする。
code:p1.py
import matplotlib.pyplot as plt
import torch as pt
import numpy as np
class Func_xd:
def __init__(self):
pass
def __call__(self, x, u):
return self.calc(x, u)
def calc(self, x, u):
return A@x + B@u
class Solver:
def __init__(self):
self.xd = Func_xd()
def solve_euler(self):
xd = self.xd
x = x0 # initial state
t = ts # start time
time = []
hist_x = []
hist_conv = []
loopflag = True
conv_count = 0
# start of iteration
while loopflag:
time.append(t)
hist_x.append(x.flatten())
#
y = C@x
err = ref - y
u = Kp@err
x = x + th*xd(x, u)
t = t + th
if t >= tf:
loopflag = False
conv = pt.linalg.norm(hist_x-1 - x.flatten())
hist_conv.append(conv)
if conv < conv_eps:
conv_count += 1
else:
conv_count = 0
if conv_count*th > conv_chk:
loopflag = False
# end of iteration
self.time = np.stack(time)
self.hist_x = pt.stack(hist_x, dim=0)
self.hist_conv = hist_conv
self.analyze()
return self.time, self.hist_x, hist_conv
def analyze(self):
hist_x = self.hist_x
time = self.time
chara = dict()
def get_time(val): # メソッド内関数
for i in range(len(hist_x) - 1):
if hist_xi,0 <= val <= hist_xi+1,0:
return timei
def get_sett_time(grade):
rate = chara'stead_val'/100*grade
print(rate, len(time))
for i in range(len(time)-1, 0, -1):
# print(hist_xi, 0 - chara'stead_val', rate)
if pt.abs(hist_xi, 0 - chara'stead_val') >= rate:
return i, timei
# 定常特性 #
# 定常値
chara'stead_val' = hist_x-1,0
# 定常偏差
chara'stead_err' = ref - chara'stead_val'
# 過渡特性 #
peak_index = pt.argmax(hist_x:,0)
chara'peak_index' = peak_index
# ピーク時間
chara'peak_time' = timepeak_index
# ピーク値
chara'peak_val' = hist_xpeak_index, 0
# 遅れ時間
chara'delay_time' = get_time(chara'stead_val' / 2)
# オーバーシュート
tmp = chara'peak_val' - chara'stead_val'
chara'overshoot' = (tmp / chara'stead_val') * 100
# 立ち上がり時間 (10% -> 90%)
val10 = chara'stead_val'*0.1
time10 = get_time(val10)
val90 = chara'stead_val'*0.9
time90 = get_time(val90)
chara'rise_time' = time90 - time10
# settime2 ... 整定時間2%
chara'sett_idx2', chara'sett_time2' = get_sett_time(2)
# settime5 ... 整定時間5%
chara'sett_idx5', chara'sett_time5' = get_sett_time(5)
self.chara = chara
return
def show(self):
for k, v in solver.chara.items():
print(k, v)
sett_x5 = [solver.time[solver.chara'sett_idx5']]
sett_y5 = [solver.hist_x[solver.chara'sett_idx5']0]
plt.grid()
plt.plot(self.time, self.hist_x:,0)
plt.plot(self.time, self.hist_x:,1)
plt.axhline(ref, c='b', lw=1)
plt.axhline(self.chara'stead_val', c='black', lw=0.7)
plt.axhline(0, c='black', lw=0.7)
plt.axvline(0, c='black', lw=0.7)
plt.show()
def get_params():
A = 0, 1], [-1 , -1 # A
B = 0], [1 # B
C = 0, 1 # C
Kp = 1 # Kゲイン
Ki = 0 # Iゲイン(未使用)
Kd = 0 # Dゲイン(未使用)
x0 = 0], [0 # 初期状態(y(0), yd(0)\top)
ref = 1 # 目標(今回はyに対する目標とする)
return A, B, C, Kp, Ki, Kd, x0, ref
def trans_tensor(params):
trans = []
for arg in params:
x = pt.tensor(arg, dtype=pt.float)
trans.append(x)
return trans
# toplevel
params = get_params()
A, B, C, Kp, Ki, Kd ,x0, ref = trans_tensor(params) # グローバル変数に返す
dim_m = A.shape0 # m-states
dim_n = B.shape1 # n-inputs
dim_l = C.shape0 # l-outputs
print(str(dim_n) + '-input, ' + str(dim_m) + '-state, ' + str(dim_l)+ '-output')
print('# A:', A)
print('# B:', B)
print('# C:', C)
ts, tf, th = 0, 40, 0.01 # 初期、終端、刻み時刻
conv_eps = 0.0000001 # 収束判定の振幅
conv_chk = 0.5 # s # 収束判定、時間
#
solver = Solver() # ソルバを生成
result = solver.solve_euler()
solver.show()
結果
code:result.txt
stead_val tensor(1.0000)
stead_err tensor(5.8413e-06)
peak_index tensor(1478)
peak_time 14.77999999999973
peak_val tensor(1.0000)
delay_time 1.6700000000000013
overshoot tensor(0.)
rise_time 3.3399999999999612
sett_idx2 580
sett_time2 5.799999999999921
sett_idx5 472
sett_time5 4.719999999999944
stead_val ... 定常値
stead_err ... 定常偏差
peak_index ... ピーク値に対応するインデックス
peak_time ... ピーク時間
peak_val ... ピーク値
delay_time ... 遅れ時間
overshoot ... オーバーシュート
rise_time ...立ち上がり時間
sett_idx2 ... 2%整定時間に対応するインデックス
sett_time2 ... 2%整定時間
sett_idx5 ... 5%整定時間に対応するインデックス
sett_time ... 5%整定時間