Loop question in code
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaire
Walter Roberson
le 29 Déc 2011
There is no such thing as an "if loop". There are for loops, while loops, and if statements.
Réponse acceptée
Sean de Wolski
le 29 Déc 2011
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 commentaires
Sean de Wolski
le 29 Déc 2011
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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!