title: Kalman Filter in Matlab
body: <p>I have a video and I have to locate the position of a ball using the Kalman equations. Suppose the initial position of the ball in the first frame <code>(xi,yi)</code> is known. </p>
  
  <p>Please guide me what would be the current position <code>(currx,curry);</code> as in the code below in subsequent frames.</p>
  
  <p>What I have so far: </p>
  
  <pre><code>R  = [0.2845 0.0045;0.0045 0.0455];
  Hi = [1 0 0 0; 0 1 0 0];
  Q  = 0.01*eye(4);
  Pe = 100*eye(4);
  F  = [1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1];
  x  = zeros(569,4);
  
  kfinite = false;
  for k = 1:nframes
  
      % Kalman initialization
      if ~kfinite
          Au    = [xi yi 0 0].';
          currx = xi;
          curry = yi;
  
      % Prediction 1st equation
      else            
          u  = [x1 y1 0 0].';
          Au = F*x(k-1,:).' + u; 
          currx = xi;
          curry = yi;
  
      end
  
      vx = currx-Au(1);
      vy = curry-Au(2);
  
      xactual      = [currx curry vx vy].';
      xactualnext  = F*xactual;
      ydesirednext = Hi*xactualnext;
  
      % Prediction 2nd equation
      pp = F*Pe*F.' + Q;   
  
      % correction 3rd equation
      K = pp*Hi.' / (Hi*pp*Hi.' + R); 
  
      % correction 4th equation
      Anu = Au + K*(ydesirednext - Hi*Au); 
  
      % correction 5th equation
      Pe = (eye(4) - K*Hi)*pp;  
      x1 = Anu(1);
      x2 = Anu(2);
  
      kfinite = true;
  
  end
  </code></pre>