Index exceeds the number of array elements (2).
2 views (last 30 days)
Show older comments
clear all; clc;
format long
en=10^-50;
q=0.9;
o=0.5;
xi=1;
U=1;
gema=i/(6*i+1);
xn(:,1)=-2;
xn(:,2)=1;
i=2;
tic;
while (norm( xn(:,i)- xn(:,i-1) ) > en)
theta(2)= ((q*i)/(i+3));
Thta(2)=((o*i)/(i+3));
s=0.017;
gema=i/(6*i+1);
r(i)=1/(100*i+1); %is used for r(n)
yn(:,i)= (xn(:,i)+theta(i)*(xn(:,i)-xn(:,i-1)));
y1n(:,i)=(xn(:,i)+Thta(i)*(xn(:,i)-xn(:,i-1)));
cn(:,i)=(yn(:,i)-3*s*U*y1n(:,i));
Sn(:,i)=xn(:,i)+xi*((((1-3*s*U)/(1+s*U*4))*cn(:,i)-xn(:,i)));
xn(:,i+1)= Sn(:,i);
if (xn(:,i) ~= xn(:,i-1))
theta(i+1)= min( 1 /( (i+1)^2 *norm( xn(:,i) - xn(:,i-1) ) ), 0.5 ); % theta reprsents theta
else
theta(i+1)= 0.5;
end
theta(i+1)
i=i+1
end
Out Put:
Index exceeds the number of array elements (2).
Error in theeta_fileVMFB (line 27)
y1n(:,i)=(xn(:,i)+Thta(i)*(xn(:,i)-xn(:,i-1)));
0 Comments
Answers (1)
Voss
on 4 Jul 2020
This error happens the second time through the while loop (that is, i == 3). The cause of the error is that Thta only has 2 elements, but this line is trying to reference Thta(3).
5 Comments
Voss
on 5 Jul 2020
There are many ways to avoid running into this error, but just having the code run without errors doesn't mean the code is correct or the results are right. Like Walter Roberson observed, the variable theta grows by one element each time through the while loop, but Thta does not. Perhaps correcting this inconsistency in an appropriate manner would be all you need to do. (But I cannot say what is appropriate and whether this would be sufficient because I don't know what theta and Thta represent and I don't know what the code is supposed to be doing.)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!