function [f,g] = additgr(f0,g0)
% Adds integrator to state-space model
%
% [F,G] = ADDITGR(F0,G0) adds an integrator at the input of a state-space
% model

% Dimension of state-space model
n = size(f0,1);

M = n+1;

x = sym(zeros(M,1));
for k=1:min(9,M)
    x(k) = sym(['x0' num2str(k)]);
end;

% M<100
for k=10:M
    x(k) = sym(['x' num2str(k)]);
end;


f = sym(zeros(M,1));
g = sym(zeros(M,1));

% Integrator
g(1) = 1;

% Shift the original system
for k=n:-1:1
    f0 = subs(f0,x(k),x(k+1));
    g0 = subs(g0,x(k),x(k+1));
end;    
f0 = f0 + g0*x(1);
f(2:end) = f0;


