0

I am new to MATLAB and have encountered a problem on an assignment. The assignment is to use the bisection method. My original code was:

a = -0.5;
b = 1.1;
f=@(x) x^5-2*x^4+3*x^3-2*x^2+x-1-cos(30*x);
val1 = f(a);
val2 = f(b);
p1a1 = val1*val2;
save('A1.dat','p1a1','-ascii')

%bisection

for i = 0:100
    c = (a+b)/2;
    if f(c) >= -0.000000001 && f(c) <= 0.000000001
        answer = c;
    elseif f(c) > 0
        b = c;
    else a = c;
    end
end
answer = c;
save('A2.dat','answer','-ascii')

However, I needed to count the number of iterations it took, so I changed the second part of the code (after "bisection") to:

tolerance = 0.000000001;
count = 0;
c =(a+b)/2;
while abs(f(c))>tolerance
    count=count+1;
    if f(c) > 0
        b = c;
    else
        a = c;
    end
end
answer = c;

Where c would show the zero of the function, within the prescribed tolerance. The new code won't run however, and I cannot seem to figure out where i have gone wrong. Apologies if it is something simple

3
  • 1
    to avoid potential infinite loop, stopping criterion must be |b-a|<eps. Reason: for numerically inaccurate f(.) function, function without zero or for very small eps you are not sure that |f(c)|<eps is ever fulfilled. On the other side |b-a|<eps will happen for sure. Commented Oct 19, 2018 at 19:21
  • @PicaudVincent Note that <= would probably be better than <, given that (by definition) eps is the smallest representable value. Also eps = 2.2e-16 on my machine, the tolerance used in the question should be of no concern for numerical accuracy. Commented Oct 19, 2018 at 19:58
  • @Wolfie yes sure you are right Commented Oct 19, 2018 at 20:41

1 Answer 1

1

You're not updating the value of c inside the loop, it never changes which means you're stuck in the while loop. Copy the c = (a+b)/2 definition to inside the loop, as you had it before.

c = (a+b)/2;
while abs(f(c))>tolerance
    count=count+1;        
    if f(c) > 0
        b = c;
    else
        a = c;
    end
    c = (a+b)/2; % <<<<<< This is needed to avoid an infinite loop!
end
answer = c;

The bisection method is guaranteed to converge to a root (if it exists), but you should be careful with while loops for numerical methods. In particular, add a maximum iteration counter

maxIters = 1000;
c = (a+b)/2;
while abs(f(c)) > tolerance && count < maxIters
    % Code as before ...
end
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome, thank you very much. I tried to put c in the while loop, but I put it at the top, before the "count=count+1. Now it makes sense. Also, thank you for the helpful tip about setting the max number of iterations. This will undoubtedly help with future coding!
@Pat No problem, consider marking this answer as accepted (the green tick mark under the voting buttons) if it answers your question!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.