MATLAB Solve MATLAB Index Problem for Graph Labels

  • Thread starter Thread starter GreenLRan
  • Start date Start date
  • Tags Tags
    Index Matlab
AI Thread Summary
The discussion centers on a MATLAB error related to plotting graph labels, specifically an "Index exceeds matrix dimensions" error. The user is attempting to plot the lift coefficient (CL) against the angle of attack (AoA) but encounters issues due to potential mismatches in vector dimensions. Suggestions include checking the sizes of the vectors involved and ensuring they are both row or column vectors. Ultimately, it was discovered that removing the title command resolved the error, indicating it may have been the source of the problem. The thread highlights the importance of verifying data dimensions in MATLAB to avoid indexing errors.
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
Views
2K
Replies
1
Views
4K
Replies
1
Views
2K
Replies
5
Views
2K
Replies
4
Views
3K
Replies
3
Views
3K
Replies
4
Views
2K
Replies
2
Views
8K
Replies
2
Views
2K
Back
Top