Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6a0e136
New function for LTI system norm computation
Jan 7, 2024
a0fbc80
* Updated documentation of function norm
Jan 8, 2024
457c623
Update sysnorm.py
Jan 9, 2024
adcb486
Merge branch 'python-control:main' into system-norms
henriks76 Jan 21, 2024
fd076c5
New function for LTI system norm computation
Jan 7, 2024
0fe2c57
* Updated documentation of function norm
Jan 8, 2024
cdf4bab
Update sysnorm.py
Jan 9, 2024
34f9537
Do not track changes in VS Code setup.
Jan 14, 2024
b419f12
Lowered tolerances in tests.
Jan 21, 2024
9ecd594
Added:
Jan 21, 2024
14fce67
Merge branch 'system-norms' of https://github.com/henriks76/python-co…
Jan 21, 2024
5c38d4f
escape latex labels
bnavigator Jan 22, 2024
99e56f8
Use Numpy API for pi instead of undocumented scipy.pi
bnavigator Jan 22, 2024
2c32913
Update notebooks (no rerun, no output change)
bnavigator Jan 22, 2024
85328d9
Merge pull request #965 from bnavigator/fix-examples
bnavigator Jan 23, 2024
7ace4bc
New function for LTI system norm computation
Jan 7, 2024
5103448
* Updated documentation of function norm
Jan 8, 2024
4fb252e
Update sysnorm.py
Jan 9, 2024
108817c
Do not track changes in VS Code setup.
Jan 14, 2024
6683eb3
Lowered tolerances in tests.
Jan 21, 2024
32d38bf
Added:
Jan 21, 2024
6f810ba
Update sysnorm.py
Jan 9, 2024
793f0d6
Added:
Jan 28, 2024
12ba677
Merge branch 'system-norms' of https://github.com/henriks76/python-co…
Jan 28, 2024
7a5af50
Fixed merge error with __all__.
Jan 28, 2024
49f7e5f
Update sysnorm.py
Jan 9, 2024
247cff5
Merge branch 'system-norms' of https://github.com/henriks76/python-co…
Jan 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update sysnorm.py
* Added additional tests and warning messages for systems with poles close to stability boundary
* Added __all__
* Added more comments
  • Loading branch information
Henrik Sandberg
Henrik Sandberg committed Jan 28, 2024
commit 4fb252eb602c01a522469013fe6eb04dedd625e1
110 changes: 88 additions & 22 deletions control/sysnorm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

Functions for computing system norms.

Routines in this module:
Routine in this module:

norm()
norm

Created on Thu Dec 21 08:06:12 2023
Author: Henrik Sandberg
Expand All @@ -16,9 +16,11 @@

import control as ct

__all__ = ['norm']

#------------------------------------------------------------------------------

