Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. % EXAMPLE FROM EL2320
  2. %
  3. % This is an example program to simulate the car example described by the
  4. % equations
  5. %
  6. % p(k+1) = p(k) + dt * s(k)
  7. % s(k+1) = s(k) + dt * u(k)
  8. %
  9. % where p(k) and s(k) is the position and speed respectively
  10. %
  11. % This can be re-written in state-space form as
  12. %
  13. % x(k+1) = A * x(k) + B * u(k)
  14. %
  15. % where A = [1 dt; 0 1], B = [0; dt], x=[p; s]
  16. %
  17. % We also assume that we measure the position, which gives
  18. % y(k) = [1 0] * x(k) = C * x(k)
  19. %
  20. % We also add noise, i.e.
  21. %
  22. % x(k+1) = A * x(k) + B * u(k) + G * w(k)
  23. % y(k) = C * x(k) + D * v(k)
  24. % where w(k) is process noise and v(k) is measurement noise.
  25. %
  26. % We assume that G is eye(2) (identity matrix) and that D is 1;
  27. %
  28. % We assume that w(k) and v(k) are white, zero-mean and Gaussian
  29. %
  30. % For estimation we use the Kalman Filter
  31. %
  32. % xhat(k+1|k) = A * xhat(k|k) + B * u(k)
  33. % P(k+1|k) = A * P * A^T + G * R * G^T
  34. %
  35. % K(k+1) = P(k+1|k) * C^T * inv(C * P(k+1|k) * C^T + D * Q * D^T)
  36. % xhat(k+1|k+1) = xhat(k+1|k) + K(k+1) * (y(k+1) - C * xhat(k+1|k))
  37. % P(k+1|k+1) = P(k+1|k) - K(k+1) * C * P(k+1|k)
  38. %
  39. % This program assumes that dt=0.1s and allows you to play with different
  40. % setting for the noise levels. Try with different values for the noise levels
  41.  
  42. %%%%%%%%%%%%%%%%%%%%%%%%%%
  43. % The system model
  44. dt=0.1
  45. A = [1 dt; 0 1];
  46. B = [0; dt];
  47. C = [1 0];
  48.  
  49. x = [1 0.5]';
  50. u = 0;
  51.  
  52. % the simulated noise
  53.  
  54. wStdP = 0.01; % Noise on simulated position
  55. wStdV = 0.1; % Noise on simulated velocity
  56. vStd = 0.1; % Simulated measurement noise on position
  57.  
  58. %%%%%%%%%%%%%%%%%%%%%%%%%%
  59. % The Kalman Filter modeled uncertainties and initial values
  60.  
  61. xhat = [-2 0]';
  62. P = eye(2)*1;
  63. G = eye(2);
  64. D = 1;
  65. R = diag([0.01^2 0.1^2]);
  66. Q = 0.1^2;
  67.  
  68. n = 100;
  69.  
  70. X = zeros(2,n+1);
  71. Xhat = zeros(2,n+1);
  72. PP = zeros(4,n+1);
  73. KK = zeros(2,n);
  74.  
  75. X(:,1) = x;
  76. Xhat(:,1) = xhat;
  77. PP(:,1) = reshape(P,4,1);
  78. figure(1)
  79. drawnow
  80.  
  81. for k = 1:n
  82. x = A * x + B * u + [wStdP*randn(1,1); wStdV*randn(1,1)];
  83. y = C * x + D * vStd*randn(1,1);
  84. X(:,k+1) = x;
  85.  
  86. % Prediction
  87. xhat = A * xhat + B * u;
  88. P = A * P * A' + G * R * G';
  89.  
  90. % Measurement update
  91. K = P * C' * inv(C * P * C' + D * Q * D');
  92. xhat = xhat + K * (y - C * xhat);
  93. P = P - K * C * P;
  94. Xhat(:,k+1) = xhat;
  95. KK(:,k) = K;
  96. PP(:,k+1) = reshape(P,4,1);
  97.  
  98. clf, subplot(2,1,1),
  99. plot(X(1,1:(k+1)),'r')
  100. hold on,
  101. plot(Xhat(1,1:(k+1)),'b')
  102. plot(X(1,1:(k+1))-Xhat(1,1:(k+1)),'g')
  103. % axis([0 n+5 -2 7])
  104. title('Position (red: true, blue: est, green: error)')
  105. %legend('true','est','error')
  106.  
  107. subplot(2,1,2),
  108. plot(X(2,1:(k+1)),'r')
  109. hold on,
  110. plot(Xhat(2,1:(k+1)),'b')
  111. plot(X(2,1:(k+1))-Xhat(2,1:(k+1)),'g')
  112. % axis([0 n+5 -5 5])
  113. title('Speed (red: true, blue: est, green: error)')
  114. %legend('true','est','error')
  115.  
  116. drawnow
  117. end
  118.  
  119. E = X - Xhat;
  120. disp(sprintf('Standard deviation of error in position (second half): %fm', std(E(1,round(size(E,2)/2):end))))
  121. disp(sprintf('Standard deviation of error in velocity (second half): %fm/s', std(E(2,round(size(E,2)/2):end))))
  122.  
  123. figure(2)
  124. title('Estimated error covariance')
  125. subplot(2,1,1),
  126. plot(sqrt(PP(1,:)))
  127. title('sqrt(P(1,1))')
  128. subplot(2,1,2),
  129. plot(sqrt(PP(4,:)))
  130. title('sqrt(P(2,2))')
  131.  
  132. figure(3)
  133. title('Kalman filter gain coefficients')
  134. subplot(2,1,1),
  135. plot(KK(1,:))
  136. title('K(1)')
  137. subplot(2,1,2),
  138. plot(KK(2,:))
  139. title('K(2)')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement