# How to solve exceed number of array element (1) problem?

3 views (last 30 days)
shahin sharafi on 5 Aug 2021
Commented: darova on 5 Aug 2021
Hi
As I try to run the bellow code, I will encounter the Index exceeds the number of array elements (1). As I tried the Time_Delay_Bra as a constant, the problem will be solved. However as I use it as "Time_Delay_Bra=0.1:0.05:0.2", it causes problem for me.
Thank youo in advance
%%
tic
clear all
clc
close all
%%
Mass=60;Lenght=1;Ja=60;g=9.81;beta2=411.67;
Kp_Bra=2.15;Kd_Bra=0.75;
alpha=(Mass*Lenght*g)/Ja;Zeta3=beta2/Ja;
N=7;
Time_Delay_Bra=0.1:0.05:0.2;
Time_Delay_Exo=0.1:0.05:0.2;
AreaCount=1;
AreaVector=zeros(1,AreaCount);
AreaMatrix=zeros(length(Time_Delay_Exo),length(Time_Delay_Bra));
for j=1:length(Time_Delay_Bra)
for jj=1:length(Time_Delay_Exo)
%% Legendre Polynomial
s1=0;
s2=-Time_Delay_Bra(j);
s3=-Time_Delay_Exo(jj);
[Phi_0_s,Phi_BTD_s,Phi_ETD_s]=Shape_Function(N,s1,s2,s3);
%%
Time_Delay_Bra=Time_Delay_Bra(j);
[M,C]=M_C(N,Time_Delay_Bra);
%%
count=1;
KP_Exo=0:20:800;
KD_Exo=0:20:800;
x=zeros(1,count);
y=zeros(1,count);
MaximunEigenValuesMatrix=zeros(length(KP_Exo),length(KD_Exo));
WantedEigenValuesforDefinedGaines=zeros(1,1);
for ii=1:length(KP_Exo)
KP_Exo(ii)
for i=1:length(KD_Exo)
KP1=KP_Exo(ii)/Ja;
KD1=KD_Exo(i)/Ja
Final_Matirx=Evaluation(KP1,KD1,alpha,Phi_0_s,Phi_BTD_s,Phi_ETD_s,M,C,Zeta3,Kp_Bra,Kd_Bra);
Max_Real_EignValues=max(real(eig(Final_Matirx)));
if Max_Real_EignValues<0
MaximunEigenValuesMatrix(ii,i)=max(real(eig(Final_Matirx)));
x(1,count)=KP1*Ja;
y(1,count)=KD1*Ja;
count=count+1;
end
hold on
end
end
k = boundary(x',y');% generate boundary of data points
plot(x(k),y(k))
Area= polyarea(x,y);
AreaMatrix(jj,j)=Area;
end
end
toc
%%
function [Phi_0_s,Phi_BTD_s,Phi_ETD_s]=Shape_Function(N,s1,s2,s3)
% % Shape Function for the Zero PArt
Phi_0_s(1)=1;
Phi_0_s(2)=1+2*s1/(-s2);
for k=3:N
Phi_0_s(k)=((2*k-3)*Phi_0_s(2)*Phi_0_s(k-1)-(k-2)*Phi_0_s(k-2))/(k-1);
end
Phi_0_s=Phi_0_s';
%% Shape Function for the Brain TimeDelay Part
Phi_BTD_s(1)=1;
Phi_BTD_s(2)=1+2*s2/(-s2);
for k=3:N
Phi_BTD_s(k)=((2*k-3)*Phi_BTD_s(2)*Phi_BTD_s(k-1)-(k-2)*Phi_BTD_s(k-2))/(k-1);
end
Phi_BTD_s=Phi_BTD_s';
%% Shape Function for the Exo TimeDelay Part
Phi_ETD_s(1)=1;
Phi_ETD_s(2)=1+2*s3/(-s2);
for k=3:N
Phi_ETD_s(k)=((2*k-3)*Phi_ETD_s(2)*Phi_ETD_s(k-1)-(k-2)*Phi_ETD_s(k-2))/(k-1);
end
Phi_ETD_s=Phi_ETD_s';
end
function [M,C]=M_C(N,Time_Delay_Bra)
Delta=zeros(N,N);
M=zeros(N,N);
C=zeros(N,N);
for i=1:N
for j=1:N
if i==j
Delta(i,j)=1;
else
Delta(i,j)=0;
end
end
end
for i=1:N
for j=1:N
M(i,j)=(Time_Delay_Bra*Delta(i,j))/(2*i-1);
if i<j
if rem(i+j, 2) == 1
C(i,j)=2;
else
C(i,j)=0;
end
end
end
end
end
function [L]=Evaluation(KP1,KD1,alpha,Phi_0_s,Phi_BTD_s,Phi_ETD_s,M,C,Zeta3,Kp_Bra,Kd_Bra)
kp_exo=KP1;
kd_exo=KD1;
kp_bra=Zeta3*Kp_Bra;
kd_bra=Zeta3*Kd_Bra;
%% G1&G2
G1=alpha*Phi_0_s'-kp_exo*Phi_ETD_s'-kp_bra*Phi_BTD_s';
G2=-kd_bra*Phi_BTD_s'-kd_exo*Phi_ETD_s';
%% C3
C3=Phi_0_s'*inv(M)*Phi_0_s;
%% X Matrix
X1=(Phi_0_s*Phi_0_s')/C3;
X2=-((Phi_0_s*Phi_0_s'/M)*C)/C3;
X3=(Phi_0_s*G1)/C3;
X4=(Phi_0_s*G2-(Phi_0_s*Phi_0_s'/M)*C)/C3;
%% L Matrix
L1=C/M+X2/M;
L2=X1/M;
L3=X3/M;
L4=C/M+X4/M;
L=[L1 L2;L3 L4];
end

darova on 5 Aug 2021
Here is the problem. You are redefining the variable inside for loop and it has (1) element again ##### 2 CommentsShowHide 1 older comment
darova on 5 Aug 2021 R2019a

### Community Treasure Hunt

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

Start Hunting!