## Copyright (C) 2016 Geraint Paul Bevan .
##
#######################################################################
##
## k = dcgain(sys)
##
## Compute the DC gain k of a linear time invariant (LTI) system
##
## Transfer function for a continuous state space system (A,B,C,D)
## G(s) = C * inv(s*I - A) * B + D
##
## DC Gain: evaluate G(s) as s -> 0
## k = C * inv(-A) * B + D
##
## Transfer function for a discrete state space system (A,B,C,D,T)
## G(z) = C * inv(z*I - A) * B + D
##
## DC Gain: evaluate G(z) as z -> 1
## k = C * inv(I-A) * B + D
##
## -*- texinfo -*-
## @deftypefn {Function File} {@var{sys} =} dcgain (@var{sys})
## Compute the dcgain of a linear time invariant (LTI) system.
##
## @strong{Inputs}
## @table @var
## @item sys
## A LTI model created by tf(), ss(), dss(), etc.
## @end table
##
## @strong{Outputs}
## @table @var
## @item k
## DC Gain of the LTI model
## @end table
##
## @strong{Example}
## @example
## G = Transfer function 'G' from input 'u1' to output ...
##
## 1
## y1: ---------------------
## s^3 + 2 s^2 + 3 s + 4
##
##
## octave:1> K = dcgain(G)
##
## K = 0.25000
## @end example
##
## @seealso{tf,ss,dss}
## Author: Geraint Paul Bevan
## Created: December 2016
## Version: 0.1
function k = dcgain(sys)
if (! isa(sys,"lti"))
error("dcgain: argument must be an lti system");
endif
tsam = get(sys,'tsam');
if (0 == tsam) # Continuous system
k = sys.c*inv(-sys.a)*sys.b+sys.d
else # Discrete system
k = sys.c*inv(eye(size(sys.a))-sys.a)*sys.b+sys.d
end
endfunction
%!assert( dcgain( tf(1,[1,1]) ) , 1 )
%!assert( dcgain( tf(2,[1,1]) ) , 2 )
%!assert( dcgain( ss([0,1;-2,-3],[0;1],[1,0],0) ) , 0.5 )