While loop

5 views (last 30 days)
Dougie
Dougie on 3 May 2012
Hoping somebody could help me fix my while loop.
Whilst the output to a function Convergence == 0 i want the while loop to continue to iterate, producing an output matrix R_2, and also updating the run_number to run_number + 1. It's not necessary to store the previous matrices, if it's faster to run then the matrices could be replaced.
Here's what i have so far.
R_2 = %%not sure what goes here.
Conv = 0; %initially convergence criteria = 0
run_number = 1;
R_2{run_number} = R_2;
while Conv == 0
R_1 = %%%%function producing R_!;
Radius = %%function producing
R_2{run_number} = %function of R1 and Radius
Conv{run_number} = Conv(R_2{run_number});
run_number = run_number+1;
end
Thanks a lot

Answers (1)

Walter Roberson
Walter Roberson on 3 May 2012
Conv{1} = 0; %instead of Conv = 0;
while Conv{end} == 0
and use
Conv{run_number} = Conv{R_2{run_number}}; %instead of Conv()
However, please examine this statement more carefully:
Conv{run_number} = Conv{R_2{run_number}};
On the first iteration, Conv{1} is the only thing that exists, so if R_2{1} is not exactly 1 then you would be accessing out of bounds. So R_2{1} must be 1, and you are then setting Conv{1} = Conv{1} and that was initialized to 0. Second iteration, Conv{1} is still the only thing that exists, so R_2{2} must be 1, and you are setting Conv{2} = Conv{1} which is 0. Third iteration, the existing Conv{1}=0 and Conv{2}=0 are what exists, so Conv{3}=0. By induction, you are going to continue the loop with 0's until you run out of memory.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!