clear;
%1d
L = 4.7e-3;
Cf = 100e-6;
Rl = inf;
% Rl = 100; %Task 2d)

A = [0,-1/L;
     1/Cf, -1/(Rl*Cf)];
B = [1/L;
    0];
C = [0,1];
D = 0;

%1e
sys_ol = ss(A,B,C,D);
Tfinal = 0.05;
step(sys_ol,Tfinal);

%2b
Q = [0.001, 0;
    0 1];
R = 1;

K = lqr(A,B,Q,R,[]);

%2c
%calculate Nu and Nx to simplify expressions
Nu = -(C*A^-1*B)^-1;
Nx = A^-1*B*(C*A^-1*B)^-1;

N = Nu+K*Nx;
Acl = A-B*K;
Bcl = B*N;

%2d
Ccl = [ 1 0; 
        0 1; 
        -K];
Dcl = [ 0;
        0;
        N];

sys_cl = ss(Acl,Bcl,Ccl,Dcl);
sys_cl.OutputName = {'i_L','v_C','u'};
step(sys_cl,0.05)
