Data identification using RLS
Show older comments
Hello guys I identified data using recursive least square method with forgetting in matlab. Code is below. It works just fine it gives me trasnfer fucntion (tf) and graph. And now I have to do same thing in simulink using matlab function, but it gives some weird results. Do you know what I'm doing wrong?

Matlab code
0.002879
output of rls: tf= ----------------------
z^2 - 1.883 z + 0.8873
clc
clear
load data_cela.mat %data contains input U and "respose" Y
time=1:1:1170;
t=time';
u=U; %input
y=Y; %output
input = 3;
output = 2;
system = input + output;
N = length(y);
%initial conditions
sigma = 1;
C = sigma*eye(system); %p
P = ones(system,1);
lamda = 1; %forgetting factor
for n=3:N
for i=1:2
W(i) = y(n-i); %output
end
for i=1:3
V(i) = u(n-i+1); %input
end
z = [V';W'];
sample_out = y(n);
pom(n)= z' * P;
error(n) = y(n) - pom(n);
C = (C - ( (C*z*z'*C)/( lamda+(z'*C*z) ) ))/lamda;
P = P + (C*z* (sample_out - (z'*P) ) );
change(1:system,n) = P;
end
f_param = [P(1:3);-P(4:5)];
num = [P(1:3,1)];
den = [1;-P(4:5,1)];
num1 = num(3,1);
trasferfunction = tf(num1,den',1)
graf2 = lsim(trasferfunction,u, t,'zoh');
figure(3)
plot(t,graf2, t, y,'r')
title('Recursive method')
legend('Identified','Measured')
grid on
Simulink scheme

Matlab_function rls_iden code
function [P,C] = fcn(u,y,P1,C1)
%initial conditions
W=[];V=[];z=[];error=[];
sigma=1;
pom=zeros(1,4);
lamda = 1;
input = 3;
output = 2;
sys = input + output;
N = length(u);
C = sigma*eye(sys);
P = ones(sys,1);
%loop
for n=3:N
for i=1:2
W(i) = y(n-i);
end
for i=1:3
V(i) = u(n-i+1);
end
z = [V';W'];
sample_out = y(n);
pom(n)= z' * P;
error(n) = y(n) - pom(n);
C = ((C - ( (C*z*z'*C)/( lamda+(z'*C*z) ) ))/lamda)+C1;
P = (P + (C*z* (sample_out - (z'*P) ) ))+P1;
end
All parameters are 1 which I think it shouldn't be. It seems like inner loop didn't do anything and parameters P stayed same like initial condition of P.
Answers (0)
Categories
Find more on Simulink in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!