Effacer les filtres
Effacer les filtres

converging criteria problem in for loop in bisection method of finding roots.

3 vues (au cours des 30 derniers jours)
MD
MD le 5 Déc 2023
Commenté : Sam Chak le 6 Déc 2023
problem: finding roots of the polynomial x3-x-11 by using bisection method.
Code:
close all
clear all
y = @(x) (x.^3)-x-11;
fplot(y);
grid on
xlo = input('Please enter the initial lower limit of x');
xuo = input('Please enter the initial upper limit of x');
x = (xlo+xuo)./2; % 1st approximation for finding root
for i = 1:1:1000
f = y(x); % function value for approximation of x
if f<0
xlo = x;
xuo = xuo;
elseif f>0
xlo = xlo;
xuo = x;
end
x(i) = (xlo+xuo)./2;
if abs(x-x(i))<=0.0001
break
else
x = x(i);
end
end
disp(i);
disp(x);
Comments: the converging criteria if abs(x-x(i))<=0.0001
break
else
x = x(i); stucks. why i can't find out. if i use == sign instead of <= then whole loop run and i get the desires root. but when i use <= this loop stucks. Please can anyone help me out.
Thanks in advance.

Réponse acceptée

Sam Chak
Sam Chak le 5 Déc 2023
Hi @MD
You can change the for-loop to a while-loop and x(i) to xnew. Also, insert the initial count. The new search interval criteria remains unchanged.
y = @(x) (x.^3) - x - 11;
fplot(y, [1 3])
grid on, xlabel x, ylabel y
xlo = 2.0;
xuo = 2.5;
x = (xlo + xuo)/2; % 1st approximation for finding root
count = 0; % initial iteration count
while true % can also use count < 1000 to break the loop
f = y(x); % function value for approximation of x
if f < 0
xlo = x;
xuo = xuo;
elseif f > 0
xlo = xlo;
xuo = x;
end
xnew = (xlo + xuo)/2;
count = count + 1; % increase count by 1
if abs(x - xnew) <= 0.0001
break
else
x = xnew;
end
end
disp(count); % change i to display count
12
disp(x);
2.3737
  2 commentaires
MD
MD le 6 Déc 2023
Modifié(e) : Sam Chak le 6 Déc 2023
Thank you Very much for your answer. I have also solved the problem by sligth modification. Please check this out:
close all
clear all
% Finding normal polynomial roots of single variable by using bisection method.
y = @(x) (x.^3)-x-11;
fplot(y);
grid on
xlo = 2.0;
xuo = 2.5;
for i = 1:1:1000
x(i) = (xlo+xuo)./2; % 1st approximation for finding root
f(i) = y(x(i)); % function value for approximation of x
if f(i)<0
xlo = x(i);
xuo = xuo;
elseif f(i)>0
xlo = xlo;
xuo = x(i);
end
x(i+1) = (xlo+xuo)./2;
if abs(x(i+1)-x(i))<=0.00001
break
else
x(i) = x(i+1);
end
end
disp(i);
15
disp(x(:,length(x)));
2.3736
Wish you all the best.
Sam Chak
Sam Chak le 6 Déc 2023
Yup @MD, your updated code for the bisection root-finding method works for the specific problem.
I also tested two other problems below, and one works, but the other doesn't.
close all
clear all
% Finding normal polynomial roots of single variable by using bisection method.
%% Test 1
% y = @(x) (x + 0.5).^2; % <-- this one works
%% Test 2
y = @(x) (x - 0.5).^2; % <-- this one does not
fplot(y, [-1 1]);
grid on
xlo = -1.0;
xuo = 1.0;
for i = 1:1:1000
x(i) = (xlo+xuo)./2; % 1st approximation for finding root
f(i) = y(x(i)); % function value for approximation of x
if f(i)<0
xlo = x(i);
xuo = xuo;
elseif f(i)>0
xlo = xlo;
xuo = x(i);
end
x(i+1) = (xlo+xuo)./2;
if abs(x(i+1)-x(i))<=0.00001
break
else
x(i) = x(i+1);
end
end
disp(i);
17
disp(x(:,length(x)));
-1.0000

Connectez-vous pour commenter.

Plus de réponses (2)

Walter Roberson
Walter Roberson le 5 Déc 2023
x = (xlo+xuo)./2; % 1st approximation for finding root
x is a scalar at that point -- so x(1) is the only defined element.
for i = 1:1:1000
for loop. i starts at 1, then next iteration will be 2, and so on.
x(i) = (xlo+xuo)./2;
before that, x is a scalar. But here you store into x(i), which on the second iteration would be x(2), third iteration would be x(3) and so on. So afterwards x will be a vector with x(1) having the old value, then a bunch of zeros, and then x(i) having the new value.
if abs(x-x(i))<=0.0001
For i > 1 then x has become a vector. You take the vector minus the most recent result, x(i) . You get a vector of resullts. You abs() that getting a vector of results. You compare the vector of results to 0.0001 . But when you ask if to process a non-scalar value, if considers the test to pass only if all of the values being tested are non-zero (that is, all of the values must be true). If even one entry in the vector did not satisfy abs(x-x(i))<=0.0001 then the if fails.
else
x = x(i);
and when the if fails, you replace the vector x (of length i) with the scalar x(i) . So the next iteration, x is back to being a scalar again...
  1 commentaire
MD
MD le 6 Déc 2023
Thank you for your academic brief but to the point explanation. My best wishes. Here i solved by slight modification:
close all
clear all
% Finding normal polynomial roots of single variable by using bisection
% method.
y = @(x) (x.^3)-x-11;
fplot(y);
grid on
xlo = input('Please enter the initial lower limit of x');
xuo = input('Please enter the initial upper limit of x');
for i = 1:1:1000
x(i) = (xlo+xuo)./2; % 1st approximation for finding root
f(i) = y(x(i)); % function value for approximation of x
if f(i)<0
xlo = x(i);
xuo = xuo;
elseif f(i)>0
xlo = xlo;
xuo = x(i);
end
x(i+1) = (xlo+xuo)./2;
if abs(x(i+1)-x(i))<=0.00001
break
else
x(i) = x(i+1);
end
end
disp(i);
disp(x(:,length(x)));

Connectez-vous pour commenter.


MD
MD le 6 Déc 2023
close all
clear all
% Finding normal polynomial roots of single variable by using bisection
% method.
y = @(x) (x.^3)-x-11;
fplot(y);
grid on
xlo = input('Please enter the initial lower limit of x');
xuo = input('Please enter the initial upper limit of x');
for i = 1:1:1000
x(i) = (xlo+xuo)./2; % 1st approximation for finding root
f(i) = y(x(i)); % function value for approximation of x
if f(i)<0
xlo = x(i);
xuo = xuo;
elseif f(i)>0
xlo = xlo;
xuo = x(i);
end
x(i+1) = (xlo+xuo)./2;
if abs(x(i+1)-x(i))<=0.00001
break
else
x(i) = x(i+1);
end
end
disp(i);
disp(x(:,length(x)));

Catégories

En savoir plus sur Polynomials 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!

Translated by