How can I force a for loop to finish all iterations without using 'end'? I want my for loop to finish before moving onto line 11 otherwise some values will be missing.

h = 0.01; %step size
A = 8000/11; %constant value
T(1)=80; %initial temperature
x(1)=0; % length along rod, 0cm to 4cm
prompt = ('Guess initial Z value');
Z(1) = input(prompt); % initial Z value (guessed) used to obtain T values
for i=2:5; % loop that should produce 5 T values and 5 Z values corresponding to different points along the rod
x(i) = x(i-1)+h;
T(i) = T(i-1) + h*Z(i-1)
Z(i) = Z(i-1) + h*(A*(T(i-1)-20));
if (T(5)-(20-(55/20)*Z(5))) < 0.1 % if initial guess is correct then a plot should be produced
plot(x,T)
break
else % if initial guess is incorrect then a new guess must generated using equation below
Z1=Z(1)
Z(1) = Z1 - Z(5) % loop needs to continue until plot is produced
end
end
% My code does not complete iterations 2:5 to get all values including T(5) and Z(5), instead it completes one iteration and moves onto the if statement which results in an error because T(5) AND Z(5) are missing.
% Is there a way to force the loop to finish completely each time before moving onto the IF statement?

7 Comments

It’s because I only want a plot if the condition is met, which will only happen once the correct initial Z value is calculated through iteration. I want my code to complete the 5 iterations before moving on to the if statement.
If you want to complete the loop, why would you put a break in there? What is the flow of your algorithm? We have no way of understanding you code, because you don't explain your goal, nor do you use comments, nor descriptive variable names. Any of those three might help us help you.
My bad, I will include comments next to the code to make it easier to understand. And, I used a break to stop the loop because once I have a plot I have achieved my goal.
How should you test that the initial guess is correct? At the moment you have an initial guess, the Z vector is only 1 element long. Or did you mean to use Z(end) in your test?
The first time through, how is there any Z at all in index 5? Since you only input 1 values when you try to access Z(5) in the if statement, it should throw an error because there is no Z(5) yet. What are you entering for Z?
I think you should use clearvars
>> clearvars
because I think your code maybe using old values of Z, with higher indexes, that are still hanging around.
Tell us what you are entering at the prompt for Z(1). I assume it's just one number, but maybe not.
In truth, my initial guess has very low chance of being correct hence why i want to use fixed point iteration ( new guess = previous guess - previous(Z(5)) so that the ‘initial guess’ gets better each time around.
Also, @imageanalyst , you are correct to say there is no Z in index 5, I get an error because the loop has not had the chance to generate Z(5) yet, i do not know how to do that which is my problem. As for Z(1), i input a random guess, i need my code to generate better guesses that can be used as the initial input for the next round in the loop.

Sign in to comment.

Answers (0)

Products

Release

R2017b

Community Treasure Hunt

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

Start Hunting!