Loop question in code

I have a code which runs a certain number of calculations and then displays the answer.
However what I want to do is tell the code to check with the vector created is a certain size, and if this check fails it goes back to a certain point in the code, adding a number to an initial variable and starting again.
I was wondering if there was a way to do this with something like a 'return' function
I don't think if or for loops are what I want
I have included my code as an example
%%Calculation to assess water temperature around the plant
C_pd = Specific_Heat(T_d,X_d);
C_pb = Specific_Heat(T_b,X_b);
e = 0.01;
f = 0.001;
k = 60; % Initial Minimum Value of T_f
l = 70; % Initial Maximum Value of T_F
m = 30; % Initial Minimum Value of T_o
n = 40; % Initial Maximum Value of T_f
i = 1;
Time1 = clock;
w = 3.6; % Error Factor
Return to here
for z = 1:3
g = 1/10^w;
for T_f = k:e:l
C_pf = Specific_Heat(T_f,X_f);
a = M_f*C_pf*(T_f-T_cw);
for T_o = m:f:n
b = M_d*C_pd*(T_d-T_o) + M_b*C_pb*(T_b-T_o);
if abs(a-b)<g
T(1,i) = T_f;
T(2,i) = T_o;
i=i+1;
end
end
end
k = min(T(1,:));
l = max(T(1,:));
m = min(T(2,:));
n = max(T(2,:));
e=e/10;
f = f/10;
w=w+1;
end
Time2 = clock;
size(T);
if size(T) == [2 1];
disp('T_f (68) = ')
disp(T(1,:))
disp('T_o (36) = ')
disp(T(2,:))
if T(1,:) == 68 % Error check
else
T_fdiff = T(1,:) - 68
end
if T(2,:) == 36
else
T_odiff = T(2,:) - 36
end
else
disp(size(T))
w = w + 0.01;
Loop from here
end
clock = etime(Time2,Time1)
Any thoughts?
Thanks in advance

1 件のコメント

Walter Roberson
Walter Roberson 2011 年 12 月 29 日
There is no such thing as an "if loop". There are for loops, while loops, and if statements.

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

 採用された回答

Sean de Wolski
Sean de Wolski 2011 年 12 月 29 日

0 投票

No MATLAB does not have a goto command. This is a good place to start:
What do you want by loop from here. What do you want the code to do? Since it looks like you're dealing with clock, perhaps a timer is a good place to start. Timers allow you to execute code repeatedly, at fixed intervals, n number of times.
doc timer

3 件のコメント

Thomas Humphrey
Thomas Humphrey 2011 年 12 月 29 日
I don't think I can restructure my code to include this. Unless I put the whole thing inside an IF loop which I don't think would be very good.
What i'm trying to do is an iteration of an equation that has 3 variables. This is done by splitting the equation into two separate equations and when they equal each other, the code reports to an array.
I then want to check this array has only 1 answer for the variables in it (as the code starts off with thousands) and then if it only has 1 answer the code displays the answer, if not it changes the input error conditional and retries the code again until only 1 answer is left.
at the moment if the code fails I am having to redo my input error conditionals until it works
Sean de Wolski
Sean de Wolski 2011 年 12 月 29 日
So put all of the code running the engine in a function and use a while-loop in another function (or script) to run the engine with varying inputs each time. The inputs can be calculated from either outputs or preset values.
Thomas Humphrey
Thomas Humphrey 2011 年 12 月 29 日
Good idea, Is there not a simpler way though? Guess not.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by