I am having a problem to run this code. It may not seem stopped when run. What's the solution?

1 次查看(过去 30 天)
g = @(f) 1/sqrt(f) + 2 * log10((RR)/3.7 + 2.51/(Re * sqrt(f)));
x_lower=0.008;
x_upper=0.0001;
x_mid=(x_lower* x_upper)/2;
while abs (g(x_mid))>0.0001
if (g(x_mid))<0
x_lower=x_mid
elseif (g(x_mid))>0
x_upper=x_mid
else
x_upper=x_lower
fprintf('%f', x_mid)
return
end
x_mid=(x_lower+x_upper)/2;
end
disp(x_mid)
  2 个评论
Voss
Voss 2022-2-12
What are the values of RR and Re?
And are you sure there is a zero in the region you are looking for it (knowing RR and Re will tell us this)?
Also, is this a typo or do you mean to have x_mid be half the product of x_lower and x_upper?
x_mid=(x_lower* x_upper)/2;
Later x_mid is the arithmetic mean (half the sum):
x_mid=(x_lower+x_upper)/2;
Also, it may not really matter because it's just names, but you have x_lower > x_upper. Is this intentional?
Akash Talapatra
Akash Talapatra 2022-2-12
The vaules of RR and Re are 0.005 and 30000. And yes, you identified my problems here in this code and really solved my problem. Your help is highly appreciable.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2022-2-12
Unrecognized function or variable 'RR'.
Error in test7>@(f)1/sqrt(f)+2*log10((RR)/3.7+2.51/(Re*sqrt(f))) (line 13)
g = @(f) 1/sqrt(f) + 2 * log10((RR)/3.7 + 2.51/(Re * sqrt(f)));
Error in test7 (line 17)
while abs (g(x_mid))>0.0001
So the fix is that you have to define RR. Use a failsafe to limit the number of iterations. Use break instead of return. You also need to use ismembertol() to see if your x_mid is close enough to your prior/last value (since you can't check equality with ==).
Possible fix:
% Demo by Image Analyst
% Initialization Steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
% Define variables
RR = 10
Re = 9
maxIterations = 1000; % Failsafe. Way more iterations than you ever expect.
loopCounter = 0; % Failsafe. Way more iterations than you ever expect.
g = @(f) 1/sqrt(f) + 2 * log10((RR)/3.7 + 2.51/(Re * sqrt(f)));
x_lower=0.008;
x_upper=0.0001;
x_mid=(x_lower* x_upper)/2;
% Bisection search:
while (abs(g(x_mid)) > 0.0001) && (loopCounter < maxIterations)
if (g(x_mid)) < 0
x_lower=x_mid;
fprintf('%d, case 1: Original x_mid = %.9f, x_lower = %.9f, x_upper = %.9f, ', loopCounter, x_mid, x_lower, x_upper)
elseif (g(x_mid))>0
x_upper=x_mid;
fprintf('%d, case 2: Original x_mid = %.9f, x_lower = %.9f, x_upper = %.9f, ', loopCounter, x_mid, x_lower, x_upper)
else
x_upper=x_lower;
fprintf('%d, case 3: Original x_mid = %.9f, x_lower = %.9f, x_upper = %.9f, ', loopCounter, x_mid, x_lower, x_upper)
break
end
% Capture old value for comparison.
x_mid_Prior = x_mid;
% Create new value.
x_mid=(x_lower+x_upper)/2;
fprintf(' final x_mid = %.9f\n', x_mid);
loopCounter = loopCounter + 1;
% Break out if the value is not changing much.
if ismembertol(x_mid, x_mid_Prior, 1e-7)
fprintf('Success! Found a solution at iteration %d with x_mid = %.9f.\n', loopCounter, x_mid)
break;
end
end
if loopCounter == maxIterations
% If you bailed out because of the failsafe, you get here.
warningMessage = sprintf('WARNING: loop exited after %d iterations without finding a solution.', loopCounter)
uiwait(warndlg(warningMessage))
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Kernel Creation from MATLAB Code 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by