% === Problem 1: Simulation of the responce ===

% Parameters
m = 0.75;
L = 1.25;
c = 0.15;
g = 9.81;
A0 = 4;
omega = 2;

% Time span for 20s simulation
tspan = [0 20];
dt = 1e-4;
t_eval = 0:dt:20;

% ODE Function
odefun = @(t, x) [
    x(2);
    (1/(m*L^2)) * (A0*sin(omega*t) - c*x(2) - m*g*L*x(1))
];

x0 = [0; 0];                        % Initial conditions
[t, x] = ode45(odefun, t_eval, x0); % Solve

% Plots for 20 sec
figure('Name', 'System responce', 'Position', [100, 100, 1280, 860]);
subplot(2,1,1);
plot(t, x(:,1)); ylabel('q(t) [rad]'); grid on; title('Γωνία');
subplot(2,1,2);
plot(t, x(:,2), 'r'); ylabel('dq(t) [rad/s]'); xlabel('t [sec]'); grid on; title('Γωνιακή Ταχύτητα');
saveas(gcf, 'output/Prob1_responce_20s.png');

% === Sampling for Problem 2 ===
Ts = 0.1;                          % Sampling period
sample_data(t, x, Ts, A0, omega, 'output/problem1_data.csv');

% --- Extended simulation to 90 sec ---
tspan = [0 90];
t_eval = 0:dt:90;

x0 = [0; 0];
[t, x] = ode45(odefun, t_eval, x0);

% Plots for 90 sec
figure('Name', 'System responce', 'Position', [100, 100, 1280, 860]);
subplot(2,1,1);
plot(t, x(:,1)); ylabel('q(t) [rad]'); grid on; title('Γωνία');
subplot(2,1,2);
plot(t, x(:,2), 'r'); ylabel('dq(t) [rad/s]'); xlabel('t [sec]'); grid on; title('Γωνιακή Ταχύτητα');
saveas(gcf, 'output/Prob1_responce_90s.png');
