converging criteria problem in for loop in bisection method of finding roots.
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Akzeptierte Antwort
Sam Chak
am 5 Dez. 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
disp(x);
2 Kommentare
Sam Chak
am 6 Dez. 2023
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);
disp(x(:,length(x)));
Weitere Antworten (2)
Walter Roberson
am 5 Dez. 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...
Siehe auch
Kategorien
Mehr zu Polynomials finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!