Skip to content

Commit

Permalink
Merge fcddc1d into 17c5867
Browse files Browse the repository at this point in the history
  • Loading branch information
baggepinnen committed Sep 23, 2020
2 parents 17c5867 + fcddc1d commit 5cf2dec
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/timeresp.jl
Expand Up @@ -143,9 +143,10 @@ function lsim(sys::StateSpace, u::Function, t::AbstractVector;
x0::VecOrMat=zeros(sys.nx), method::Symbol=:cont)
ny, nu = size(sys)
nx = sys.nx
u0 = u(x0,1)
if length(x0) != nx
error("size(x0) must match the number of states of sys")
elseif size(u(x0,1)) != (nu,) && size(u(x0,1)) != (nu,1)
elseif !(u0 isa Number && nu == 1) && (size(u0) != (nu,) && size(u0) != (nu,1))
error("return value of u must be of size nu")
end
T = promote_type(Float64, eltype(x0))
Expand Down Expand Up @@ -207,7 +208,7 @@ function ltitr(A::AbstractMatrix{T}, B::AbstractMatrix{T}, u::Function, t,

for i=1:iters
x[:,i] = x0
uout[:,i] = u(x0,t[i])
uout[:,i] .= u(x0,t[i])
x0 = A * x0 + B * uout[:,i]
end
return transpose(x), transpose(uout)
Expand Down
7 changes: 6 additions & 1 deletion test/test_timeresp.jl
Expand Up @@ -33,10 +33,15 @@ sysd = c2d(sys, 0.1)[1]
sysdfb = ss(sysd.A-sysd.B*L, sysd.B, sysd.C, sysd.D, 0.1)
#Simulate without input
yd, td, xd = lsim(sysdfb, zeros(501), t, x0=x0)

@test y yd
@test x xd

# Test that the discrete lsim accepts u function that returns scalar
L = lqr(sysd,Q,R)
u(x,i) = -L*x
yd, td, xd = lsim(sysd, u, t, x0=x0)
@test norm(y - yd)/norm(y) < 0.05 # Since the cost matrices are not discretized, these will differ a bit
@test norm(x - xd)/norm(x) < 0.05

#Test step and impulse Continuous
t0 = 0:0.05:2
Expand Down

0 comments on commit 5cf2dec

Please sign in to comment.