You will write the bisection method. The program tests for a sign change, halves an interval again and again, and counts how many halvings a given accuracy needs.
The idea. If is continuous and and have opposite signs, a root lies between and . Replace the interval by the half where the sign still changes. After halvings the interval has length .
Your tasks.
sign_change(f, a, b). Return True when f(a) and f(b) have opposite signs.bisect(f, a, b, steps). Halve the interval steps times, keeping the half with the sign change, and return the midpoint of the final interval.steps_needed(width, tol). Return the smallest whole number n with width / 2 ** n < tol.Have you tried it?
Do the tasks in the editor first, and run the checker after each one. Its hints help when a check fails. When you are ready, compare your code with this solution.
Log in to join the discussion.
Loading…