Bisectional method Not always giving correct roots

My code sometimes does not give correct roots? Sometimes it will and other times it wont depending on the boundaries, please let me know if you guys know what is causing it.
clc
clear all
close all
f=@(x)(x.^3+7*x.^2-33*x-135);
yl = input('type approximated lower boundary:');
yu = input('type approximated upper boundary:');
err=input('Type desired approximate error limit:');
if(f(yl)*f(yu)) > 0
disp('TRY NEW BOUNDARIES');
return
%if values do not produce a negative number give better boundaries
end
while abs(yu-yl) >= err %basically how close do you want the boundaries to go
ynew=(yl+yu)/2;
if (f(yl)*f(yu) < 0)
yu=ynew;
else
yl=ynew;
end
end
fprintf('The root of this equation given your error limit is=%f', ynew);

Answers (1)

Anthony - can you provide an example (with inputs) that gives the correct solution and an example that doesn't? Also, don't you need to include the new value, ynew, in your comparison
ynew = (yl + yu) / 2;
if (f(yl) * f(ynew)) < 0
%etc.
end
rather than re-using the upper bound again as
if (f(yl)*f(yu) < 0) % incorrect?

10 Comments

Something that doesnt work:22,-30,.001.
One that works is 18,-6,.001.
I tried this after putting in the correction (I believe youre right) But it's still throwing bad answers:/)
It seems to give correct answers more often now.. Maybe it has something to do with the checker to see if you can use those boundaries. It will still display an answer when it gives of an error if you cant use those boundaries.
Anthony - I don't understand your inputs: 22,-30,.001. Isn't the first input supposed to be the lower bound on the interval and the second input the upper bound? So shouldn't the order be reversed to -30, 22, 0.001?
For your function, there seem to be roots at (at least) -9 and 5. Is this true?
As for your second input set, what happens if you try entering in the order -6, 18, 0.001?
yes you are correct, I just typed them in wrong.
Here:
-20,20,.0001 gives 9.999 (Root is actually -9,-3,5)
one that is correct is:(-600,300,.001)
Thank you
Yes, -9,-3, and 5. When I type those inputs you gave it does give an answer however my code says "Ignore this answer" I put that because im not sure how to keep it from displaying.
But it will still throw bad answers when they are within acceptable boundaries
You may also want to consider
while abs(yu-yl) >= err
Do we need another condition to determine if we are finished? Take a look at Bisection Method algorithm. What other check do they have?
ah I got it! It should be while its less than or equal to. It's working properly.
Thank you so much!!
Can you tell me why it threw off numbers? Im guessing it did an extra iteration due to it not being less than which threw it off?
Again, thank you so much
It actually needed the <= on the statement below the if statement. Otherwise it didnt work.
I think that the equality is needed to handle the case where f(ynew) is zero, like in the article with the If f(c) = 0.

Sign in to comment.

Categories

Tags

Asked:

on 17 Feb 2019

Commented:

on 17 Feb 2019

Community Treasure Hunt

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

Start Hunting!