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 – defining callable symbolic expressions


In Chapter 3, we learned how to define a mathematical function as a callable symbolic expression. Since we'll be working with callable symbolic expressions extensively in this chapter, let's learn a little more about how to use them. Enter the following code into an input cell in a worksheet, and evaluate the cell:

var('a, b, c, x')
f(x) = a * x^2 + b * x + c    # A callable symbolic expression
print("f(x):")
f.show()
print("Variables in f: {0}  Arguments in f: {1}".format(
    f.variables(), f.arguments()))
print("Type of f: {0}".format(type(f)))

g(x) = f.derivative(x)
print("g(x):")
g.show()
print("Variables in g: {0}  Arguments in g: {1}".format(
    g.variables(), g.arguments()))

g_plot = plot(g(a=1, b=-1), (-1, 1))
g_plot.axes_labels(['x', 'g(x)'])
show(g_plot, figsize=(3,3), aspect_ratio=1.0)

The output is shown below:

What just happened?

We started with a var statement to tell Sage that x, a, b, and c are symbolic variables...