51 行
1.4 KiB

  1. % === Problem 1: Simulation of the responce ===
  2. % Parameters
  3. m = 0.75;
  4. L = 1.25;
  5. c = 0.15;
  6. g = 9.81;
  7. A0 = 4;
  8. omega = 2;
  9. % Time span for 20s simulation
  10. tspan = [0 20];
  11. dt = 1e-4;
  12. t_eval = 0:dt:20;
  13. % ODE Function
  14. odefun = @(t, x) [
  15. x(2);
  16. (1/(m*L^2)) * (A0*sin(omega*t) - c*x(2) - m*g*L*x(1))
  17. ];
  18. x0 = [0; 0]; % Initial conditions
  19. [t, x] = ode45(odefun, t_eval, x0); % Solve
  20. % Plots for 20 sec
  21. figure('Name', 'System responce', 'Position', [100, 100, 1280, 860]);
  22. subplot(2,1,1);
  23. plot(t, x(:,1)); ylabel('q(t) [rad]'); grid on; title('Γωνία');
  24. subplot(2,1,2);
  25. plot(t, x(:,2), 'r'); ylabel('dq(t) [rad/s]'); xlabel('t [sec]'); grid on; title('Γωνιακή Ταχύτητα');
  26. saveas(gcf, 'output/Prob1_responce_20s.png');
  27. % === Sampling for Problem 2 ===
  28. Ts = 0.1; % Sampling period
  29. sample_data(t, x, Ts, A0, omega, 'output/problem1_data.csv');
  30. % --- Extended simulation to 90 sec ---
  31. tspan = [0 90];
  32. t_eval = 0:dt:90;
  33. x0 = [0; 0];
  34. [t, x] = ode45(odefun, t_eval, x0);
  35. % Plots for 90 sec
  36. figure('Name', 'System responce', 'Position', [100, 100, 1280, 860]);
  37. subplot(2,1,1);
  38. plot(t, x(:,1)); ylabel('q(t) [rad]'); grid on; title('Γωνία');
  39. subplot(2,1,2);
  40. plot(t, x(:,2), 'r'); ylabel('dq(t) [rad/s]'); xlabel('t [sec]'); grid on; title('Γωνιακή Ταχύτητα');
  41. saveas(gcf, 'output/Prob1_responce_90s.png');