plotting from a nested for loop?

1 visualización (últimos 30 días)
Christopher
Christopher el 14 de Jun. de 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
read_inp_type2;
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
read_inp_type3;
LLT_type3(b,N,c,a,alpha,alphoL,e,AR);
end
end
clear all
  4 comentarios
dpb
dpb el 15 de Jun. de 2013
Editada: dpb el 15 de Jun. de 2013
Is the function LLT_type2() your function? Why not vectorize it to return N values for the vector passed into it?
But, that aside, assuming it really does return the multiple values, and you want CL, CDi, Cl (and now a whole lot more have shown up in the return??? but we'll ignore that for now) then what I was suggesting was sotoo--
N = [1, 5, 20];
CL = zeros(1, length(N)); % preallocate
CDi=CL; Cl = CL; % ditto except by copying
for i = 1:length(N);
[CL(i),CDi(i),Cl(i)]=LLT_type2(b,N(i),c,a,alpha,alphaoL,e,AR);
end
Now you've got three vectors of length N directly -- you don't need all the intermediary variables at all.
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
Then, your higher-level function could look something like
N=[1 5 20];
[CL,CDi,Cl]=LLT_type2(b,N,c,a,alpha,alphaoL,e,AR);
% do whatever w/ return values here
and the loop all is hidden at the lower level.
If there are more things to compute and return, just follow the same pattern for them.
dpb
dpb el 15 de Jun. de 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.

Iniciar sesión para comentar.

Respuestas (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by