How can I use the iteration to store variables? It gives error Subscript indices must either be real positive integers or logicals.
1 回表示 (過去 30 日間)
古いコメントを表示
I wrote the code for 2 agents, but facing problem in storing variables in iteration. As the error shows Subscript indices must either be real positive integers or logicals. I want to rum the loop for 50 iterations.
t0=0;
tf=20;
tspan=[t0:1:tf];
x0=[3;0;0];
u1(1)=3; u2(1)=-3;
E1(1)=2; E2(1)=2;
itt=50;N=4;
y1=zeros
y1(1)=3; y2(1)=-1;
a=[0 -1;-1 0];
% For 1 agent
for i=1:itt
options=odeset('RelTol',1e-3,'AbsTol',1e-6);
[t,x]=ode23(@new,tspan,x0,options)
y1_est(i)=y1(i-1)+E1(i)*(du1(i));
err1(i)=y1(i)-y1_est(i);
if (sign(E1(i))<0)
E1(i)=E1(1);
else
E1(i)=E1(i-1)+(eta*du1(i-1)*err1(i-1))/(mu+du1(i-1)^2)
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function xdot = new(t,x)
x1=x(1);
x2=x(2);
x3=x(3);
X1=[x1;x2;x3];
A=[0.5 0.035 0.025;0.0255 0.6 -0.99;0.75 0.03 0.025];
B=[0.2;0.2;0];
C=[1 0 1];
y1=C*X1;
xdot1 =A*X1+B*u1;
xdot =[xdot1];
end
0 件のコメント
採用された回答
Geoff Hayes
2021 年 6 月 17 日
Shivanshu - presumably the error is with
for i=1:itt
options=odeset('RelTol',1e-3,'AbsTol',1e-6);
[t,x]=ode23(@new,tspan,x0,options)
y1_est(i)=y1(i-1)+E1(i)*(du1(i));
because on the first iteration of the list, i is 1 yet the code tries to access y1(i-1)...hence the error. I think that you need to reconsider your how you are accessing data from these arrays. I was going to suggest starting from 2 (i.e. for i=2:itt) but I suspect that will lead to other issues.
8 件のコメント
Geoff Hayes
2021 年 6 月 18 日
If I just use dummy values for Y1 and Y2, then I see the same error as you. It appears that the output from new1 is no longer a column vector/array but a matrix
xdot =
2.1000 2.2440
0.6765 0.8205
2.2500 2.2500
-0.5750 -0.7190
-1.5900 -1.7340
0.0250 0.0250
and this may be related to how your u1 and u2 are computed. I recommend using the MATLAB debugger to step through the code to determine why this happens.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!