-
Notifications
You must be signed in to change notification settings - Fork 10
Added basic implementation of compreal for state space realization #26
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
Open
JD123473
wants to merge
2
commits into
gnu-octave:main
Choose a base branch
from
JD123473:main
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.
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| ## Copyright (C) 2026 J. Román <jdaniel.roman2004@gmail.com> | ||
| ## | ||
| ## This file is part of the control package for GNU Octave. | ||
| ## | ||
| ## Octave is free software; you can redistribute it and/or modify it | ||
| ## under the terms of the GNU General Public License as published by | ||
| ## the Free Software Foundation; either version 3 of the License, or | ||
| ## (at your option) any later version. | ||
| ## | ||
| ## Octave is distributed in the hope that it will be useful, but | ||
| ## WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| ## GNU General Public License for more details. | ||
| ## | ||
| ## You should have received a copy of the GNU General Public License | ||
| ## along with Octave; see the file COPYING. If not, | ||
| ## see <http://www.gnu.org/licenses/>. | ||
|
|
||
| ## -*- texinfo -*- | ||
| ## @deftypefn {Function File} {[@var{csys}, @var{T}] =} compreal (@var{sys}, @var{realization}) | ||
| ## Returns the state-space companion controllable and observable form of the input | ||
| ## system as well as the transformation matrix. | ||
| ## | ||
| ## Given the state vector | ||
| ## @tex | ||
| ## $$x(t)$$ | ||
| ## @end tex | ||
| ## @ifnottex | ||
| ## @example | ||
| ## @group | ||
| ## x(t) | ||
| ## @end group | ||
| ## @end example | ||
| ## @end ifnottex | ||
| ## | ||
| ## We look for a transformation matrix such that | ||
| ## | ||
| ## @tex | ||
| ## $$ x(t)=Tz(t) $$ | ||
| ## @end tex | ||
| ## @ifnottex | ||
| ## @example | ||
| ## @group | ||
| ## x(t)=Tz(t) | ||
| ## @end group | ||
| ## @end example | ||
| ## @end ifnottex | ||
| ## | ||
| ## @tex | ||
| ## Where $T$ is our transformation matrix and $z(t)$ is our new state vector. | ||
| ## @end tex | ||
| ## @ifnottex | ||
| ## @group | ||
| ## Where T is our transformation matrix and $z(t)$ is our new state vector. | ||
| ## @end group | ||
| ## @end ifnottex | ||
| ## | ||
| ## Our new state-space is given by the following relationships: | ||
| ## | ||
| ## @tex | ||
| ## $$ A_z = T^{-1}AT $$ | ||
| ## $$ B_z = T^{-1}B $$ | ||
| ## $$ C_z = CT $$ | ||
| ## $$ D_z = D $$ | ||
| ## @end tex | ||
| ## @ifnottex | ||
| ## @example | ||
| ## @group | ||
| ## A_z = inv(T)AT | ||
| ## B_z = inv(T)B | ||
| ## C_z = CT | ||
| ## D_z = D | ||
| ## @end group | ||
| ## @end example | ||
| ## @end ifnottex | ||
| ## | ||
| ## The @var{realization} argument selects the companion form: | ||
| ## | ||
| ## @table @code | ||
| ## @item 'c' | ||
| ## Controllable companion form. | ||
| ## | ||
| ## @item 'o' | ||
| ## Observable companion form. | ||
| ## @end table | ||
| ## | ||
| ## @end deftypefn | ||
|
|
||
| ## Based on equation (15a) and (15b) of: | ||
| ## T. Kailath, Linear systems. Prentice-Hall Englewood Cliffs, NJ, 1980. | ||
| ## ISBM 0-13-536961-4 | ||
|
|
||
| ## Author: J. Román <jdaniel.roman2004@gmail.com> | ||
| ## Created: May 2026 | ||
|
|
||
| function [csys, T] = compreal (sys, realization) | ||
|
|
||
| ## Validation | ||
|
|
||
| if (nargin < 1 || nargin > 2) | ||
| print_usage (); | ||
| endif | ||
|
|
||
| if (issiso (sys) == 0) | ||
| error ("compreal: system must be SISO"); | ||
| endif | ||
|
|
||
| if (nargin == 1 || strcmp (realization, "c")) | ||
| realization = "controllable"; | ||
| elseif (strcmp (realization, "o")) | ||
| realization = "observable"; | ||
| else | ||
| error ("compreal: realization must be 'c' or 'o'"); | ||
| endif | ||
|
|
||
|
|
||
|
|
||
| ## Matrix T | ||
|
|
||
| sys = ss (sys); | ||
|
|
||
| switch (realization) | ||
|
|
||
| case "controllable" | ||
|
|
||
| ctrmatrix = ctrb (sys.A, sys.B); | ||
|
|
||
| if (rank (ctrmatrix) < rows (ctrmatrix)) | ||
|
|
||
| error ("compreal: system is not controllable"); | ||
|
|
||
| endif | ||
|
|
||
| T = ctrmatrix; | ||
|
|
||
| case "observable" | ||
|
|
||
| obsmatrix = obsv (sys.A, sys.C); | ||
|
|
||
| if (rank (obsmatrix) < rows (obsmatrix)) | ||
|
|
||
| error ("compreal: system is not observable"); | ||
|
|
||
| endif | ||
|
|
||
| T = inv (obsmatrix); | ||
|
|
||
| endswitch | ||
|
|
||
| ## Transformation | ||
|
|
||
| A = T \ sys.A * T; | ||
|
|
||
| B = T \ sys.B; | ||
|
|
||
| C = sys.C * T; | ||
|
|
||
| D = sys.D; | ||
|
|
||
| csys = ss (A, B, C, D); | ||
|
|
||
| endfunction | ||
|
|
||
| %!shared sys, A, B, C, D | ||
| %! A = [0 1; -2 -3]; | ||
| %! B = [0; 1]; | ||
| %! C = [1 0]; | ||
| %! D = 0; | ||
| %! sys = ss (A, B, C, D); | ||
|
|
||
| %!test | ||
| %! [csys, T] = compreal (sys, 'c'); | ||
| %! assert (tfdata(tf(csys)), tfdata(tf(sys)), 1e-6); | ||
|
|
||
| %!test | ||
| %! [csys, T] = compreal (sys, 'c'); | ||
| %! assert (T, ctrb (A, B), 1e-10); | ||
|
|
||
| %!test | ||
| %! [csys, T] = compreal (sys, 'c'); | ||
| %! assert (csys.A, inv (T) * A * T, 1e-10); | ||
|
|
||
| %!test | ||
| %! [csys, T] = compreal (sys, 'o'); | ||
| %! assert (tfdata (tf (csys)), tfdata (tf (sys)), 1e-6); | ||
|
|
||
| %!error <realization must be> | ||
| %! compreal (sys, 'x'); | ||
|
|
||
| %!error <system must be SISO> | ||
| %! sysmimo = ss (A, B, eye(2), zeros(2,1)); | ||
| %! compreal (sysmimo); | ||
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.
Please check the parameters before using them, otherwise the error message might be misleadig for the user. As far as I can see:
'c'if it is omitted