False Position method. How many itinerations should it take to find the root?
Show older comments
Hi! I am programming the false position method. However, with 100 itinerations, my program cannot find a root with an error smaller than 10e-10 nor 10e-6.
Is this normal, or is it just that my program that does not work properly?
Find attached the code.
Hope you can help me!
2 Comments
Please post the code, instead of posting the picture of the code.
From the looks of it, it seems 100 iterations are not enough to find a root with that tolerance.
Also note the different between
10e-10
1e-10 %10^x is equal to 1ex
llucia
on 6 Apr 2023
Answers (1)
Dyuman Joshi
on 6 Apr 2023
Edited: Dyuman Joshi
on 6 Apr 2023
A correction is needed -
while n<maxits && error(n+1)>eps_x && abs(f(x0))>eps_f
%^here
If any of the condition is met, exit the loop
%I'm running it as a script with some changes
f = @(x) x^3 + 2*x^2 + 10*x - 20;
x0=0;x1=1;
error=abs(x1-x0);
n=0;
while error > 1e-10 && abs(f(x0)) > 1e-10
a=f(x0);b=f(x1);
x = (x0*b-x1*a)/(b-a);
if a*b<0
x1=x;
else
x0=x;
end
error=abs(x1-x0);
n=n+1;
end
n
format long
x
f(x)
2 Comments
A correction is needed -
while n<maxits && error(n+1)>eps_x && abs(f(x0))>eps_f
Why ? I found the first version quite plausible. As long as not both conditions are met, the root is questionable and should be improved. And the code works as posted.
f = @(x)x.^3+2*x.^2 + 10*x -20;
a = 0;
b = 1;
eps_x = 1e-10;
eps_f = 1e-10;
maxits = 100;
[x n error] = RegulaFalsa1(f,a,b,eps_x,eps_f,maxits)
function [x,n,error] = RegulaFalsa1(f,a,b,eps_x,eps_f,maxits)
x0 = a;
x1 = b;
n = 0;
error(1) = abs(b-a);
while n < maxits && (error(n+1) > eps_x || abs(f(x0)) > eps_f)
x = (x0*f(x1)-x1*f(x0))/(f(x1)-f(x0));
if f(x0)*f(x1) < 0
x1 = x;
else
x0 = x;
end
n = n+1;
error(n+1) = abs(x1-x0);
end
end
llucia
on 6 Apr 2023
Categories
Find more on Spline Postprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!