Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168740
Image: ubuntu2004

A brief tutorial for calculus on Sage

Sage is a free open-source mathematics software system.  It is quite powerful, and can be used for a wide range of mathematical tasks, but today we'll focus on the basics of using Sage to do integration.  First, we'll look at the basics of communicating with Sage. 

Using Sage as a calculator

Try typing 2+2 into the box below.  Hit Shift+Enter or click "evaluate" to evaluate.  (See what happens if you just hit Enter.  I guarantee this will happen to you many times.  It happens to me often when I use Sage.)

2+2

Sage knows about mathematical constants, like π\pi and ee.  It also knows a lot of commonly used functions, such as the natural log, trig functions, and inverse trig functions.  Try evaluating each of the following.

cos(pi)
ln(e)
arccos(0)
arccos(1/sqrt(2))

Notice that by default, Sage will not give a numerical answer if it can give a symbolic answer.  This is usually good - you can keep symbolic expressions in your work until the very end, so that you minimize rounding errors.  Sometimes it might not simplify as much as you'd like.  Every expression has a numerical approximation method and a simplify method.  An method of an expression is invoked by using the "." operator (a period).  The numerical approximation method is n() and the simplify method is simplify().  Evaluate the examples below.

arccos(0).n()
arccos(1/sqrt(2)).n()
arccos(1/sqrt(2)).simplify()

Use Sage to compute

  • a decimal value of 55/6 (Hint: 55/6 is an expression and has an n() method, too.)
  • the cosine of π4\frac{\pi}{4}
  • the natural log of 123
55/6.n()
(pi/4).n()
cos(pi/4).n(digits=)
ln(123).simplify()

Variables and expressions

In order to use a variable like xx to define an expression in Sage, you have to tell Sage that you want to use that symbol as a variable.  You can use expressions to define equations, which Sage can solve.  Evaluate the following to see how this works.

x = var('x') solve([x^2 + 6*x + 8 == 0], x)

We can use Sage to find the quadratic formula.  Sage will solve an expression in multiple variables for one in terms of all the others.

a,b,c = var('a b c') solve([a*x^2 + b*x + c == 0], x)

In Sage a 'callable symbolic expression' is a mathematical epression using a variable you have defined.  The following examples will illustrate how you can work with these.

f = 3*x^2 + 1 f(x=3)

You can plot these functions pretty easily.

g = (atan(a))^2 plot(g)

Plot more than one function by adding plots.  You can also play around with colors and ranges.

plot(g,(-.5,1),color="red") + plot(f)

Try these:

  • Define a function in terms of tt: g(t)=cos(t)+4t2g(t) = \cos(t) + 4t^2.  Evaluate your function at t=0t = 0 and t=πt = \pi.
  • plot gg
  • Solve an equation in the variable yy to find out where the graphs of 3y+63y + 6 and 2y2+42y^2 +4 intersect.  Make a plot showing the intersection points.
t = var('t') g = cos(t) + 4*t^2
print "g(0) = " ,g(t=0), " and g(pi) = ", g(t=pi)
plot(g, (-pi,pi))
y = var('y') solve([3*y+6==2*y^2 + 4], y)
h = 3*y + 6
plot(3*y+6, (-1,3)) + plot(2*y^2 + 4, (-1,3),color = "red") + points([(-.5,h(y=-.5)), (2,h(y=2))], color="green")

Actual Calculus

Sage can also find derivatives and integrals (definite and indefinite) of expressions. (Note that the integral function returns an antiderivative.)

Df = diff(f) print Df
Df(x=1)
If = integral(f,x) print If
If(x=2) - If(x=1)
integral(f,x,1,2)

You can also take derivatives and integrals of expressions directly.  Here we find the derivative and antiderivative of the function cosθsinθ\cos\theta\sin\theta.

theta = var('theta') D = diff(cos(theta)*sin(theta)) D

Incidentally, check this out:

D.trig_simplify()
integral(cos(theta)*sin(theta),theta)

Remember, Sage can do lots of stuff symbolically.  For example, consider the function A(x)=0x3t2+cost  dtA(x) = \int_0^x 3t^2 + \cos t \; dt.  We can easily find an expression for this function, and then compute with it.

t = var('t') A = integral(3*t^2 + cos(t),t, 0,x) A
A(x=-pi)

Note that Sage confirms the Fundamental Theorem of Calculus:

diff(A)

Some exercises

Now you should have enough Sage tools to answer some questions.

1. Compute 0π/4ln(1+tanx)  dx\int_0^{\pi/4} \ln(1 + \tan x) \; dx

2.  Last time we worked hard to integrate 1(x+1)(x2+2x+2)2\frac{1}{(x+1)(x^2 + 2x +2)^2}

  1. Check our work from last time using Sage. 
  2. Note also that Sage expressions have a partial_fraction() method, which will try to compute a partial fraction decomposition. Experiment with this to find the partial fraction decomposition.

3.  Let L(c)L(c) be the length of the parabola f(x)=x2f(x) = x^2 from x=0x=0 to x=cx=c, where c0c\geq0 is a constant. Write an integral for LL, and use Sage to evaluate this integral to produce a function. Plot it.

sage.calculus.calculus.maxima.eval('logarc:true')
x = var('x') c = var('c') g = diff(x^2) arclength = integral(sqrt(1+g^2),x,0,c) arclength

4.  Plot the y=cos(x2)y = \cos(x^2) on the interval [0,π][0, \sqrt{\pi}].  Use Sage to see whether the regions above and below the xx axis generate equal volumes when rotated about the yy axis.