For the following code, It shows my 51 iterations, however i only want to display the last iteration

조회 수: 3 (최근 30일)
clc; clear all; close all;
prompt = 'What is the value of the relative pipe roughness, e/d? ';
m = input(prompt);
prompt = 'What is the value of the Reynolds number, Re? ';
Re = input(prompt);
g=@(f) 1/sqrt(f) + 2*log10((m*1/3.71)+2.51/(Re*sqrt(f)));
xLeft = 0.008; % lower limit x value initial guess
xRight = 0.08; % upper limit x value initial guess
fLeft = g(xLeft); % function of xLeft
fRight = g(xRight); % function of xRight
nIteration = 0;
if (fLeft*fRight>0)
error('The fLeft and fRight value should have different signs');
end
for i = 0:50
err = abs((xLeft-xRight)/xLeft)*100;
xAvg = (xLeft+xRight)/2;
fAvg = g(xAvg);
if (fAvg*fLeft>0)
xLeft = xAvg;
fLeft = fAvg;
else
xRight = xAvg;
fRight = fAvg;
end
nIteration = nIteration+1;
disp(['Error as percentage: ',num2str(err)]);
disp(['friction factor: ', num2str(xLeft)]);
disp(['Solution reached in ',num2str(nIteration),' iterations']);
end

답변 (1개)

the cyclist
the cyclist 2021년 3월 4일
Looks like you could just pull these lines out, and put them after the end statement of the loop:
disp(['Error as percentage: ',num2str(err)]);
disp(['friction factor: ', num2str(xLeft)]);
disp(['Solution reached in ',num2str(nIteration),' iterations']);
  댓글 수: 7
the cyclist
the cyclist 2021년 3월 4일
Running your code right here, and I see only the last iteration's results displayed. Not sure why you have something else.
clc; clear all; close all;
% prompt = 'What is the value of the relative pipe roughness, e/d? ';
% m = input(prompt);
m = 0.002;
% prompt = 'What is the value of the Reynolds number, Re? ';
% Re = input(prompt);
Re = 2e6;
g=@(f) 1/sqrt(f) + 2*log10((m*1/3.71)+2.51/(Re*sqrt(f)));
xLeft = 0.008; % lower limit x value initial guess
xRight = 0.08; % upper limit x value initial guess
fLeft = g(xLeft); % function of xLeft
fRight = g(xRight); % function of xRight
nIteration = 0;
if (fLeft*fRight>0)
error('The fLeft and fRight value should have different signs');
end
for i = 0:50
err = abs((xLeft-xRight)/xLeft)*100;
xAvg = (xLeft+xRight)/2;
fAvg = g(xAvg);
if (fAvg*fLeft>0)
xLeft = xAvg;
fLeft = fAvg;
else
xRight = xAvg;
fRight = fAvg;
end
nIteration = nIteration+1;
end
% These lines are now outside the for loop
disp(['Error as percentage: ',num2str(err)]);
Error as percentage: 2.6577e-13
disp(['friction factor: ', num2str(xLeft)]);
friction factor: 0.023498
disp(['Solution reached in ',num2str(nIteration),' iterations']);
Solution reached in 51 iterations
Ben Spurr
Ben Spurr 2021년 3월 4일
I have just adjusted my code and the output gives just the values i want. It seems that the problem was that the disp functions should be after the initial for loop closes. Thanks for the help and looks good now!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by