Loop question in code
조회 수: 5 (최근 30일)
이전 댓글 표시
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
2011년 12월 29일
There is no such thing as an "if loop". There are for loops, while loops, and if statements.
채택된 답변
Sean de Wolski
2011년 12월 29일
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
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!