Iteration using while loop
4 次查看(过去 30 天)
显示 更早的评论
Hi i am struggling with the question below, can someone help please?
For pressure drop in pipe flow the pipe friction factor, Cf, is an important parameter. To use software to solve problems graphical forms relating Re to Cf are not that helpful. Coulson & Richardson Vol 1 give some equations relating Re to Cf, some of them are implicit (rather than explicit)
THE EQUATION ------ Sigma^(-0.5)=2.5*ln(Re*(Sigma^0.5))+0.3
The equation is implicit as it cannot be rearranged to get Φ = f(Re). However if we can get the equation in a form Φ = f(Re, Φ), then can try to find Φ by a substitution and iteration method (basically guessing the answer).
Procedure (i) manipulate the eqn to get Φ = f(Re, Φ) - SYMBOL IS SIGMA
(ii) guess a value of Φ, Φi
(iii) the next guess of Φ, Φi+1= f(Re, Φi)
(iv) repeat until Φi+1= Φi to a desired accuracy
Ex 6.3 For a Reynolds Number of 10000, what is the value of Φ?
Hints:
manipulate equation (3) into the form required by (i) above and create a function file to describe it. NB Matlab uses ‘log’ for the natural logarithm (not ‘ln’) and ‘log10’ for base 10 logarithms (not ‘log’).
you will need to think of a logic statement to control your loop – it is probably easiest to use a ‘while’ loop, an adequate desired accuracy is 1e-8 (1 x 10-8).
0 个评论
回答(1 个)
Walter Roberson
2022-3-13
Example:
error = -inf;
x = -1;
while error < 0
x = x + 0.1
error = x.^3 - 3.*x.^2 + 1
end
That is, you initialize your error, and while your error is outside the bounds you want. You change your trial value somehow (the way to determine the new value is important!!), and you update your estimate of the error.
3 个评论
Walter Roberson
2022-3-13
The question is about how to do iteration using a while loop, and I show how to do iteration using a while loop.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!