📄 rls.m
字号:
%Initial_PID_objfun_MSE.m
%Written by Ian Griffin July 2003
%Last Modified 20-08-03
%____________________________________________________________________
function[sys,x0,str,ts]=...
rls(t,x,u,flag,n,theta_init,P_init,lambda,samp_t)
% RLS S-function
% Discrete-time recursive least-squares estimator
% for the Ball and Hoop system
%
% y = x'*theta
%
% Inputs:
% phi - regressor vector,
% y - output.
% Outputs:
% theta - parameter vector,
% P - covariance matrix.
% Parameters:
% n - number of parameters,
% theta_init - initial parameter estimates,
% P_init - initial covariance matrix,
% lambda - forgetting factor,
% samp_t - sampling interval.
%____________________________________________________________________
switch flag,
%%%%%%%%%%%%%%%%%%
% Initialization %
%%%%%%%%%%%%%%%%%%
case 0,
[sys,x0,str,ts] = mdlInitializeSizes(n,theta_init,P_init,samp_t);
%%%%%%%%%%
% Update %
%%%%%%%%%%
case 2,
sys = mdlUpdate(x,u,n,lambda);
%%%%%%%%%%
% Output %
%%%%%%%%%%
case 3,
sys = mdlOutputs(x,n);
%%%%%%%%%%%%%%%%%%%%
% Unexpected flags %
%%%%%%%%%%%%%%%%%%%%
otherwise
sys = [];
end
%____________________________________________________________________
% mdlInitializeSizes
% Return the sizes, initial conditions, and sample times for the Sfunction.
%____________________________________________________________________
function [sys,x0,str,ts] =
mdlInitializeSizes(n,theta_init,P_init,samp_t)
sizes = simsizes;
sizes.NumContStates = 0;
sizes.NumDiscStates = n+n^2;
sizes.NumOutputs = n;
sizes.NumInputs = n+1;
sizes.DirFeedthrough = 0;
sizes.NumSampleTimes = 1;
sys = simsizes(sizes);
x0 = mdlPack(theta_init,P_init,n);
str = [];
ts = [samp_t 0];
%____________________________________________________________________
% mdlUpdate
% Update theta and P
%____________________________________________________________________
function sys = mdlUpdate(x,u,n,lambda)
% unpack theta,P, phi, and y
[theta,P] = mdlUnpack(x,n);
phi = u(1:n);
y = u(n+1);
% compute new estimate and update covariance matrix
K = P*phi/(lambda + phi'*P*phi);
new_theta = theta + K*(y - phi'*theta);
new_P = (eye(n) - K*phi')*P/lambda;
new_P = (new_P + new_P')/2;
% repack theta and P in x
x = mdlPack(new_theta,new_P,n);
sys = x;
%____________________________________________________________________
% mdlOutputs
% Return theta and P
%____________________________________________________________________
function sys = mdlOutputs(x,n)
sys=x(1:n);
sys=sys(:);
%____________________________________________________________________
% mdlPack
% Pack theta and P in x
%____________________________________________________________________
function x = mdlPack(theta,P,n)
P_vec = [];
for j = 1:n
P_vec = [P_vec;P(:,j)];
end
x = [theta;P_vec];
%____________________________________________________________________
% mdlUnpack
% Unpack theta and P from x
%____________________________________________________________________
function [theta,P] = mdlUnpack(x,n)
theta = x(1:n,1);
P = [];
for j = 1:n
P = [P,x(j*n+1:(j+1)*n,1)];
End
%____________________________________________________________________
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -