79 lines
2.0 KiB

  1. % === Problem 3c: Effect of Input Amplitude A0 on Estimation Accuracy ===
  2. clear; close all;
  3. % True system parameters
  4. m = 0.75;
  5. L = 1.25;
  6. c = 0.15;
  7. g = 9.81;
  8. mL2_true = m * L^2;
  9. mgL_true = m * g * L;
  10. theta_true = [mL2_true; c; mgL_true];
  11. % Simulation settings
  12. omega = 2; % input frequency
  13. Ts = 0.1; % sampling period
  14. dt = 1e-4; % integration resolution
  15. T_final = 20; % simulation time
  16. % Amplitudes to test
  17. A0_list = [1, 2, 4, 6, 8, 16];
  18. n_cases = length(A0_list);
  19. rel_errors_all = zeros(3, n_cases);
  20. for i = 1:n_cases
  21. A0 = A0_list(i);
  22. % Simulate system
  23. t_full = 0:dt:T_final;
  24. odefun = @(t, x) [
  25. x(2);
  26. (1/(m*L^2)) * (A0*sin(omega*t) - c*x(2) - m*g*L*x(1))
  27. ];
  28. x0 = [0; 0];
  29. [t_sim, x_sim] = ode45(odefun, t_full, x0);
  30. % Resample at Ts
  31. t_sampled = t_sim(1):Ts:t_sim(end);
  32. q = interp1(t_sim, x_sim(:,1), t_sampled);
  33. u = A0 * sin(omega * t_sampled);
  34. N = length(t_sampled);
  35. % Estimate derivatives
  36. dq = zeros(N,1);
  37. ddq = zeros(N,1);
  38. for k = 2:N-1
  39. dq(k) = (q(k+1) - q(k-1)) / (2*Ts);
  40. ddq(k) = (q(k+1) - 2*q(k) + q(k-1)) / Ts^2;
  41. end
  42. % LS Estimation
  43. idx = 2:N-1;
  44. X = [ddq(idx), dq(idx), q(idx)'];
  45. y = u(idx).';
  46. theta_hat = (X' * X) \ (X' * y);
  47. rel_error = abs((theta_hat - theta_true) ./ theta_true) * 100;
  48. rel_errors_all(:, i) = rel_error;
  49. % Print
  50. fprintf('A0 = %d → mL^2=%.4f (%.2f%%), c=%.4f (%.2f%%), mgL=%.4f (%.2f%%)\n', ...
  51. A0, theta_hat(1), rel_error(1), ...
  52. theta_hat(2), rel_error(2), ...
  53. theta_hat(3), rel_error(3));
  54. end
  55. % === Plot ===
  56. figure('Name', 'Problem 3c - Effect of A0', 'Position', [100, 100, 1000, 600]);
  57. plot(A0_list, rel_errors_all', '-o', 'LineWidth', 2, 'MarkerSize', 4);
  58. legend({'mL^2', 'c', 'mgL'}, 'Location', 'northeast');
  59. xlabel('Input Amplitude A_0');
  60. ylabel('Relative Error [%]');
  61. title('Effect of Input Amplitude on Parameter Estimation');
  62. grid on;
  63. ylim([0 1.1]);
  64. saveas(gcf, 'output/Prob3c_AmplitudeEffect.png');