フィルターのクリア

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.

1 回表示 (過去 30 日間)
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 件のコメント
Image Analyst
Image Analyst 2019 年 1 月 6 日
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.
Damian Sztangierski
Damian Sztangierski 2019 年 1 月 6 日
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.

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by