You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

108 lines
2.7 KiB

  1. %
  2. % Problem 2c: Estimation under external disturbance d(t)
  3. %
  4. clear
  5. % True system parameters
  6. a1 = 2.0;
  7. a2 = 1.0;
  8. a3 = 0.5;
  9. b = 2.0;
  10. % Simulation setup
  11. Ts = 0.001;
  12. T_total = 30;
  13. t = 0:Ts:T_total;
  14. N = length(t);
  15. % Reference trajectory
  16. r_d = zeros(1, N);
  17. r_d(t >= 10 & t < 20) = pi/10;
  18. % Smooth bound phi(t)
  19. phi0 = 1.5;
  20. phi_inf = 0.05;
  21. lambda = 0.5;
  22. phi = (phi0 - phi_inf) * exp(-lambda * t) + phi_inf;
  23. % Control parameters
  24. k1 = 1.0;
  25. k2 = 1.0;
  26. rho = 1.0;
  27. % External disturbance
  28. d = 0.15 * sin(0.5 * t);
  29. % Initial conditions
  30. r = zeros(1, N);
  31. dr = zeros(1, N);
  32. ddr = zeros(1, N);
  33. % Estimated trajectory reconstruction
  34. r_hat = zeros(1, N);
  35. dr_hat = zeros(1, N);
  36. dr_hat(1) = 0; r_hat(1) = 0;
  37. % Parameter estimation setup
  38. theta_hat = zeros(4, N);
  39. theta_hat(:,1) = [1; 1; 1; 1];
  40. gamma = 0.66;
  41. alpha = zeros(1, N);
  42. u = zeros(1, N);
  43. for k = 1:N-1
  44. % Control law
  45. z1 = (r(k) - r_d(k)) / phi(k);
  46. z1 = max(min(z1, 0.999), -0.999);
  47. alpha(k) = -k1 * log((1 + z1) / (1 - z1));
  48. z2 = (dr(k) - alpha(k)) / rho;
  49. z2 = max(min(z2, 0.999), -0.999);
  50. u(k) = -k2 * log((1 + z2) / (1 - z2));
  51. % System dynamics (with disturbance)
  52. phi_vec = [-dr(k); -sin(r(k)); dr(k)^2 * sin(2*r(k)); u(k)];
  53. ddr(k) = a1 * phi_vec(1) + a2 * phi_vec(2) + a3 * phi_vec(3) + b * phi_vec(4) + d(k);
  54. dr(k+1) = dr(k) + Ts * ddr(k);
  55. r(k+1) = r(k) + Ts * dr(k);
  56. % Parameter estimation (disturbance unmodeled)
  57. y = ddr(k);
  58. y_hat = theta_hat(:,k)' * phi_vec;
  59. e = y - y_hat;
  60. theta_hat(:,k+1) = theta_hat(:,k) + Ts * gamma * e * phi_vec;
  61. % Reconstruct r_hat from estimated theta
  62. phi_hat = [-dr_hat(k); -sin(r_hat(k)); dr_hat(k)^2 * sin(2 * r_hat(k)); u(k)];
  63. dd_r_hat = theta_hat(:,k)' * phi_hat;
  64. dr_hat(k+1) = dr_hat(k) + Ts * dd_r_hat;
  65. r_hat(k+1) = r_hat(k) + Ts * dr_hat(k);
  66. end
  67. fprintf('\n2c: Final estimated parameters (with disturbance):\n');
  68. fprintf('a1: %.4f, a2: %.4f, a3: %.4f, b: %.4f\n', theta_hat(1,end), theta_hat(2,end), theta_hat(3,end), theta_hat(4,end));
  69. % Plot
  70. figure('Name', 'Problem 2c - Estimation under Disturbance', 'Position', [100, 100, 1280, 860]);
  71. sgtitle('Problem 2c - Estimation under External Disturbance');
  72. subplot(3,1,1);
  73. plot(t, theta_hat', 'LineWidth', 1.4);
  74. legend('a_1', 'a_2', 'a_3', 'b');
  75. ylabel('\theta estimates'); grid on; title('Εκτιμήσεις παραμέτρων');
  76. subplot(3,1,2);
  77. plot(t, r, 'b', t, r_hat, '--r', 'LineWidth', 1.4);
  78. legend('r(t)', 'r_{hat}(t)');
  79. ylabel('Roll angle [rad]'); title('Πραγματικό vs εκτιμώμενο r(t)'); grid on;
  80. subplot(3,1,3);
  81. plot(t, r - r_hat, 'k', 'LineWidth', 1.4);
  82. ylabel('e_r(t)'); xlabel('Time [s]'); title('Σφάλμα θέσης: r(t) - r̂(t)'); grid on;
  83. if ~exist('output', 'dir')
  84. mkdir('output');
  85. end
  86. saveas(gcf, 'output/Problem2c_estimation.png');