False Position method. How many itinerations should it take to find the root?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Dyuman Joshi
le 6 Avr 2023
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
Réponses (1)
Dyuman Joshi
le 6 Avr 2023
Modifié(e) : Dyuman Joshi
le 6 Avr 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 commentaires
Torsten
le 6 Avr 2023
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
Voir également
Catégories
En savoir plus sur Spline Postprocessing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!