Solve MATLAB Index Problem for Graph Labels

In summary: Read the full conversationIn summary, the conversation revolves around a problem with graph labels in a code for AEM313 Project 2. The error "? Index exceeds matrix dimensions" is mentioned and the code is provided. The size of CL is checked and the error is attributed to the title command. Some troubleshooting is done but the issue remains unresolved.
  • #1
GreenLRan
61
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
  • #2
Check the size of CL:

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

1 17
which is the same size as alphaLzero-8:alphaLzero+8
 
  • #4
check and see if they are both column or row vectors. your error look like that may be the problem.
 
  • #5
they are both 1 row 17 columns
 
  • #6
"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])
 
  • #7
That doesn't work either, Same error..."Index exceeds matrix dimensions"
 
  • #8
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.
 
  • #9
%%%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')
 

1. What is the MATLAB index problem for graph labels?

The MATLAB index problem for graph labels refers to the issue of correctly labeling data points or nodes on a graph when using the plot or graph functions in MATLAB. Due to the way MATLAB indexes arrays and matrices, the labels may not correspond to the correct data point or node on the graph.

2. How does the MATLAB index problem affect graphing?

The MATLAB index problem can result in mislabeled data points or nodes on a graph, which can lead to confusion and incorrect interpretation of the data. It can also make it difficult to create a visually appealing and accurate graph.

3. Is there a solution to the MATLAB index problem for graph labels?

Yes, there are several solutions to the MATLAB index problem for graph labels. One solution is to manually adjust the axis labels using the 'XTick' and 'YTick' functions. Another solution is to use the 'XTickLabel' and 'YTickLabel' functions to specify the labels for each data point or node on the graph.

4. Can the MATLAB index problem be prevented?

While the MATLAB index problem cannot be completely prevented, it can be minimized by carefully organizing and labeling the data points or nodes before plotting the graph. This can help ensure that the correct labels are assigned to each data point.

5. Are there any resources available to help with the MATLAB index problem for graph labels?

Yes, there are many online resources and tutorials available that provide step-by-step instructions on how to solve the MATLAB index problem for graph labels. The official MATLAB documentation also has information on how to correctly label graphs and prevent the index problem.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
345
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
Back
Top