% Create a 2nd-order linear system:
% d2x + .6*dx + x = u(t)
format compact
fprintf('Look at the details of this m-file on how to simulate response to initial conditions.\n')
A = [0 1; -1 -.6];
B = [0; 1];
C = [1 0];
D = [0];

ss1 = ss(A,B,C,D);
x0 = [3;2];  % Initial position and velocity, at t=0
figure(1); clf
initial(ss1,x0,12); hold on; grid on
tout = [1:10];
xout = zeros(2,length(tout)); % pre-allocate, to be solved below...
plot(0,x0(1),'ko','MarkerFaceColor','g')
plot(0+[0 .2],x0(1) + [0 .2]*x0(2),'g-','LineWidth',2)
for n=1:length(tout)
    input('Hit enter to continue...')
    figure(1)
    tnow = tout(n);
    S = 0;
    for k=0:100
        % e^(A*t) = sum{ (A*t)^k / (k!) }
        % Below, we generate that approximate "sum", and call it "S":
        % k
        S = S + (A*tnow)^k/(prod(1:k));
    end
    S
    xout(:,n) = S*x0;
    plot(tout(n),xout(1,n),'ko','MarkerFaceColor','r')
    plot(tout(n)+[0 .2],xout(1,n) + [0 .2]*xout(2,n),'r-','LineWidth',2)
    
    
end



