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.
 
 
 

104 lines
2.8 KiB

  1. %
  2. % Problem 1c: Effect of bounded sinusoidal disturbance on measurement x(t)
  3. %
  4. clear
  5. % True system parameters
  6. m_true = 1.315;
  7. b_true = 0.225;
  8. k_true = 0.725;
  9. % Simulation parameters
  10. Ts = 0.001;
  11. T_total = 40;
  12. t_full = 0:Ts:T_total;
  13. % Generate full input signal
  14. u_full = 2.5 * sin(t_full);
  15. % Simulate the true system
  16. x = zeros(1, length(t_full));
  17. dx = zeros(1, length(t_full));
  18. ddx = zeros(1, length(t_full));
  19. x(1) = 0; dx(1) = 0;
  20. for k = 1:length(t_full)-1
  21. f = @(x_, dx_, u_) (1/m_true) * (u_ - b_true * dx_ - k_true * x_);
  22. k1 = f(x(k), dx(k), u_full(k));
  23. k2 = f(x(k) + Ts/2 * dx(k), dx(k) + Ts/2 * k1, u_full(k));
  24. k3 = f(x(k) + Ts/2 * dx(k), dx(k) + Ts/2 * k2, u_full(k));
  25. k4 = f(x(k) + Ts * dx(k), dx(k) + Ts * k3, u_full(k));
  26. ddx(k) = k1;
  27. dx(k+1) = dx(k) + Ts/6 * (k1 + 2*k2 + 2*k3 + k4);
  28. x(k+1) = x(k) + Ts * dx(k);
  29. end
  30. ddx(1:end-1) = diff(dx) / Ts;
  31. ddx(end) = ddx(end-1);
  32. % Initial estimation (clean) using Lyapunov
  33. T_total = 40;
  34. index_limit = round(T_total / Ts);
  35. t = t_full(1:index_limit);
  36. N = length(t);
  37. u = u_full(1:index_limit);
  38. x = x(1:index_limit);
  39. dx = dx(1:index_limit);
  40. ddx = ddx(1:index_limit);
  41. phi_all = [ddx; dx; x];
  42. theta_hat = zeros(3, N);
  43. theta_hat(:, 1) = [1; 1; 1];
  44. gamma = 0.66;
  45. for k = 1:N-1
  46. phi = phi_all(:,k);
  47. y = u(k);
  48. y_hat = theta_hat(:,k)' * phi;
  49. e = y - y_hat;
  50. theta_hat(:,k+1) = theta_hat(:,k) + Ts * gamma * e * phi;
  51. end
  52. % Disturbance settings
  53. eta0 = 0.1;
  54. f0 = 0.5;
  55. eta = eta0 * sin(2 * pi * f0 * t);
  56. x_noisy = x + eta;
  57. % Use clean derivatives, noisy position
  58. phi_all_noise = [ddx; dx; x_noisy];
  59. theta_hat_noise = zeros(3, N);
  60. theta_hat_noise(:, 1) = [1; 1; 1];
  61. for k = 1:N-1
  62. phi = phi_all_noise(:,k);
  63. y = u(k);
  64. y_hat = theta_hat_noise(:,k)' * phi;
  65. e = y - y_hat;
  66. theta_hat_noise(:,k+1) = theta_hat_noise(:,k) + Ts * gamma * e * phi;
  67. end
  68. fprintf('\n1c: Final estimates with disturbance:\n');
  69. fprintf('Estimated m: %.4f, b: %.4f, k: %.4f\n', ...
  70. theta_hat_noise(1,end), theta_hat_noise(2,end), theta_hat_noise(3,end));
  71. figure('Name', '1c - Parameter Estimation with Disturbance', 'Position', [100, 100, 1280, 860]);
  72. sgtitle(sprintf('Lyapunov Estimation with Disturbance | η_0 = %.2f', eta0));
  73. subplot(3,1,1);
  74. plot(t, theta_hat(1,:), 'b', t, theta_hat_noise(1,:), '--b', 'LineWidth', 1.2);
  75. ylabel('m(t)'); grid on; title('Μάζα');
  76. legend('Clear', 'With noise');
  77. subplot(3,1,2);
  78. plot(t, theta_hat(2,:), 'r', t, theta_hat_noise(2,:), '--r', 'LineWidth', 1.2);
  79. ylabel('b(t)'); grid on; title('Απόσβεση');
  80. legend('Clear', 'With noise');
  81. subplot(3,1,3);
  82. plot(t, theta_hat(3,:), 'k', t, theta_hat_noise(3,:), '--k', 'LineWidth', 1.2);
  83. ylabel('k(t)'); xlabel('t [s]'); grid on; title('Ελαστικότητα');
  84. legend('Clear', 'With noise');
  85. if ~exist('output', 'dir')
  86. mkdir('output');
  87. end
  88. saveas(gcf, sprintf('output/Prob1c_disturbance_eta%.2f.png', eta0));