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 Taylor series


Run the following code to see how to compute a one-dimensional Taylor series with Sage.

var('x,k')
colors=['red', 'black', 'green', 'magenta']

x1 = pi/2
xmin = x1 - pi;    xmax = x1 + pi

f(x) = sin(x)
p1 = plot(f, (xmin, xmax), color='blue')


Taylor_series_3(x) = f.taylor(x, x1, 3)
p1 += plot(Taylor_series_3, (xmin, xmax), legend_label='3',
    color='red')

Taylor_series_5(x) = f.taylor(x, x1, 5)
p1 += plot(Taylor_series_5, (xmin, xmax), legend_label='5',
    color='green')

Taylor_series_7(x) = f.taylor(x, x1, 7)
p1 += plot(Taylor_series_7, (xmin, xmax), legend_label='7',
    color='black')

Taylor_series_7.show()
show(p1)

The results are shown in the following screenshot:

What just happened?

The Taylor series expansion approximates the behaviour of a function in the vicinity of a point. With an infinite number of terms, the series should match the function exactly in a small region near the point. However, the Taylor series provides a good approximation...