Here is my solution to the root finding homework we had last week.
a = 1 b = 2 pa = a**4-3*a**2 + 2*a-1 pb = b**4-3*b**2 + 2*b-1 while b - a > 0.0001: c = (a + b)/2 pc = c**4-3*c**2 + 2*c-1 if pa > 0 and pc > 0: a = c pa = pc elif pa < 0 and pc < 0: a = c pa = pc else: b = c pb = pc print("The root is between ",a," and ",b)
Note that since we had not seen functions at that point in the course I had to compute the values p(a), p(b), and p(c) without using a function.
My solution looks very similar to solutions that many of you submitted for the homework.
For any given problem there are likely to be many different solutions that work. Below is an alternative solution that is much shorter. This solution takes advantage of some special characteristics of the problem.
a = 1 b = 2 while b - a > 0.0001: c = (a + b)/2 pc = c**4-3*c**2 + 2*c-1 if pc < 0: a = c else: b = c print("The root is between ",a," and ",b)
Here are some observations about what is going on in this problem:
Now that we know how to write functions in Python, it is natural to rewrite this program to use a function. I am also going to take this opportunity to show yet another strategy for figuring out whether to set a = c or b = c.
def p(x): return x**4 - 3*x**2 + 2*x - 1 a = 1 b = 2 while b - a > 0.0001: c = (a + b)/2 if p(a)*p(c) > 0: a = c else: b = c print("The root is between ",a," and ",b)
The logic in this solution makes use of the observation that if p(a) and p(c) have the same sign then their product will be positive.
Since we have successfully implemented the algorithm for finding a root, the final step is to construct a function that packages this solution up so we can use it in other programs as a way to find a root of a function.
def bisection(f,a,b,tolerance = 0.0001): """Finds a root of f(x) for x between a and b. Note that a must be less than b, and f(a) and f(b) must have opposite signs for this to work correctly.""" while b - a > tolerance: c = (a + b)/2 if f(a)*f(c) > 0: a = c else: b = c return (a+b)/2 def p(x): return x**4-3*x**2 + 2*x-1 print("The root is approximately ",bisection(p,1,2))
Here are some things to note about this solution.