Regula-falsi Method that keeps running

10 views (last 30 days)
BG
BG on 19 Mar 2023
Commented: BG on 19 Mar 2023
I have this script for Regula-Falsi Method to get a root of a function but it just keeps running. Anyone can help find what's wrong?
f = @(x) 2.^x - x.^2 - 2; % Function whose roots are desired
xL = 4; xU = 5; % Initial guesses
Tol = 1e-6; % Tolerance (0.000001)
c = 0; % Counter for the no. of iterations
while abs( xU-xL ) > Tol
c = c + 1
xm = xU-f(xU)* (xU-xL)/f(xU)- f(xL); % Compute xm
fm = f(xm); % Evaluate the function at x = xm
% % Print the intermediate results
fprintf('Iteration: %d\t',c);
fprintf ('[xL,xU,xm ] = [%.6f, %.6f, %.6f]\n', xL,xU,xm);
if f(xL ) *fm < 0 % If the root is on the left,
xU = xm ; % then set xm as the new xU.
elseif fm * f( xU ) < 0 % But if the root is on the
xL = xm ; % then set xm as the new xL.
else
break; % Stop! xm is exactly the root
end
end
plot(xm,f(xm),'r*', 'Markersize', 10); % plot the xm points
fprintf ('The root is: %.6f \n', xm);

Accepted Answer

Alan Stevens
Alan Stevens on 19 Mar 2023
Brackets!!
f = @(x) 2.^x - x.^2 - 2; % Function whose roots are desired
xL = 4; xU = 5; % Initial guesses
Tol = 1e-6; % Tolerance (0.000001)
c = 0; % Counter for the no. of iterations
while abs( xU-xL ) > Tol
c = c + 1;
xm = xU-f(xU)* (xU-xL)/(f(xU)- f(xL)); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Put brackets round f(xU)-f(xL)
fm = f(xm); % Evaluate the function at x = xm
% % Print the intermediate results
fprintf('Iteration: %d\t',c);
fprintf ('[xL,xU,xm ] = [%.6f, %.6f, %.6f]\n', xL,xU,xm);
if f(xL ) *fm < 0 % If the root is on the left,
xU = xm ; % then set xm as the new xU.
elseif fm * f( xU ) < 0 % But if the root is on the
xL = xm ; % then set xm as the new xL.
else
break; % Stop! xm is exactly the root
end
end
Iteration: 1
[xL,xU,xm ] = [4.000000, 5.000000, 4.285714]
Iteration: 2
[xL,xU,xm ] = [4.285714, 5.000000, 4.390866]
Iteration: 3
[xL,xU,xm ] = [4.390866, 5.000000, 4.425434]
Iteration: 4
[xL,xU,xm ] = [4.425434, 5.000000, 4.436351]
Iteration: 5
[xL,xU,xm ] = [4.436351, 5.000000, 4.439754]
Iteration: 6
[xL,xU,xm ] = [4.439754, 5.000000, 4.440811]
Iteration: 7
[xL,xU,xm ] = [4.440811, 5.000000, 4.441138]
Iteration: 8
[xL,xU,xm ] = [4.441138, 5.000000, 4.441240]
Iteration: 9
[xL,xU,xm ] = [4.441240, 5.000000, 4.441271]
Iteration: 10
[xL,xU,xm ] = [4.441271, 5.000000, 4.441281]
Iteration: 11
[xL,xU,xm ] = [4.441281, 5.000000, 4.441284]
Iteration: 12
[xL,xU,xm ] = [4.441284, 5.000000, 4.441285]
Iteration: 13
[xL,xU,xm ] = [4.441285, 5.000000, 4.441285]
Iteration: 14
[xL,xU,xm ] = [4.441285, 5.000000, 4.441285]
Iteration: 15
[xL,xU,xm ] = [4.441285, 5.000000, 4.441285]
Iteration: 16
[xL,xU,xm ] = [4.441285, 5.000000, 4.441285]
Iteration: 17
[xL,xU,xm ] = [4.441285, 5.000000, 4.441285]
Iteration: 18
[xL,xU,xm ] = [4.441285, 5.000000, 4.441285]
Iteration: 19
[xL,xU,xm ] = [4.441285, 5.000000, 4.441285]
Iteration: 20
[xL,xU,xm ] = [4.441285, 5.000000, 4.441285]
Iteration: 21
[xL,xU,xm ] = [4.441285, 5.000000, 4.441285]
Iteration: 22
[xL,xU,xm ] = [4.441285, 5.000000, 4.441285]
Iteration: 23
[xL,xU,xm ] = [4.441285, 5.000000, 4.441285]
Iteration: 24
[xL,xU,xm ] = [4.441285, 5.000000, 4.441285]
Iteration: 25
[xL,xU,xm ] = [4.441285, 5.000000, 4.441285]
Iteration: 26
[xL,xU,xm ] = [4.441285, 5.000000, 4.441285]
Iteration: 27
[xL,xU,xm ] = [4.441285, 5.000000, 4.441285]
Iteration: 28
[xL,xU,xm ] = [4.441285, 5.000000, 4.441285]
Iteration: 29
[xL,xU,xm ] = [4.441285, 5.000000, 4.441285]
Iteration: 30
[xL,xU,xm ] = [4.441285, 5.000000, 4.441285]
plot(xm,f(xm),'r*', 'Markersize', 10); % plot the xm points
fprintf ('The root is: %.6f \n', xm);
The root is: 4.441285

More Answers (0)

Categories

Find more on Dialog Boxes in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!