Solve MATLAB Index Problem for Graph Labels

  • Context: MATLAB 
  • Thread starter Thread starter GreenLRan
  • Start date Start date
  • Tags Tags
    Index Matlab
Click For Summary

Discussion Overview

The discussion revolves around a MATLAB indexing issue encountered while trying to plot graph labels in a project related to aerodynamics. Participants are troubleshooting an error message indicating that the index exceeds matrix dimensions, which occurs during the plotting of coefficients of lift (CL) against angle of attack (AoA). The scope includes technical explanations and debugging of MATLAB code.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes an error message related to indexing in their MATLAB code when attempting to plot graph labels.
  • Another participant suggests checking the size of the CL variable to ensure it matches the expected dimensions for plotting.
  • A participant confirms that the size of CL is indeed 1 row by 17 columns, which matches the expected size for the AoA range.
  • It is proposed that the issue may arise from the orientation of the vectors, prompting a check to see if both CL and AoA are column or row vectors.
  • A later reply suggests an alternative way to define the AoA variable for clarity, but this does not resolve the error.
  • One participant claims that the error is caused by the title() command and notes that removing it allows the plot to work correctly.
  • Another participant shares a revised version of the code with additional parameters and calculations, but the original indexing issue remains a point of contention.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the cause of the error, with multiple viewpoints on whether the issue lies in the indexing, vector orientation, or the title command. The discussion remains unresolved regarding the exact source of the problem.

Contextual Notes

Participants mention specific dimensions and orientations of matrices and vectors, but there are unresolved assumptions about the data types and structures being used in the MATLAB code. The discussion also involves multiple iterations of the code, which may introduce additional complexity.

GreenLRan
Messages
59
Reaction score
0
I'm having a problem getting my graph labels to work. I get an error saying


"? Index exceeds matrix dimensions.

Error in ==> aeroproject2 at 88
plot(alphaLzero-8:alphaLzero+8,CL(1,1:17)),title('CL vs Alpha'),xlabel('Angle of Attack'),ylabel('CL')"

can anyone help? thanks, and here is the code.



%%%AEM313 Project 2%%%

