clear;
clc;
pkg load control

%% task 1f)
L = 4.7e-3;     % henry
Cf = 100e-6;    % farad
Rl = inf;       % ohm

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

B = [1/L;
    0];

C = [0,1];
D = 0;

%% task 1g)
sys_ol = ss(A,B,C,D);
Tfinal = 0.1;
step(sys_ol,Tfinal);

%% task 1h)
disp("Determinant of controllability matrix:")
C_con = det([B A*B])

%% task 2a)
disp("\n Poles for the desired behavior:")
pole1 = -100+ 100*2*pi*i
pole2 = -100 - 100*2*pi*i

%% task 2b)
disp("\n K values:");
K = place(A,B,[pole1, pole2])
sys_stable = ss((A-B*K),B,C,D);

%% task 2c)
hold on
step(sys_stable,Tfinal)
hold on;

%%extra: exponential damping
t = linspace(0,Tfinal,100);
plot(t,5.255*(1-exp(-t/10e-3)))
