Code covered by the BSD License  

Highlights from
Numerical Computing with Simulink, Vol. 1

image thumbnail
from Numerical Computing with Simulink, Vol. 1 by Richard Gran
This sequel to Numerical Computing with MATLAB explores the mathematics of simulation.

c2d_ncs(a, b, deltat)
function [Phi, Gamma] = c2d_ncs(a, b, deltat)
%C2D_NCS	Converts a state space model in continuous time
%   to a state space discrete time equivalent model.
%
%   In MATLAB you use the function as follows: 
%	[Phi, Gamma] = ced_ncs(A,B,deltat) 
%
%   The output is the discrete equivalent of the continuous-time 
%   state-spacemodel:
%           .
%		    x = Ax + Bu
%
%	i.e. the outputs are the matrices in the model:
%
%		x    = Phi(deltat) * x + Gamma(deltat) u
%        k+1                  k                 k
%
%	where the assumption is made that the input is held constant over the
%	sample time deltat.

[ma,na]  = size(a);
[mb,nb]  = size(b);
if ma~=na
    error('The matrix a must be square')
end
if mb~=ma 
    error('The matrix b must have the same number of rows as the matrix a')
end

%  Have you done the exercise that asks you to verify the following?
augmented_matrix = [[a b];zeros(nb,na+nb)];
exp_aug          = expm(augmented_matrix*deltat);
Phi              = exp_aug( 1:na, 1:na );
Gamma            = exp_aug( 1:na, na+1:na+nb );

%  Do you understand this code?

Contact us at files@mathworks.com