function f = lqgcost(x)
%LQGCOST Function which returns the difference between the
%	input and the actual output.
%	Returns a measure of the tracking performance of the system.

%	Copyright (c) 1990-94 by The MathWorks, Inc.

K = x(1:5); % The state feedback control matrix
FF = x(6);  % The feedforward gain

set_param('lqgopt/Matrix gain','D',mat2str(K))
set_param('lqgopt/FF Gain','Gain',mat2str(FF))

[t,x,y] = rk45('lqgopt',6);  % Simulate for 6 seconds
y2 = table1([t,y],0:(6/200):6);  % Interpolate over 201 points
f = y2(:,2) - y2(:,1);   % Output minus the input signal 
