You will write a small solver that finds every solution of an equation on an interval by scanning for sign changes. It finds only real crossings, so a false root of the algebra never appears. The checker tests it on three equations of this course.
The idea. Write the equation as , where is the left side minus the right side. A solution is where changes sign, so scan the interval on a grid and look at each pair of neighbours. Then narrow down the root by halving the interval again and again.
Your tasks.
sign_changes(f, xs). Count the neighbouring pairs of points in the array xs where f changes sign.refine(f, a, b). Halve the interval times, always keeping the half where the sign changes. Return the middle.solve_all(f, lo, hi). Use points from lo to hi. Return the list of roots, one for each sign change, found by refine.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…
With Standard, you also open 12 more problems, 2 Python labs and 1 extra course. Everything you have used here stays free, always.