%%see in givens%%
prompt = {'Taper Ratio:','Full Span (b):','Wing Twist (degrees):','Zero Lift AOA (degrees):','Sweep Angle (degrees):','M (number of coefficients):'};
title = 'Input';
num_lines = 1;
def = {'.5','5','0','0','22','3'};
answer = inputdlg(prompt,title,num_lines,def);
assignin('base','lamda',answer{1});
assignin('base','b',answer{2});
assignin('base','alpha_GT',answer{3});
assignin('base','alphaLzero',answer{4});
assignin('base','sweep',answer{5});
assignin('base','m',answer{6});
lamda = str2num(lamda)
b = str2num(b)
alpha_GT = str2num(alpha_GT)
alphaLzero = str2num(alphaLzero)
sweep = str2num(sweep)
m = str2num(m)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
S=(2*(.5*b*lamda + 2*(.5 * b * .5 *( .5 * (1-lamda)))));
AR= b^2/S;
d_theta = pi /(2*m);
i=1;
theta{i} = pi / 2;
while i <= m
i=i+1;
theta{i} = theta{i-1} - d_theta;
end
for p = 1:m
n = 1;
for j = 1:m
%%%using + in Chord(theta_naught) for right wing
B(p,j) = 2*b/(pi*(1+(1-lamda)*cos(theta{p}))) * sin(n*theta{p}) + n*sin(n*theta{p})/sin(theta{p});
n=n+2;
end
end
for x = 1:m
y=1;
AOA = alphaLzero - 8;
while AOA <= alphaLzero + 8
alpha = AOA + alpha_GT * cos (theta{x});
C(x,y) = (alpha - alphaLzero)*pi/180;
AOA = AOA + 1;
y=y+1;
end
end
A = inv(B(1:m,1:m))*C(1:m,1:17);
CL = pi*AR*A(1,1:17)
AR
for u = 1:17
g=1;
l=1;
while g <= m * 2 - 1
sum=g*A(l,u)^2;
g=g+2;
l=l+1;
end
CDi(u) = pi*AR*sum;
end
aslope = (CL(10)-CL(9));
AoA= alphaLzero-8:alphaLzero+8;
tau = 2*pi/aslope - 2;
v=1;
if (AR > 4) && (sweep < 10)
figure(1)
plot(AoA,CL),title('CL vs Alpha'),xlabel('Angle of Attack'),ylabel('CL')
end
if (AR < 4) && (sweep < 10)
a=2*pi/(tau^2+3*tau+3)^.5;
for AoA = alphaLzero-8:alphaLzero+8;
CL(v)=a*(AoA - alphaLzero);
v=v+1;
end
figure(1)
plot(alphaLzero-8:alphaLzero+8,CL(1:17)),title('CL vs Alpha'),xlabel('Angle of Attack'),ylabel('CL')
end
if (AR > 4) && (sweep > 10)
sweep = sweep*pi/180;
a = 2*pi*cos(sweep)/(1+cos(sweep)*(1+tau));
for AoA = alphaLzero-8:alphaLzero+8;
CL(v) = a*(AoA - alphaLzero);
v=v+1;
end
CL(1,1:17)
figure(1)
plot(alphaLzero-8:alphaLzero+8,CL(1,1:17)),title('CL vs Alpha'),xlabel('Angle of Attack'),ylabel('CL')
end
if (AR < 4) && (sweep > 10)
sweep = sweep*pi/180;
a=2*pi*cos(sweep)/( (1+(cos(sweep)*(1+tau))^2)^.5 + (cos(sweep)*(1+tau)));
for AoA = alphaLzero-8:alphaLzero+8;
CL=a*(AoA - alphaLzero);
v=v+1;
end
figure(1)
plot(alphaLzero-8:alphaLzero+8,CL(1:17)),title('CL vs Alpha'),xlabel('Angle of Attack'),ylabel('CL')
end
 
Physics news on Phys.org
Check the size of CL:

size(CL)
 
I checked the size and it gave me
ans =

1 17
which is the same size as alphaLzero-8:alphaLzero+8
 
check and see if they are both column or row vectors. your error look like that may be the problem.
 
they are both 1 row 17 columns
 
"plot(alphaLzero-8:alphaLzero+8,CL(1:17))"

Try (for clarity):

AA=alphaLzero-8:alphaLzero+8;

plot(AA(1,:),CL(1,:))

(of course, size(AA) should be [1 17])
 
That doesn't work either, Same error..."Index exceeds matrix dimensions"
 
I found that it's a problem with the title() command.. for some reason the title command is causing the error. I took it out, and it works fine.
 
%%%AEM313 Project 2%%%

