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 – calculating derivatives


Run the following code to see how to compute derivatives with Sage:

var('x, y')
f(x, y) = 3 * x^4 * y^3 + 9 * y * x^2 - 4 * x + 8 * y
print("f(x,y):")
f.show()

dfdx(x, y) = diff(f, x)
print("df/dx:")
dfdx.show()
dfdy(x, y) = diff(f, y)
print("df/dy:")
dfdy.show()

print("Second derivative:")
d2fdx2(x, y) = derivative(f, x, 2)    # Synonym for diff
d2fdx2.show()

# Trigonometric functions
g(x) = sqrt(x^3 + csc(x))
print("g(x):")
g.show()
dgdx(x) = g.diff(x)
print("dg/dx:")
dgdx.show()
# Implicit differentiation
# The next line tells Sage that y is a function of x
y(x) = function('y', x)
expr = 5 * y^2 + sin(y) == x^2

print("Expression:")
expr.show()

# take the derivative and solve for dy/dx
dydx = solve(diff(expr), diff(y))
print("dy/dx:")
dydx[0].show()

The results are shown in the following screenshot:

What just happened?

Sage is able to compute derivatives for many types of functions. You can use a function called diff (or differentiate or derivative...