Friday, 15 May 2015

matlab - how to output all the iteration results from a FOR loop into matrix and plot the graph -



matlab - how to output all the iteration results from a FOR loop into matrix and plot the graph -

i have 2 for loops in nested format. sec loop calculates final equation. display of result outside sec loop in order display when sec loop complete.

below logic used in matlab. need plot graph of eqn2 vs x.

l=100 x=1:10 eqn1 y=1:l eqn2 end value = num2strn eqn2 disp value end

currently problem facing value or output of eqn2 replaced after each cycle until x reaches 10. hence, workspace table of eqn2 , value shows lastly value. intention document output values of value in every cycle of x 1:10.

how can this?

you pseudo-coded little taste - have tried reconstruct trying do. if understood correctly, should address question (store intermediate results calculation in array z):

l=100 z = zeros(l,10); x=1:10 % perform calculations eqn1 y=1:l % perform more calculations; not clear whether result of % loop on y=1:l yields 1 value, or l. going assume l values z(y, x) = eqn2(x, y) end value =num2strn eqn2 disp value end % have value of each evaluation of innermost loop available. can plot follows: figure; plot( x, z); % multiple plots mutual x parameter; may need utilize z' (transpose)... title 'this plot'; xlabel 'this x axis'; ylabel 'this y axis';

as other questions asked in comments, findd inspiration in following:

l = 100; nx = 20; ny = 99; % choosing how many x , y values test z = zeros(ny, nx); % allocate space results x = linspace(0, 10, nx); % x , y don't need integers y = linspace(1, l, ny); myflag = 0; % flag can used breaking out of both loops xi = 1:nx % xi , yi integers yi = 1:ny % evaluate "some function" of x(xi) , y(yi) % note these not constrained integers z(yi, xi) = (x(xi)-4).^2 + 3*(y(yi)-5).^2+2; % break status asking if z(yi, xi) < 5 fprintf(1, 'z less 5 x=%.1f , y=%.1f\n', x(xi), y(yi)); myflag = 1; % set flag break out of both loops break end end if myflag==1, break; end % break out of outer loop end

this may not had in mind - cannot understand "run loop untill values of z(y,x) <5 , should output x". if run outer loop completion (that's way know "all values of z(y,x)" value of x lastly value was... why suggesting running through values of x , y, collecting whole matrix z, , examining z things want.

for example, if wonder if there value x z < 5, (if didn't break out of for loops):

highestz = max(z, [], 1); % "take highest value of z in 1 dimension fprintf(1, 'z < 5 x = %d\n', x(highestz<5));

etc.

if can't figure out here, give up...

matlab

No comments:

Post a Comment