Advertisement
makispaiktis

PROS - Ergasia 1 - Thema 1 - sim.m

Apr 4th, 2021 (edited)
933
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.46 KB | None | 0 0
  1. clear all
  2. clc
  3.  
  4. % Παραμετροι
  5. m = 15;
  6. k = 2;
  7. b = 0.2;
  8. % Function Handle
  9. u = @(t) 5 * sin(2* t) + 10.5;
  10.  
  11. % Initial State, all zero
  12. x0(1) = 0;
  13. x0(2) = 0;
  14. tspan = 0 : 0.1: 10;
  15.  
  16. % Simulation
  17. [t, state] = ode15s(@(t, state)msd(t, state, m, b, k, u), tspan, x0);
  18. t;
  19. display(' ');
  20. y = state(:,1);
  21. display(' ');
  22. velocity = state(:,2);
  23. display(' ');
  24.  
  25. % Plot
  26. figure(1);
  27. plot(t, y);                        % Θεση
  28. figure(2);
  29. plot(t, velocity);                % Ταχυτητα
  30. figure(3);
  31. plot(y, velocity);
  32. display(' ');
  33. display(' ');
  34.  
  35. % ========================================================================
  36. % ========================= Filters with lsim ============================
  37. % ========================================================================
  38. sys1 = tf([-1 0], [1 3 2]);
  39. sys2 = tf([-1], [1 3 2]);
  40. sys3 = tf([1], [1 3 2]);
  41. z = [];
  42. z(1,:) = lsim(sys1, y, t);
  43. z(2,:) = lsim(sys2, y, t);
  44. % Make u a 101-elements array
  45. counter = 0;
  46. uList = [];
  47. for i = 0: 0.1: 10
  48.     counter = counter + 1;
  49.     uList(counter) = u(i);
  50. end
  51. z(3,:) = lsim(sys3, uList, t)
  52.  
  53. z(1,3)
  54. z(2,3)
  55. z(3,3)
  56.  
  57.  
  58.  
  59. A = zeros(3)
  60. N = 101;
  61. for i = 1:N
  62.     % Το i θα είναι η στήλη μας
  63.     stili = z(:,i);
  64.     A = A + stili * stili';
  65. end
  66. A = A / N
  67. Ainv = inv(A)
  68.  
  69. B = zeros(3,1)
  70. for i = 1:N
  71.     stili = z(:,i);
  72.     B = B + stili * y(i);
  73. end
  74. B = B / N
  75.  
  76. answer = Ainv * B
  77. M = 1 / answer(3)
  78. K = (2 + answer(2)) * M
  79. B = (3 + answer(1)) * M
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement