# plotting from a nested for loop?

8 views (last 30 days)
Christopher on 14 Jun 2013
I have a function that calls another function inside a for loop and the for loop runs that function for N values. The code is shown below. I am trying to plot CL vs N, CDi vs N, and Cl vs y0. I can get CL vs N to plot by setting CL=[result(1),result(2),result(3)], but I can't figure out how to vectorize CDi, Cl, or y0. The current setup runs the LLT_type2 function for N=1, N=5, and N=20 and plots CL vs N, but I am also looking to plot CDi vs N and Cl vs y0. I guess what I am asking is how can I pull out CDi, Cl, and y0 for each iteration of N, so that I can make the plots.
Thanks,
function [LLT_solver] = LLT_run
global b N c a alpha alphoL e AR
type=2
if type == 2
N=[1, 5, 20];
result= zeros(1,length(N));
for i = 1:length(N)
result(i) = LLT_type2(b,N(i),c,a,alpha,alphaoL,e,AR);
[CL,CDi,Cl,y0]=LLT_type2(b,N(i),c,a,alpha,alphoL,e,AR);
end
CL=[result(1),result(2),result(3)]
figure(1)
plot(N,CL)
else if type == 3
LLT_type3(b,N,c,a,alpha,alphoL,e,AR);
end
end
clear all
dpb on 15 Jun 2013
Of course, again, if the function were
function [CL,CDi,Cl]=LLT_type2(b,N,c,a,alpha,alphaoL,e,AR); % whatever it is it does help text
CL = zeros(1, length(N));
CDi=CL; Cl = CL;
for i = 1:length(N)
CL(i) = code_that_computes_CL;
% for the other return variables
end
end
I'll add one final(? :) ) comment on the above...
Just to be clear, the point of suggesting this is that while a loop is shown inside the function as illustrative how computing the N values one hopes that in reality the computations could be fully (or at least mostly) vectorized via Matlab's inherent ability to operate on arrays and vectors and thus the loops could disappear entirely.
If so, one would thus end up w/ both a simpler-looking piece of source and a more generally useful function and likely better performance to boot.