Extracting data from a loop

1 次查看(过去 30 天)
Jason
Jason 2011-3-6
(pseudocode)
function Sactual = Untitled( P,Cactual)
% Ccalculated = The calculated sand concentration from the loop % Cactual = The known sand concentration from the loop
S=0:1:10;
A loop that stops looping when Ccalculated ~= Cactual Ccalculated = S*P; end
Extract the last value calculated to get Sactual
end
Is there a way of extracting the last calculated value. So for example if P=2 and Cactual = 6 the value of Sactual should be 3.
Also is there a loop that stops looping when Ccalculated ~= Cactual? I imagine using the while function only stops for one loop and keep iterating the rest
  1 个评论
Jan
Jan 2011-3-6
Please reformat your pseudo code. It would be a very good idea to post Matlab code instead.

请先登录,再进行评论。

回答(4 个)

Jan
Jan 2011-3-6
Getting the last calculated value is trivial: Ccalculated is this value already.
If the inner loop should stop, when Ccalulated differs from Cactual, there is no reason to stop the outer loop.
Summary: I do not understand, what you are trying to achieve. Please formulate the question with more details.
  1 个评论
Paulo Silva
Paulo Silva 2011-3-6
neither do I but what can we do when people don't take their time to think about what they are asking, at least Jason tried to come up with pseudo pseudo code lol
there are people that don't even explain what they want!

请先登录,再进行评论。


Jan
Jan 2011-3-6
Or perhaps:
function Sactual = Untitled(P, Cactual)
Sactual = find((0:10) * P == Cactual) - 1;
Actually I do not see a need for a loop at all. Even "round(Cactual / P) - 1" might be a valid solution.
But let's wait and see if Jason can enlighten us.

Paulo Silva
Paulo Silva 2011-3-6
function Sactual = Untitled(P,Cactual)
for Sactual=0:10
Ccalculated=S*P;
if Ccalculated~=Cactual, break,end
end

Jason
Jason 2011-3-6
Hey sorry I didn't really explain what I'm doing with much clarity. This is an overview of the problem I'm trying to solve. This equation finds the minimum stream power of a river. 1. Assume a value for D. 2. Work out V from V=Q/(W*D) (values Q,W are inputted) 3. Solve S=(w*Cts*10^(I/J))/(V-Vcr) Where: I = 5.435-0.286*log10(w*d/v)-0.457*log10(Us/w) J = 1.799-0.409*log10(w*d/v)-0.314*log10(Us/w)
Us = (9.81*R*S)^0.5;
if Us*d/v is between 1.2 and 70
Vcr=w*((2.5/(log10(Us*d/v)-0.06))+0.66)
Otherwise
Vcr=w*2.05
So Us and as a result I and J depend on S
4. Select another D and repeat the steps 5. Compare computed V*S values and find a minimum value
So I have two equations and three unknowns. I thought the best way of solving this is by rearranging the equation in point no.3 to have Cts = function(...) instead of S = function(...). Then iterate to find S when the computed Cts equals the Cts inputted at the start of the function. Cts=Cactual. The inputs would be Cactual,Cts,R,d,v,w,Q,W,D
I'm not very good at explaining things but I hope this sheds some light on the situation. Thanks for the help you've already given me :)
  1 个评论
Paulo Silva
Paulo Silva 2011-3-6
That almost doesn't seem related to the original question!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by