Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 13 additions & 1 deletion lib/ControlSystemsBase/src/discrete.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@
elseif method === :fwdeuler
Ad, Bd, Cd, Dd = (I+Ts*A), Ts*B, C, D
x0map = I(nx)
elseif method === :bwdeuler
Ad = inv(I - Ts*A)
TsB = Ts*B
Bd = (Ad * TsB)
Cd = C*Ad
Dd = Cd * TsB + D
x0map = I(nx)
elseif method === :tustin
a = w_prewarp == 0 ? Ts/2 : tan(w_prewarp*Ts/2)/w_prewarp
a > 0 || throw(DomainError("A positive w_prewarp must be provided for method Tustin"))
Expand All @@ -77,7 +84,7 @@
Dd = a*Cd*B + D
x0map = Matrix{T}(I, nx, nx)
elseif method === :matched
error("NotImplemented: Only `:zoh`, `:foh`, :tustin and `:fwdeuler` implemented so far")
error("NotImplemented: Only `:zoh`, `:foh`, :tustin, `:fwdeuler` and `bwdeuler` implemented so far")

Check warning on line 87 in lib/ControlSystemsBase/src/discrete.jl

View check run for this annotation

Codecov / codecov/patch

lib/ControlSystemsBase/src/discrete.jl#L87

Added line #L87 was not covered by tests
else
error("Unsupported method: ", method)
end
Expand Down Expand Up @@ -111,6 +118,11 @@
Ac = (A-I)./sys.Ts
Bc = B./sys.Ts
Cc, Dc = C, D
elseif method === :bwdeuler
Ac = (I - inv(A))/sys.Ts
Bc = (A\B) ./ sys.Ts
Cc = C/A
Dc = D - sys.Ts*C*Bc
elseif method === :tustin
a = w_prewarp == 0 ? sys.Ts/2 : tan(w_prewarp*sys.Ts/2)/w_prewarp
a > 0 || throw(DomainError("A positive w_prewarp must be provided for method Tustin"))
Expand Down
4 changes: 2 additions & 2 deletions lib/ControlSystemsBase/test/test_discrete.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ sysd = c2d(sys, 1)
@test d2c(sysd) ≈ sys


# forward euler / tustin
# forward euler / tustin / backward euler
@test c2d(C_111, 1, :fwdeuler).A == I + C_111.A
for method in (:fwdeuler, :tustin)
for method in (:fwdeuler, :tustin, :bwdeuler)
@test d2c(c2d(C_111, 0.01, method), method) ≈ C_111 atol = sqrt(eps())
@test d2c(c2d(C_212, 0.01, method), method) ≈ C_212 atol = sqrt(eps())
@test d2c(c2d(C_221, 0.01, method), method) ≈ C_221 atol = sqrt(eps())
Expand Down
Loading