% Linear Systems, Homework 2
% Date: 11/13/06
% File: HW2_1b.m
% Name: Feng-Li Lian, fengli@ntu.edu.tw

close all; clear all;

%syms R1 R2 L C s t tau

% 1a

R1 = 2;
R2 = 10;
L  = 0.5;
C  = 1;

Ap = [ -R1/L -1/L; 
       1/C -1/(C*R2)];

Bp = [ 1/L ; 0 ];
Cp = [ 0 1 ];
Dp = 0;


eig(Ap)


Sysp = ss(Ap,Bp,Cp,Dp);

[Nump, Denp] = ss2tf( Ap, Bp, Cp, Dp );


% state-space solution

% mehtod 1: by direct calculation

syms t tau

xp0 = zeros(2,1);
u = 1;
%u  = sin(0.5*pi*tau);

expAp = expm( Ap * t );

zi = expAp * xp0

zs = int( expm( Ap*(t-tau) ) * Bp * u, tau, 0, t )

xpt = zi + zs

subs( xpt, t, 1 )

for i=1:101
    time1(i) = (i-1)*0.1;
    state1(:,i) = subs( xpt, t, time1(i) );
    u1 = subs( u, tau, time1(i) );
    output1(i) = Cp*state1(:,i) + Dp*u1;
end

figure(11)
subplot(211)
plot(time1,state1)
xlabel('time'); ylabel('state');
subplot(212)
plot(time1,output1,'g')
xlabel('time'); ylabel('output');




% mehtod 2: by step function

[output2,time2,state2] = step(Sysp,10);

figure(21)
subplot(211)
plot(time2,state2)
xlabel('time'); ylabel('state');
subplot(212)
plot(time2,output2,'g')
xlabel('time'); ylabel('output');



% mehtod 3: by ode45 solver
% see the other set of files

