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.
 
 
 

105 lines
2.7 KiB

  1. %
  2. % Problem 2b: Estimation of unknown parameters using Lyapunov (r, dr, u measurable)
  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. % Initial conditions
  28. r = zeros(1, N);
  29. dr = zeros(1, N);
  30. ddr = zeros(1, N);
  31. % Estimated trajectory reconstruction
  32. r_hat = zeros(1, N);
  33. dr_hat = zeros(1, N);
  34. dr_hat(1) = 0; r_hat(1) = 0;
  35. % Parameter estimation setup
  36. theta_hat = zeros(4, N);
  37. theta_hat(:,1) = [1; 1; 1; 1];
  38. gamma = 0.66;
  39. alpha = zeros(1, N);
  40. u = zeros(1, N);
  41. for k = 1:N-1
  42. % Control law
  43. z1 = (r(k) - r_d(k)) / phi(k);
  44. z1 = max(min(z1, 0.999), -0.999);
  45. alpha(k) = -k1 * log((1 + z1) / (1 - z1));
  46. z2 = (dr(k) - alpha(k)) / rho;
  47. z2 = max(min(z2, 0.999), -0.999);
  48. u(k) = -k2 * log((1 + z2) / (1 - z2));
  49. % System dynamics
  50. phi_vec = [-dr(k); -sin(r(k)); dr(k)^2 * sin(2*r(k)); u(k)];
  51. ddr(k) = a1 * phi_vec(1) + a2 * phi_vec(2) + a3 * phi_vec(3) + b * phi_vec(4);
  52. dr(k+1) = dr(k) + Ts * ddr(k);
  53. r(k+1) = r(k) + Ts * dr(k);
  54. % Parameter estimation
  55. y = ddr(k);
  56. y_hat = theta_hat(:,k)' * phi_vec;
  57. e = y - y_hat;
  58. theta_hat(:,k+1) = theta_hat(:,k) + Ts * gamma * e * phi_vec;
  59. % Reconstruct r_hat from estimated theta
  60. phi_hat = [-dr_hat(k); -sin(r_hat(k)); dr_hat(k)^2 * sin(2 * r_hat(k)); u(k)];
  61. dd_r_hat = theta_hat(:,k)' * phi_hat;
  62. dr_hat(k+1) = dr_hat(k) + Ts * dd_r_hat;
  63. r_hat(k+1) = r_hat(k) + Ts * dr_hat(k);
  64. end
  65. fprintf('\n2b: Final estimated parameters:\n');
  66. 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));
  67. % Plot
  68. figure('Name', 'Problem 2b - Estimation with State Comparison', 'Position', [100, 100, 1280, 860]);
  69. sgtitle('Problem 2b - Parameter Estimation and State Reconstruction');
  70. subplot(3,1,1);
  71. plot(t, theta_hat', 'LineWidth', 1.4);
  72. legend('a_1', 'a_2', 'a_3', 'b');
  73. ylabel('\theta estimates'); grid on; title('Εκτιμήσεις παραμέτρων');
  74. subplot(3,1,2);
  75. plot(t, r, 'b', t, r_hat, '--r', 'LineWidth', 1.4);
  76. legend('r(t)', 'r_{hat}(t)');
  77. ylabel('Roll angle [rad]'); title('Πραγματικό vs εκτιμώμενο r(t)'); grid on;
  78. subplot(3,1,3);
  79. plot(t, r - r_hat, 'k', 'LineWidth', 1.4);
  80. ylabel('e_r(t)'); xlabel('Time [s]'); title('Σφάλμα θέσης: r(t) - r̂(t)'); grid on;
  81. if ~exist('output', 'dir')
  82. mkdir('output');
  83. end
  84. saveas(gcf, 'output/Problem2b_estimation.png');