Let's start by finding the roots of a polynomial.
g(x) = expand((x^2 - 1)^3 * (x^2 + 1) * (x - 2)); g.show() print("Root at x = {0}".format(g.find_root(-2,2))) print("Root at x = {0}".format(g.find_root(-2,0))) print("Root at x = {0}".format(g.find_root(0.5,1.5))) plt = plot(g, (x, -1.2, 2.01)) show(plt, figsize=(4, 3))
The output is shown in the following screenshot:
We defined a fairly complicated polynomial equation with a number of real and imaginary roots. We can see that the function will have real roots at 1, -1, and 2 simply by looking at the factored form of the function, which can be confirmed by looking at the plot. When trying out a new numerical method, it's always a good idea to start with a problem that you know the answer to, so that you can evaluate the accuracy and reliability of the method. The find_root
function is a relatively simple way to find a single root within a given domain, specified by the given...