Error: Index exceeds the number of array elements. Index must not exceed 1.

조회 수: 7 (최근 30일)
Aliya
Aliya 2024년 10월 18일
댓글: Aliya 2024년 10월 19일
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
Index exceeds the number of array elements. Index must not exceed 1.
% % 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
DGM
DGM 2024년 10월 18일
편집: DGM 2024년 10월 18일
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.
Aliya
Aliya 2024년 10월 19일
thank you this parte was indeed incorrect!

댓글을 달려면 로그인하십시오.

답변 (1개)

Cris LaPierre
Cris LaPierre 2024년 10월 18일
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)
Index exceeds the number of array elements. Index must not exceed 1.

카테고리

Help CenterFile Exchange에서 Numeric Solvers에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by