I am trying to plot some functions and display a legend. My code can be found below:
%% DATCOM spanloading method
tol = Input.tol;
iteration = 0;
difference =0;
AVL_step = 0.25;
Interp_step =0.1;
AVLruns = 3;
Angle = ((1:AVLruns)*AVL_step)+AOA;
while sum(difference < 0) <= fix(tol*Input.Surface.Nspan) && iteration < 100
iteration = iteration +1;
if iteration <= AVLruns
AOA = AOA + AVL_step;
[Yle_wing,Spanloading] = obj.AVLspanloading(Input,CLa,AOA); % Creates spanloading with AVL
Scalefunc = 1/(max(Yle_wing)-min(Yle_wing)); % Scale function
Ynorm= ((Yle_wing - min(Yle_wing)) .* Scalefunc)'; % Normalize semi-span from 0 to 1
if length(YClmax) ~= length(Ynorm) && iteration ==1
Clmax_dist= interp1(YClmax,Clmax_dist,Ynorm,'linear');
end
difference = (Clmax_dist - Spanloading); % Difference between resampled CL3d and Cl2d
cl_matrix(iteration,:) = Spanloading;
else
AOA = AOA + Interp_step;
for QQ = 1:Input.Surface.Nspan
CL3d = interp1(Angle,cl_matrix(:,QQ)',AOA,'linear','extrap');
Spanloading(:,QQ) = CL3d;
end
difference = (Clmax_dist - Spanloading);
end
figure(1)
pl = plot(Yle_wing,Clmax_dist,'r');
legendStrs = {'2D Clmax'};
set(pl,'linewidth',1.5);
hold on
if iteration <= AVLruns
plot(Yle_wing,Spanloading,'g--o')
legendStrs = [legendStrs, {'Spanloading by AVL'}];
else
plot(Yle_wing,Spanloading)
legendStrs = [legendStrs, {'Spanloading by extrapolation'}];
end
xlabel('2y/b')
ylabel('Local Cl')
title('DATCOM SPANLOADING METHOD')
legend('boxon')
legend(legendStrs,'Location','SouthWest');
end
if iteration >= 100
disp('Spanloading did not converge, while loop terminated by reaching maximum iteration count')
end
Here, I create a plot in the same figure using the hold on
statement and with an if statement. Running my code will run both conditions of the if statement. Hence, multiple lines will be plotted in this Figure.
Therefore, I want to make a legend for all three plot commands. However, I don't seem to understand how to create the legend for a plot function within an if statement since the following makes my second plot green, and the rest of the plots red.
How would I go about?
Edit: I've incorporated my whole while
loop
DisplayName
property of plots and the related example. Also maybe you could highlight in your code where the loop is? – zeeMonkeez