Book Image

Sage Beginner's Guide

By : Craig Finch
1 (1)
Book Image

Sage Beginner's Guide

1 (1)
By: Craig Finch

Overview of this book

Table of Contents (17 chapters)
Sage Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – finding roots of a polynomial


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:

What just happened?

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...