def norm(system, p=2, tol=1e-6):
def norm(system, p=2, tol=1e-6, print_warning=True):
"""Computes norm of system.

Parameters
Expand All @@ -30,11 +32,13 @@ def norm(system, p=2, tol=1e-6):
tol : float
Relative tolerance for accuracy of L_infinity norm computation. Ignored
unless p='inf'.
print_warning : bool
Print warning message in case norm value may be uncertain.

Returns
-------
norm : float
Norm of system
norm_value : float or NoneType
Norm value of system (float) or None if computation could not be completed.

Notes
-----
Expand All @@ -54,52 +58,114 @@ def norm(system, p=2, tol=1e-6):
C = G.C
D = G.D

if p == 2: # H_2-norm
#
# H_2-norm computation
#
if p == 2:
# Continuous time case
if G.isctime():
if (D != 0).any() or any(G.poles().real >= 0):
poles_real_part = G.poles().real
if any(np.isclose(poles_real_part, 0.0)):
if print_warning:
print("Warning: Poles close to, or on, the imaginary axis. Norm value may be uncertain.")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use print here and in the error cases below. For warnings use warnings, if it is an error, please keep raising the exception. And be more specific than just Exception.

return float('inf')
elif (D != 0).any() or any(poles_real_part > 0.0): # System unstable or has direct feedthrough?
return float('inf')
else:
P = ct.lyap(A, B@B.T)
return np.sqrt(np.trace(C@P@C.T))
try:
P = ct.lyap(A, B@B.T)
except Exception as e:
print(f"An error occurred solving the continuous time Lyapunov equation: {e}")
return None

# System is stable to reach this point, and P should be positive semi-definite.
# Test next is a precaution in case the Lyapunov equation is ill conditioned.
if any(la.eigvals(P) < 0.0):
if print_warning:
print("Warning: There appears to be poles close to the imaginary axis. Norm value may be uncertain.")
return float('inf')
else:
norm_value = np.sqrt(np.trace(C@P@C.T)) # Argument in sqrt should be non-negative
if np.isnan(norm_value):
print("Unknown error. Norm computation resulted in NaN.")
return None
else:
return norm_value

# Discrete time case
elif G.isdtime():
if any(abs(G.poles()) >= 1):
poles_abs = abs(G.poles())
if any(np.isclose(poles_abs, 1.0)):
if print_warning:
print("Warning: Poles close to, or on, the complex unit circle. Norm value may be uncertain.")
return float('inf')
elif any(poles_abs > 1.0): # System unstable?
return float('inf')
else:
P = ct.dlyap(A, B@B.T)
return np.sqrt(np.trace(C@P@C.T + D@D.T))

elif p == "inf": # L_infinity-norm
try:
P = ct.dlyap(A, B@B.T)
except Exception as e:
print(f"An error occurred solving the discrete time Lyapunov equation: {e}")
return None

# System is stable to reach this point, and P should be positive semi-definite.
# Test next is a precaution in case the Lyapunov equation is ill conditioned.
if any(la.eigvals(P) < 0.0):
if print_warning:
print("Warning: There appears to be poles close to the complex unit circle. Norm value may be uncertain.")
return float('inf')
else:
norm_value = np.sqrt(np.trace(C@P@C.T + D@D.T)) # Argument in sqrt should be non-negative
if np.isnan(norm_value):
print("Unknown error. Norm computation resulted in NaN.")
return None
else:
return norm_value
#
# L_infinity-norm computation
#
elif p == "inf":
def _Hamilton_matrix(gamma):
"""Constructs Hamiltonian matrix."""
"""Constructs Hamiltonian matrix. For internal use."""
R = Ip*gamma**2 - D.T@D
invR = la.inv(R)
return np.block([[A+B@invR@D.T@C, B@invR@B.T], [-C.T@(Ip+D@invR@D.T)@C, -(A+B@invR@D.T@C).T]])

if G.isdtime(): # Bilinear transformation of discrete time system to s-plane if no poles at |z|=1 or z=0
# Discrete time case
# Use inverse bilinear transformation of discrete time system to s-plane if no poles on |z|=1 or z=0.
# Allows us to use test for continuous time systems next.
if G.isdtime():
Ad = A
Bd = B
Cd = C
Dd = D
if any(np.isclose(abs(la.eigvals(Ad)), 1.0)):
if print_warning:
print("Warning: Poles close to, or on, the complex unit circle. Norm value may be uncertain.")
return float('inf')
elif any(np.isclose(la.eigvals(Ad), 0.0)):
print("L_infinity norm computation for discrete time system with pole(s) at z = 0 currently not supported.")
print("L_infinity norm computation for discrete time system with pole(s) at z=0 currently not supported.")
return None

# Inverse bilinear transformation
In = np.eye(len(Ad))
Adinv = la.inv(Ad+In)
A = 2*(Ad-In)@Adinv
B = 2*Adinv@Bd
C = 2*Cd@Adinv
D = Dd - Cd@Adinv@Bd


# Continus time case
if any(np.isclose(la.eigvals(A).real, 0.0)):
if print_warning:
print("Warning: Poles close to, or on, imaginary axis. Norm value may be uncertain.")
return float('inf')

gaml = la.norm(D,ord=2) # Lower bound
gamu = max(1.0, 2.0*gaml) # Candidate upper bound
gaml = la.norm(D,ord=2) # Lower bound
gamu = max(1.0, 2.0*gaml) # Candidate upper bound
Ip = np.eye(len(D))

while any(np.isclose(la.eigvals(_Hamilton_matrix(gamu)).real, 0.0)): # Find an upper bound
while any(np.isclose(la.eigvals(_Hamilton_matrix(gamu)).real, 0.0)): # Find actual upper bound

@bnavigator bnavigator Jan 9, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered using SLICOT-Routines (already or to be wrapped by Slycot) optimized for certain Hamiltonian matrices instead of the generic scipy eigenvalue solver? (I didn't check if the SLICOT routines are applicable here)

gamu *= 2.0

while (gamu-gaml)/gamu > tol:
Expand All @@ -110,5 +176,5 @@ def _Hamilton_matrix(gamma):
gamu = gam
return gam
else:
print("Norm computation for p =", p, "currently not supported.")
print(f"Norm computation for p={p} currently not supported.")
return None