Need help with this bisection method code!
Show older comments
Im studying for a math test and on a old test there is a task about bisection. The task is to solve x^2=2 with the bisection method and the precision should be with 10 decimals. The problem is that it seems like the teachers recommended solution to the task isn't quite right. Here's the code:
a=1;
b=2;
tol=1e-10;
fa=a*a-2;
fb=b*b-2;
while b-a>tol
c=(a+b)/2;
fc=c+c-2;
if fa*fc<0
b=c;
fb=fc;
elseif fb*fc<0
a=c;
fa=fc;
else
break
end
end
x=c
I keep getting the answer x= 1.000000000087311
What am I doing wrong, have been stuck with this problem for a while now so I might have missed some obvious misstakes
/Johan
Accepted Answer
More Answers (0)
Categories
Find more on Entering Commands 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!