On line 53: error = abs(-15 - Theta(i)); . I'm getting an error that says, "Index exceeds the number of array elements. Index must not exceed 17901". what am I doing wrong?

1 visualización (últimos 30 días)
clc
clear all
%Given:
M1 = 3;
G = 1.4;
tot = 180;
Theta= zeros(1,tot);
p2_p1= zeros(1,tot);
n=1;
for i = 1 : 0.01 : tot
Theta(n) = atand(2*cotd(i)*((M1^(2)*sind(i))^2-1)/(M1^(2)*(G+cosd(2*i)+2)));
p2_p1(n) = 1+(2*G)/((G+1)*(M1*sind(i))^2-1);
n = n+1;
end
error = 1;
i = 0;
while error > 0.001
i=i+1;
error = 20 - Theta(i);
end
d=i;
b2 = 38;
Mn1 = M1*sind(b2);
Mn2 = sqrt((Mn1^(2)+(2/(G-1)))/(2*G/(G-1)*Mn1^(2)-1));
M2 = Mn2/(sind(b2-20))
n = 1;
for i = 30 : 0.01 : 149
theta2(n) = 20+atand(2*cotd(i)*((M2*(sind(i)))^2-1)/(2+M2^(2)*(G+cosd(2*i)+2)));
p3_p1(n) = p2_p1(d)*(1+(2*G)/(G+1)*(M2*sind(i))^2-1);
n = n+1;
end
error = 1;
i=0;
while error > 0.001
i=i+1;
error = abs(-15 - Theta(i));
end
a=i;
b3 = 32;
Mn1 = M1*sind(b3);
Mn3 = sqrt((Mn1^(2)+(2/(G-1)))/(2*G/(G-1)*Mn1^(2)-1));
M3 = Mn3/(sind(a-15))

Respuestas (1)

Voss
Voss el 1 de Dic. de 2023
Editada: Voss el 1 de Dic. de 2023
First, let me run the code almost up to where the error happens:
clc
clear all
%Given:
M1 = 3;
G = 1.4;
tot = 180;
Theta= zeros(1,tot);
p2_p1= zeros(1,tot);
n=1;
for i = 1 : 0.01 : tot
Theta(n) = atand(2*cotd(i)*((M1^(2)*sind(i))^2-1)/(M1^(2)*(G+cosd(2*i)+2)));
p2_p1(n) = 1+(2*G)/((G+1)*(M1*sind(i))^2-1);
n = n+1;
end
error = 1;
i = 0;
while error > 0.001
i=i+1;
error = 20 - Theta(i);
end
d=i;
b2 = 38;
Mn1 = M1*sind(b2);
Mn2 = sqrt((Mn1^(2)+(2/(G-1)))/(2*G/(G-1)*Mn1^(2)-1));
M2 = Mn2/(sind(b2-20))
M2 = 1.9621
n = 1;
for i = 30 : 0.01 : 149
theta2(n) = 20+atand(2*cotd(i)*((M2*(sind(i)))^2-1)/(2+M2^(2)*(G+cosd(2*i)+2)));
p3_p1(n) = p2_p1(d)*(1+(2*G)/(G+1)*(M2*sind(i))^2-1);
n = n+1;
end
Now, at this point, Theta is a 1x17901 row vector:
whos Theta
Name Size Bytes Class Attributes Theta 1x17901 143208 double
The minimum value of abs(-15-Theta), which you call error and calculate element-by-element, is approximately 0.0139:
min(abs(-15 - Theta))
ans = 0.0139
Therefore, no element of abs(-15-Theta) is less than or equal to 0.001, so your while loop iterates through all elements of Theta and never terminates, until the error occurs when trying to access element 17902 (i.e., one beyond the end of Theta).
error = 1;
i=0;
while error > 0.001
i=i+1;
error = abs(-15 - Theta(i));
end
Index exceeds the number of array elements. Index must not exceed 17901.
a=i;
b3 = 32;
Mn1 = M1*sind(b3);
Mn3 = sqrt((Mn1^(2)+(2/(G-1)))/(2*G/(G-1)*Mn1^(2)-1));
M3 = Mn3/(sind(a-15))

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by