- Notifications
You must be signed in to change notification settings - Fork441
new dynamics and output functions for statespace and iosystems#558
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
5 commits Select commitHold shift + click to select a range
e99273a
added dynamics and output to statespace and iosystems
sawyerbfuller9678bd1
add remark in docstring for iosys._rhs and _out that they are intende…
sawyerbfullerc0f7d06
fix to possibly fix pytest errors when using matrix
sawyerbfuller64d0dde
another try at fixing failing unit tests
sawyerbfuller23c4b09
fixes/cleanups suggested by @murrayrm
sawyerbfullerFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
68 changes: 65 additions & 3 deletionscontrol/iosys.py
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 |
---|---|---|
@@ -358,26 +358,88 @@ def _update_params(self, params, warning=False): | ||
if (warning): | ||
warn("Parameters passed to InputOutputSystem ignored.") | ||
def _rhs(self, t, x, u, params={}): | ||
"""Evaluate right hand side of a differential or difference equation. | ||
Private function used to compute the right hand side of an | ||
input/output system model. Intended for fast | ||
evaluation; for a more user-friendly interface | ||
you may want to use :meth:`dynamics`. | ||
""" | ||
NotImplemented("Evaluation not implemented for system of type ", | ||
type(self)) | ||
def dynamics(self, t, x, u): | ||
"""Compute the dynamics of a differential or difference equation. | ||
Given time `t`, input `u` and state `x`, returns the value of the | ||
right hand side of the dynamical system. If the system is continuous, | ||
returns the time derivative | ||
dx/dt = f(t, x, u) | ||
where `f` is the system's (possibly nonlinear) dynamics function. | ||
If the system is discrete-time, returns the next value of `x`: | ||
x[t+dt] = f(t, x[t], u[t]) | ||
Where `t` is a scalar. | ||
The inputs `x` and `u` must be of the correct length. | ||
Parameters | ||
---------- | ||
t : float | ||
the time at which to evaluate | ||
x : array_like | ||
current state | ||
u : array_like | ||
input | ||
sawyerbfuller marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
Returns | ||
------- | ||
dx/dt or x[t+dt] : ndarray | ||
""" | ||
return self._rhs(t, x, u) | ||
def _out(self, t, x, u, params={}): | ||
"""Evaluate the output of a system at a given state, input, and time | ||
Private function used to compute the output of of an input/output | ||
system model given the state, input, parameters. Intended for fast | ||
evaluation; for a more user-friendly interface you may want to use | ||
:meth:`output`. | ||
""" | ||
# If no output function was defined in subclass, return state | ||
return x | ||
def output(self, t, x, u): | ||
"""Compute the output of the system | ||
Given time `t`, input `u` and state `x`, returns the output of the | ||
system: | ||
y = g(t, x, u) | ||
The inputs `x` and `u` must be of the correct length. | ||
Parameters | ||
---------- | ||
t : float | ||
the time at which to evaluate | ||
x : array_like | ||
current state | ||
u : array_like | ||
input | ||
sawyerbfuller marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
Returns | ||
------- | ||
y : ndarray | ||
""" | ||
return self._out(t, x, u) | ||
def set_inputs(self, inputs, prefix='u'): | ||
"""Set the number/names of the system inputs. | ||
89 changes: 89 additions & 0 deletionscontrol/statesp.py
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
76 changes: 76 additions & 0 deletionscontrol/tests/statesp_test.py
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 |
---|---|---|
@@ -8,6 +8,7 @@ | ||
""" | ||
import numpy as np | ||
from numpy.testing import assert_array_almost_equal | ||
import pytest | ||
import operator | ||
from numpy.linalg import solve | ||
@@ -47,6 +48,17 @@ def sys322(self, sys322ABCD): | ||
"""3-states square system (2 inputs x 2 outputs)""" | ||
return StateSpace(*sys322ABCD) | ||
@pytest.fixture | ||
def sys121(self): | ||
"""2 state, 1 input, 1 output (siso) system""" | ||
A121 = [[4., 1.], | ||
[2., -3]] | ||
B121 = [[5.], | ||
[-3.]] | ||
C121 = [[2., -4]] | ||
D121 = [[3.]] | ||
return StateSpace(A121, B121, C121, D121) | ||
@pytest.fixture | ||
def sys222(self): | ||
"""2-states square system (2 inputs x 2 outputs)""" | ||
@@ -751,6 +763,70 @@ def test_horner(self, sys322): | ||
np.squeeze(sys322.horner(1.j)), | ||
mag[:, :, 0] * np.exp(1.j * phase[:, :, 0])) | ||
@pytest.mark.parametrize('x', | ||
[[1, 1], [[1], [1]], np.atleast_2d([1,1]).T]) | ||
@pytest.mark.parametrize('u', [0, 1, np.atleast_1d(2)]) | ||
def test_dynamics_and_output_siso(self, x, u, sys121): | ||
assert_array_almost_equal( | ||
sys121.dynamics(0, x, u), | ||
sys121.A.dot(x).reshape((-1,)) + sys121.B.dot(u).reshape((-1,))) | ||
assert_array_almost_equal( | ||
sys121.output(0, x, u), | ||
sys121.C.dot(x).reshape((-1,)) + sys121.D.dot(u).reshape((-1,))) | ||
assert_array_almost_equal( | ||
sys121.dynamics(0, x), | ||
sys121.A.dot(x).reshape((-1,))) | ||
assert_array_almost_equal( | ||
sys121.output(0, x), | ||
sys121.C.dot(x).reshape((-1,))) | ||
sawyerbfuller marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
# too few and too many states and inputs | ||
@pytest.mark.parametrize('x', [0, 1, [], [1, 2, 3], np.atleast_1d(2)]) | ||
def test_error_x_dynamics_and_output_siso(self, x, sys121): | ||
with pytest.raises(ValueError): | ||
sys121.dynamics(0, x) | ||
with pytest.raises(ValueError): | ||
sys121.output(0, x) | ||
@pytest.mark.parametrize('u', [[1, 1], np.atleast_1d((2, 2))]) | ||
def test_error_u_dynamics_output_siso(self, u, sys121): | ||
with pytest.raises(ValueError): | ||
sys121.dynamics(0, 1, u) | ||
with pytest.raises(ValueError): | ||
sys121.output(0, 1, u) | ||
@pytest.mark.parametrize('x', | ||
[[1, 1], [[1], [1]], np.atleast_2d([1,1]).T]) | ||
@pytest.mark.parametrize('u', | ||
[[1, 1], [[1], [1]], np.atleast_2d([1,1]).T]) | ||
def test_dynamics_and_output_mimo(self, x, u, sys222): | ||
assert_array_almost_equal( | ||
sys222.dynamics(0, x, u), | ||
sys222.A.dot(x).reshape((-1,)) + sys222.B.dot(u).reshape((-1,))) | ||
assert_array_almost_equal( | ||
sys222.output(0, x, u), | ||
sys222.C.dot(x).reshape((-1,)) + sys222.D.dot(u).reshape((-1,))) | ||
assert_array_almost_equal( | ||
sys222.dynamics(0, x), | ||
sys222.A.dot(x).reshape((-1,))) | ||
assert_array_almost_equal( | ||
sys222.output(0, x), | ||
sys222.C.dot(x).reshape((-1,))) | ||
# too few and too many states and inputs | ||
@pytest.mark.parametrize('x', [0, 1, [1, 1, 1]]) | ||
def test_error_x_dynamics_mimo(self, x, sys222): | ||
with pytest.raises(ValueError): | ||
sys222.dynamics(0, x) | ||
with pytest.raises(ValueError): | ||
sys222.output(0, x) | ||
@pytest.mark.parametrize('u', [1, [1, 1, 1]]) | ||
def test_error_u_dynamics_mimo(self, u, sys222): | ||
with pytest.raises(ValueError): | ||
sys222.dynamics(0, (1, 1), u) | ||
with pytest.raises(ValueError): | ||
sys222.output(0, (1, 1), u) | ||
class TestRss: | ||
"""These are tests for the proper functionality of statesp.rss.""" | ||
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.