Error: Index exceeds the number of array elements. Index must not exceed 1.
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello, I am trying to build a code that identifies a system using recursive least squares and i created an function based on the theory
function[thetak,Pk]=real_time_identification_try2(u,y,Pk_1,thetak_1,alphak)
%function determines theta and the covariance
%u= data input
%y=data output
%n number of zeros
%m number of poles
%alpha = forgetting factor
%vector a_k(vector with inputs and outputs)
a_k=[u, -y];
%implementation
thetak= thetak_1+((Pk_1* a_k.')/((1/alphak)+a_k*Pk_1*a_k.'))*(y(k-a_k*thetak_1));
Pk = Pk_1 -((Pk_1*a_k.'*a_k*Pk_1)/((1/alphak)*a_k*Pk_1*a_k.'));
end
And am calling the function in this script
clc
clear
load linearidade2.mat
u=simout.Data(:,1);
y=simout.Data(:,2);
N = length(u);
%poles and zeros
n= 2;
m= 0;
alphak=0.99;
%setup covariance and theta
P0=100*eye(n+m+1);
theta0 = 0;
%incial conditions
Pk = P0;
thetak = theta0;
for k= max(n,m)+1: N
%input data
u=(k:-1:k-m);
y=(k-1:-1:k-n);
% past inputs
for j = 0:m+1
if k-j > 0
a_k(j+1) = u(k-j); % Insert u(k), u(k-1), ..., u(k-m) em a_k
end
end
% past outputs
for j=1:n
if k-j > 0
a_k(m+j+1) = -y(k-j); %insert y(k),y(k-1),...,y(k-n) em a_k
end
end
[thetak,Pk] = real_time_identification_try2(u, y,Pk,thetak, alphak);
end
% % updating the covariance matix for k+1
for j=0:m+n
if k-j > 0;
P = Pk;
theta = thetak;
end
end
[thetak,Pk]=real_time_identification_try2(u,y,Pk_1,thetak_1,alphak)
% Create the transfer function with estimated parameters
G_estimated = tf([theta(1:1:m+1)], [1 theta(m+2:1:n+m+1)], 0.010);
disp('Estimated Transfer Function:');
G_estimated
% Display final estimated parameters
disp('Final estimated parameters:');
disp(theta);
Thank you in advance
4 comentarios
DGM
el 18 de Oct. de 2024
Editada: DGM
el 18 de Oct. de 2024
You initialize u and y from the matfile
u=simout.Data(:,1);
y=simout.Data(:,2);
but then you overwrite the data with some indices
%input data
u=(k:-1:k-m);
y=(k-1:-1:k-n);
There's also a code analyzer warning on this line:
Pk = Pk_1 -((Pk_1*a_k.'*a_k*Pk_1)/((1/alphak)*a_k*Pk_1*a_k.'));
Parenthesize the multiplication of a_k with its transpose to ensure the result is Hermetian
I don't think that's what's causing your indexing error, but you should double check that it's not causing any issues for your intended purpose.
Respuestas (1)
Cris LaPierre
el 18 de Oct. de 2024
When the first for loop starts, your variables have the following values
- k = 3
- j = 0
- m = 0
- n = 2
So the value of u is u=(k:-1:k-m) which is 3. Then inside the look that defines a_k, you index u using u(k-j), which is u(3). But u is a scalar (one element). Here is code that recreates the error.
u=3;
u(3)
0 comentarios
Ver también
Categorías
Más información sobre Linear Model Identification en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!