%%see in givens%%
prompt = {'Taper Ratio:','Full Span (b):','Wing Twist (degrees):','Zero Lift AOA (degrees):','Sweep Angle (degrees):','M (number of coefficients):','Re','lam or turb (1 or 2)'};
title = 'Input';
num_lines = 1;
def = {'.5','5','0','0','22','3','500000','1'};
answer = inputdlg(prompt,title,num_lines,def);
assignin('base','lamda',answer{1});
assignin('base','b',answer{2});
assignin('base','alpha_GT',answer{3});
assignin('base','alphaLzero',answer{4});
assignin('base','sweep',answer{5});
assignin('base','m',answer{6});
assignin('base','Re',answer{7});
assignin('base','desig',answer{8});
lamda = str2num(lamda)
b = str2num(b)
alpha_GT = str2num(alpha_GT)
alphaLzero = str2num(alphaLzero)
sweep = str2num(sweep)
m = str2num(m)
Re = str2num(Re)
desig = str2num(desig)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
S=(2*(.5*b*lamda + 2*(.5 * b * .5 *( .5 * (1-lamda)))));
AR= b^2/S;
d_theta = pi /(2*m);
i=1;
theta{i} = pi / 2;
while i <= m
i=i+1;
theta{i} = theta{i-1} - d_theta;
end
for p = 1:m
n = 1;
for j = 1:m
%%%using + in Chord(theta_naught) for right wing
B(p,j) = 2*b/(pi*(1+(1-lamda)*cos(theta{p}))) * sin(n*theta{p}) + n*sin(n*theta{p})/sin(theta{p});
n=n+2;
end
end
for x = 1:m
y=1;
AOA = alphaLzero - 8;
while AOA <= alphaLzero + 8
alpha = AOA + alpha_GT * cos (theta{x});
C(x,y) = (alpha - alphaLzero)*pi/180;
AOA = AOA + 1;
y=y+1;
end
end
A = inv(B(1:m,1:m))*C(1:m,1:17)
CL = pi*AR*A(1,1:17);
AR
%%%% calculations based on AR and sweep %%%%%
aslope = (CL(10)-CL(9));
AoA= alphaLzero-8:alphaLzero+8;
tau = 2*pi/aslope - 2;
v=1;
%%if (AR > 4) && (sweep < 10)
%% figure(1)
%% plot(AoA,CL),title('CL vs Alpha'),xlabel('Angle of Attack'),ylabel('CL')
%%end
if (AR < 4) && (sweep < 10)
a=2*pi/(tau^2+3*tau+3)^.5;
for AoA = alphaLzero-8:alphaLzero+8;
CL(v)=a*(AoA - alphaLzero);
v=v+1;
end
end
if (AR > 4) && (sweep > 10)
sweep = sweep*pi/180;
a = 2*pi*cos(sweep)/(1+cos(sweep)*(1+tau));
for AoA = alphaLzero-8:alphaLzero+8;
CL(v) = a*(AoA - alphaLzero);
v=v+1;
end
end
if (AR < 4) && (sweep > 10)
sweep = sweep*pi/180;
a=2*pi*cos(sweep)/( (1+(cos(sweep)*(1+tau))^2)^.5 + (cos(sweep)*(1+tau)));
for AoA = alphaLzero-8:alphaLzero+8;
CL(v)=a*(AoA - alphaLzero);
v=v+1;
end
end

%%%% induced drag %%%%
for u = 1:17
g=3;
l=2;
del=g*(A(l,u)/A(1,u))^2;
while l<m %% g <= m * 2 - 1
g=g+2;
l=l+1;
del = del + g*(A(l,u)/A(1,u))^2;
if A(l,u) == 0
del = 0;
end
end

CDi(1,u) = CL(1,u)^2*(1+del)/(pi*AR)
end
e = 1/(1+del);
%%% skin friction drag %%%
mslope = -(1-lamda)/(b/2);
dx=.1*b/2;
Cf_lam = 1.328/(Re)^.5 * 2;
Cf_turb = .074/(Re)^.2 * 2;
while dx <= b/2
y = mslope*dx + 1;
Re_y = Re*y;
Cf_lam = Cf_lam + 1.328/(Re_y)^.5 * 4;
Cf_turb = Cf_turb + .074/(Re_y)^.2 * 4;
dx=dx+.1*b/2;
end
if desig == 1
CD = CDi(1:17) + Cf_lam;
end
if desig == 2
CD = CDi(1:17) + Cf_turb;
end
MAC= 2/S*(lamda^2+lamda+1)*b/6;
AoA=alphaLzero-8:alphaLzero+8;
figure(1)
%%plot (AoA(1,9),CD(1,9)),title('CD vs Alpha'),xlabel('Angle of Attack'),ylabel('CD')
%%hold on
%%title('CD vs Alpha')
xlabel('Angle of Attack')
ylabel('CD')
plot (AoA(1,:),CD(1,:)),xlabel('Angle of Attack'),ylabel('CD')

figure(2)
plot (AoA(1,:),CL(1,:)),xlabel('Angle of Attack'),ylabel('CL')
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 8 ·
Replies
8
Views
2K