-
Notifications
You must be signed in to change notification settings - Fork 442
Implement okid #1031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
KybernetikJo
wants to merge
2
commits into
python-control:main
Choose a base branch
from
KybernetikJo:implement_okid
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Implement okid #1031
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# okid_msd.py | ||
# Johannes Kaisinger, 13 July 2024 | ||
# | ||
# Demonstrate estimation of Markov parameters via okid. | ||
# SISO, SIMO, MISO, MIMO case | ||
|
||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
import os | ||
|
||
import control as ct | ||
|
||
def create_impulse_response(H, time, transpose, dt): | ||
"""Helper function to use TimeResponseData type for plotting""" | ||
|
||
H = np.array(H, ndmin=3) | ||
|
||
if transpose: | ||
H = np.transpose(H) | ||
|
||
q, p, m = H.shape | ||
inputs = np.zeros((p,p,m)) | ||
|
||
issiso = True if (q == 1 and p == 1) else False | ||
|
||
input_labels = [] | ||
trace_labels, trace_types = [], [] | ||
for i in range(p): | ||
inputs[i,i,0] = 1/dt # unit area impulse | ||
input_labels.append(f"u{[i]}") | ||
trace_labels.append(f"From u{[i]}") | ||
trace_types.append('impulse') | ||
|
||
output_labels = [] | ||
for i in range(q): | ||
output_labels.append(f"y{[i]}") | ||
|
||
return ct.TimeResponseData(time=time[:m], | ||
outputs=H, | ||
output_labels=output_labels, | ||
inputs=inputs, | ||
input_labels=input_labels, | ||
trace_labels=trace_labels, | ||
trace_types=trace_types, | ||
sysname="H_est", | ||
transpose=transpose, | ||
plot_inputs=False, | ||
issiso=issiso) | ||
|
||
# set up a mass spring damper system (2dof, MIMO case) | ||
# Mechanical Vibrations: Theory and Application, SI Edition, 1st ed. | ||
# Figure 6.5 / Example 6.7 | ||
# m q_dd + c q_d + k q = f | ||
m1, k1, c1 = 1., 4., 1. | ||
m2, k2, c2 = 2., 2., 1. | ||
k3, c3 = 6., 1. | ||
|
||
A = np.array([ | ||
[0., 0., 1., 0.], | ||
[0., 0., 0., 1.], | ||
[-(k1+k2)/m1, (k2)/m1, -(c1+c2)/m1, c2/m1], | ||
[(k2)/m2, -(k2+k3)/m2, c2/m2, -(c2+c3)/m2] | ||
]) | ||
B = np.array([[0.,0.],[0.,0.],[1/m1,0.],[0.,1/m2]]) | ||
C = np.array([[1.0, 0.0, 0.0, 0.0],[0.0, 1.0, 0.0, 0.0]]) | ||
D = np.zeros((2,2)) | ||
|
||
|
||
xixo_list = ["SISO","SIMO","MISO","MIMO"] | ||
xixo = xixo_list[3] # choose a system for estimation | ||
match xixo: | ||
case "SISO": | ||
sys = ct.StateSpace(A, B[:,0], C[0,:], D[0,0]) | ||
case "SIMO": | ||
sys = ct.StateSpace(A, B[:,:1], C, D[:,:1]) | ||
case "MISO": | ||
sys = ct.StateSpace(A, B, C[:1,:], D[:1,:]) | ||
case "MIMO": | ||
sys = ct.StateSpace(A, B, C, D) | ||
|
||
dt = 0.1 | ||
sysd = sys.sample(dt, method='zoh') | ||
sysd.name = "H_true" | ||
|
||
# random forcing input | ||
t = np.arange(0,100,dt) | ||
u = np.random.randn(sysd.B.shape[-1], len(t)) | ||
|
||
response = ct.forced_response(sysd, U=u) | ||
response.plot() | ||
plt.show() | ||
|
||
m = 100 | ||
ir_true = ct.impulse_response(sysd, T=t[:m+1]) | ||
H_true = ir_true.outputs | ||
|
||
|
||
H_est = ct.okid(response, m, dt=dt) | ||
# helper function for plotting only | ||
ir_est = create_impulse_response(H_est, | ||
ir_true.time, | ||
ir_true.transpose, | ||
dt) | ||
|
||
ir_true.plot(title=xixo) | ||
ir_est.plot(color='orange',linestyle='dashed') | ||
plt.show() | ||
|
||
if 'PYCONTROL_TEST_EXAMPLES' not in os.environ: | ||
plt.show() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add a "See Also" section and reference
markov
? Perhaps also a note here (and inmarkov
) about how this differs from themarkov
command?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would that be enough?
See Also
markov
Notes
The :func:
~control.markov
command estimates the Markov parameters directly, which can be hard for slightly damped systems.The :func:
~control.observer_kalman_identification
command uses a Kalman filter, which is better suited for slightly damped systems.