-
Notifications
You must be signed in to change notification settings - Fork 453
Expand file tree
/
Copy pathsysnorm_test.py
More file actions
74 lines (59 loc) · 3.45 KB
/
sysnorm_test.py
File metadata and controls
74 lines (59 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# -*- coding: utf-8 -*-
"""
Tests for sysnorm module.
Created on Mon Jan 8 11:31:46 2024
Author: Henrik Sandberg
"""
import control as ct
import numpy as np
import pytest
def test_norm_1st_order_stable_system():
"""First-order stable continuous-time system"""
s = ct.tf('s')
G1 = 1/(s+1)
assert np.allclose(ct.norm(G1, p='inf'), 1.0) # Comparison to norm computed in MATLAB
assert np.allclose(ct.norm(G1, p=2), 0.707106781186547) # Comparison to norm computed in MATLAB
Gd1 = ct.sample_system(G1, 0.1)
assert np.allclose(ct.norm(Gd1, p='inf'), 1.0) # Comparison to norm computed in MATLAB
assert np.allclose(ct.norm(Gd1, p=2), 0.223513699524858) # Comparison to norm computed in MATLAB
def test_norm_1st_order_unstable_system():
"""First-order unstable continuous-time system"""
s = ct.tf('s')
G2 = 1/(1-s)
assert np.allclose(ct.norm(G2, p='inf'), 1.0) # Comparison to norm computed in MATLAB
with pytest.warns(UserWarning, match="System is unstable!"):
assert ct.norm(G2, p=2) == float('inf') # Comparison to norm computed in MATLAB
Gd2 = ct.sample_system(G2, 0.1)
assert np.allclose(ct.norm(Gd2, p='inf'), 1.0) # Comparison to norm computed in MATLAB
with pytest.warns(UserWarning, match="System is unstable!"):
assert ct.norm(Gd2, p=2) == float('inf') # Comparison to norm computed in MATLAB
def test_norm_2nd_order_system_imag_poles():
"""Second-order continuous-time system with poles on imaginary axis"""
s = ct.tf('s')
G3 = 1/(s**2+1)
with pytest.warns(UserWarning, match="Poles close to, or on, the imaginary axis."):
assert ct.norm(G3, p='inf') == float('inf') # Comparison to norm computed in MATLAB
with pytest.warns(UserWarning, match="Poles close to, or on, the imaginary axis."):
assert ct.norm(G3, p=2) == float('inf') # Comparison to norm computed in MATLAB
Gd3 = ct.sample_system(G3, 0.1)
with pytest.warns(UserWarning, match="Poles close to, or on, the complex unit circle."):
assert ct.norm(Gd3, p='inf') == float('inf') # Comparison to norm computed in MATLAB
with pytest.warns(UserWarning, match="Poles close to, or on, the complex unit circle."):
assert ct.norm(Gd3, p=2) == float('inf') # Comparison to norm computed in MATLAB
def test_norm_3rd_order_mimo_system():
"""Third-order stable MIMO continuous-time system"""
A = np.array([[-1.017041847539126, -0.224182952826418, 0.042538079149249],
[-0.310374015319095, -0.516461581407780, -0.119195790221750],
[-1.452723568727942, 1.7995860837102088, -1.491935830615152]])
B = np.array([[0.312858596637428, -0.164879019209038],
[-0.864879917324456, 0.627707287528727],
[-0.030051296196269, 1.093265669039484]])
C = np.array([[1.109273297614398, 0.077359091130425, -1.113500741486764],
[-0.863652821988714, -1.214117043615409, -0.006849328103348]])
D = np.zeros((2,2))
G4 = ct.ss(A,B,C,D) # Random system generated in MATLAB
assert np.allclose(ct.norm(G4, p='inf'), 4.276759162964244) # Comparison to norm computed in MATLAB
assert np.allclose(ct.norm(G4, p=2), 2.237461821810309) # Comparison to norm computed in MATLAB
Gd4 = ct.sample_system(G4, 0.1)
assert np.allclose(ct.norm(Gd4, p='inf'), 4.276759162964228) # Comparison to norm computed in MATLAB
assert np.allclose(ct.norm(Gd4, p=2), 0.707434962289554) # Comparison to norm computed in MATLAB