How do I fix it: Index exceeds matrix dimensions?

Please, I am a begginer on Matlab and when I run my code the following warning appers:
Index exceeds matrix dimensions.
Error in otimizando (line 47) end
-- How can I fix it? I already read that it is because of the size of the matrix, but I can't figure it out. The code is:
i = 1;
a(i) = (1+((4*sin(phi_inicial)^2)/solidez*lift_coef*cos(phi_inicial)));
b(i) = (1-3*a(i))/((4*a(i))-1);
error(i) = 5; % guess
error1(i) = 5; % guess
while (error(i)>=0.00001 || error1(i)>=0.00001)
phi(i) = tan((1-a(i))/((1+b(i))*lambda_r));
F = (2/pi)*acos(exp(-((blade_number/2)*(1-(radius_analyzed/radius_total))/((radius_analyzed/radius_total)*sin(phi(i))))));
alfa(i) = phi(i)*180/pi - beta_inicial;
lift(i) = -9e-9*alfa(i)^6 - 3e-7*alfa(i)^5 + 3e-6*alfa(i)^4 - 0.0001*alfa(i)^3 - 0.0003*alfa(i)^2 + 0.1194*alfa(i)+0.0034; % NACA 0012
drag(i) = 9e-9*alfa(i)^6 + 2e-8*alfa(i)^5 - 3e-6*alfa(i)^4 -6e-6*alfa(i)^3 + 0.0003*alfa(i)^2 + 0.0003*alfa(i) + 0.0045; % NACA 0012
trust_coef(i) = (solidez*((1-a(i))^2)*(lift(i)*cos(phi(i))+drag(i)*sin(phi(i))))/sin(phi(i))^2;
if (trust_coef(i)>= 0.96) % Correção Glauerts
a(i+1) = (1/F)*(0.143+sqrt(0.0203-0.6427*(0.889-drag(i))));
else
a(i+1) = (1+((4*F*sin(phi(i))^2)/(solidez*lift(i)*cos(phi(i)))))^-1;
end
b(i+1) = ((4*F*cos(phi(i))/solidez*lift(i))-1)^-1;
error(i) = abs(abs(a(i+1))-abs(a(i)));
error1(i) = abs(abs(b(i+1))-abs(b(i)));
i = i + 1;
end

5 Comments

Please provide the complete error message. Most of all do not let the readers guess, which line is failing.
The message was:
>> otimizando
Index exceeds matrix dimensions.
Error in otimizando (line 46)
end
Which line is line 46? And don't use "error" for a variable name, since that is an inbuilt MATLAB function name.
The line 46 is the line of the "end". How I said before, I'm really a beginner on MATLAB. I do not know how to make this loop works. I tried just use a(i+1)-a(i) for my condition in the while loop, but it doesn't work as well.
Now it gave me the following warning:
Attempted to access error(2); index out of bounds because numel(error)=1.
Error in otimizando (line 27)
while (error(i)>=0.00001 || error1(i)>=0.00001)

Sign in to comment.

 Accepted Answer

You don't update the values for error(i+1) and error1(i+1) like you do for a(i+1) and b(i+1), so on the next iteration they are not defined but you attempt to access them. Try this instead:
error(i+1) = abs(abs(a(i+1))-abs(a(i)));
error1(i+1) = abs(abs(b(i+1))-abs(b(i)));

2 Comments

Thank you! You helped me a lot!
You're welcome! I would like to note that it took only 6 minutes from the time you posted the actual error message with the offending line for us to figure out the error. The lesson here is, in any future posts you make it will help immensely if you post information like this right up front.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!