You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: DEM_demo_GF_and_KF.m
    path of file: ./repos/bspm/thirdparty/spm12/toolbox/DEM
    
    the code of the file until where you have to start completion: function DEM_demo_GF_and_KF
% A demonstration of generalised and Kalman filtering where the number
% of hidden states exceeds the number of variables observed. The metrics of
% performance are the mean sum of squared error and the SSE normalized by
% the posterior precision (NESS). The results of a single time series
% analysis are shown first and then the simulations are repeated under
% linear and nonlinear observation models to compare the relative
% performance of DEM and EKF. The superiority of DEM (generalised filtering)
% over Kalman filtering rests on the optimisation of K - the rate of
% generalised descent on free energy (see code after 'return').
%__________________________________________________________________________
% Copyright (C) 2011 Wellcome Trust Centre for Neuroimaging
 
% Karl Friston
% $Id: DEM_demo_GF_and_KF.m 4804 2012-07-26 13:14:18Z karl $
 
 
% linear generative model
%==========================================================================
 
% The problem: A simple linear convolution model with parameters
%--------------------------------------------------------------------------
n       = 8;                  % number of hidden states
m       = 4;                  % number of observations
P.f     = full(toeplitz(sparse([1 2],1,[-2 1],n,1)));
P.g     = spm_dctmtx(m,n);    % Jacobian and observer matrices
 
              
% preliminaries
%--------------------------------------------------------------------------
N        = 128;               % length of sequence
t        = 1:N;               % time
M(1).E.s = 1;                 % smoothness of fluctuations
M(1).E.n = 4;                 % order of generalised coordinates
M(1).E.K = exp(-1);           % rate of generalized gradient descent
M(1).E.linear = 1; 
 
% Level 1: 
%--------------------------------------------------------------------------
M(1).f   = inline('P.f*x','x','v','P');
M(1).g   = inline('P.g*x','x','v','P');
M(1).x   = zeros(n,1);
M(1).pE  = P;
M(1).V   = exp(4);            % precision of observation noise
M(1).W   = exp(2);            % precision of fluctuations on hidden states
 
   
% create data
%==========================================================================
DEM      = spm_DEM_generate(M,N);
 
% Comparative inversions (variants of generalised filtering)
%==========================================================================
 
% DEM and EKF
%--------------------------------------------------------------------------
DEM     = spm_DEM(DEM);
[EKF S] = spm_ekf(DEM.M,DEM.Y);
  
 
% show prediction errors

    
function DEM_demo_GF_and_KF
% A demonstration of generalised and Kalman filtering where the number
% of hidden states exceeds the number of variables observed. The metrics of
% performance are the mean sum of squared error and the SSE normalized by
% the posterior precision (NESS). The results of a single time series
% analysis are shown first and then the simulations are repeated under
% linear and nonlinear observation models to compare the relative
% performance of DEM and EKF. The superiority of DEM (generalised filtering)
% over Kalman filtering rests on the optimisation of K - the rate of
% generalised descent on free energy (see code after 'return').
%__________________________________________________________________________
% Copyright (C) 2011 Wellcome Trust Centre for Neuroimaging
 
% Karl Friston
% $Id: DEM_demo_GF_and_KF.m 4804 2012-07-26 13:14:18Z karl $
 
 
% linear generative model
%==========================================================================
 
% The problem: A simple linear convolution model with parameters
%--------------------------------------------------------------------------
n       = 8;                  % number of hidden states
m       = 4;                  % number of observations
P.f     = full(toeplitz(sparse([1 2],1,[-2 1],n,1)));
P.g     = spm_dctmtx(m,n);    % Jacobian and observer matrices
 
              
% preliminaries
%--------------------------------------------------------------------------
N        = 128;               % length of sequence
t        = 1:N;               % time
M(1).E.s = 1;                 % smoothness of fluctuations
M(1).E.n = 4;                 % order of generalised coordinates
M(1).E.K = exp(-1);           % rate of generalized gradient descent
M(1).E.linear = 1; 
 
% Level 1: 
%--------------------------------------------------------------------------
M(1).f   = inline('P.f*x','x','v','P');
M(1).g   = inline('P.g*x','x','v','P');
M(1).x   = zeros(n,1);
M(1).pE  = P;
M(1).V   = exp(4);            % precision of observation noise
M(1).W   = exp(2);            % precision of fluctuations on hidden states
 
   
% create data
%==========================================================================
DEM      = spm_DEM_generate(M,N);
 
% Comparative inversions (variants of generalised filtering)
%==========================================================================
 
% DEM and EKF
%--------------------------------------------------------------------------
DEM     = spm_DEM(DEM);
[EKF S] = spm_ekf(DEM.M,DEM.Y);
  
 
% show prediction errors
%==========================================================================
spm_figure('Getwin','Figure 1');
 
MSE(1,:) = mean((DEM.pU.x{1} - DEM.qU.x{1}).^2);
MSE(2,:) = mean((DEM.pU.x{1} - EKF).^2);
 
for i = 1:N
    E         = DEM.pU.x{1}(:,i) - DEM.qU.x{1}(:,i);
    NEES(1,i) = E'*spm_inv(DEM.qU.S{i})*E;
    E         = DEM.pU.x{1}(:,i) - EKF(:,i);
    NEES(2,i) = E'*spm_inv(S{i})*E;
end