Index exceeds the number of array elements (interation)
Ältere Kommentare anzeigen
Im trying to iterate
xin = xh^2/sqrt(x(n)^2+mu^2);
x(n+1) = x(n)-((x(n)-muz-xin)/((1+xinx(n))/(x(n)^2+mu^2)))
In the setup, pre iteration, I am getting an error
Index exceeds the number of array elements. Index must not exceed 1.
for the last line below. Can anybody point me in the right direction? I can provide the full code if needed.
clc
clear
format long
% lambda is x
Rmain=27; %ft
Cmain=1.7; %ft
Nbmain=4;
Tipspeed_main=725; %ft/s
W=16000; %lb
alt=5000;
alpha_tpp=3;
%knots
%%
Amain=pi*Rmain^2;
sigma=(Nbmain*Cmain)/(pi*Rmain);
[~, ~, ~, RHO] = atmosisa(alt*0.3048);
rho=RHO*0.0685218/35.3147;
CT = W/(rho*Amain*Tipspeed_main^2);
xh = sqrt(CT/2);
n = 1;
x(n) = xh;
err = 10^-5;
relerr(n) = abs(x(n+1)-x(n))./x(n+1);
1 Kommentar
Voss
am 21 Mär. 2024
x(2) hasn't been calculated yet.
Antworten (1)
T.Nikhil kumar
am 9 Apr. 2024
Hello Jaevon,
I can see that you are facing an error about index exceeding the number of elements while working with an array.
It seems that the error you're encountering is due to trying to access x(n+1) before it has been calculated. In the last line i.e. where you define ‘relerr(n)’, you're attempting to use x(n+1) which does not exist at that point in your code.
I assume that you have provided the pre-iteration code only and have the iteration logic already. I would suggest you to calculate x(n+1) first and then ‘relerr(n)’ inside your iteration loop. Please look at the below snippet for a rough idea of how your loop should look like:
while true %some condition as per your requirement (don't leave it as true)
%calculate xin
xin = xh^2 / sqrt(x(n)^2 + mu^2);
%calculate x(n+1)
x(n+1) = x(n) - ((x(n) - muz - xin) / ((1 + xin * x(n)) / (x(n)^2 + mu^2)));
%now that x(n+1) has been calculated, calculate the relerr
relerr(n) = abs(x(n+1) - x(n)) / x(n+1);
%update counter
n = n + 1;
end
You can also consider preallocating space for 'x' and 'relerr' for efficiency.
Hope this helps you proceed further!
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!