Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

A few examples of doing Calculus with Sage.

Views: 137

Calculus in Sage

Herein we discuss methods for doing some calculus in Sage.

Differentiation

Sage can differentiate symbolic expressions using the command derivative, or the alias diff. The first argument is the expression or function to be differentiated, and the second is the variable to differentiate with respect to. (Don't forget to declar your symbolic variables using var!)

var("x") derivative(3*x^2 + 2*x + 5, x)
x 6*x + 2

Sage can even handle other variables as constants, but be sure to declare them as symbolic:

var("k") diff(sin(k*x), x)
k k*cos(k*x)

If you define a function and ask Sage to differentiate that, Sage will return another function. The output can look a little weird:

f(x) = x^2 derivative(f, x)
x |--> 2*x

But on the other hand, you can define a new function using your derivative, and it will work as you would hope:

fprime = derivative(f, x) fprime(3)
6

Note that the above is correct, since if f(x)=x2f(x) = x^2, then f(x)=2xf'(x) = 2 x, and so f(3)=6f'(3) = 6.

So here (for example) is a shortcut way to make a plot of a function ff and its first derivative on the interval [0,4][0, 4]:

f(x) = sin(x) fprime = derivative(f, x) plot((f(x), fprime(x)), (x, 0, 4))

Sage can also take the second, third, or higher derivative of an expression or function:

f(x) = x^3 - 3*x^2 + x - 5 derivative(f, x) derivative(f, x, 2) derivative(f, x, 3) derivative(f, x, 4)
x |--> 3*x^2 - 6*x + 1 x |--> 6*x - 6 x |--> 6 x |--> 0

Integration

Not surprisingly, Sage can also do symbolic integration using all the techniques we have learned, plus some others. Sage can do definite integrals, such as the integral from x=ax = a to x=bx = b of the function ff using variable xx with the command integrate(f, (x, a, b)):

var("x") integrate(x^2, (x, 1, 2))
x 7/3

This is easy to check by hand: [ \int_1^2 x^2 , dx = \left[ \frac13 x^3\right]_{1}^2 = \frac13 (8 - 1)=\frac73 ]

Sage can also do indefinite integrals:

integrate(sec(x)^2, x)
tan(x)

(But notice it does not include the +C+C that we know we should include.)

Sage can happily solve integrals that we have not learned how to solve yet. For example, we know that the area underneath the top half of a circle of radius 22 should be the area under y=4x2y = \sqrt{4 - x^2} as xx goes from 2-2 to 22, or [ \int_{-2}^{2} \sqrt{4 - x^2} , dx ] as the graph below shows:

plot(sqrt(4-x^2), (x, -2, 2), fill=true, aspect_ratio=1)

We don't know how to solve that integral (although you will learn a technique for doing so in Calculus II), but we do know that the area should be [ \frac12 \pi r^2 = \frac12 \pi 2^2 = 2 \pi ] Sage can actually solve the integral exactly:

integrate(sqrt(4-x^2), (x, -2, 2))
2*pi

Similarly with indefinite integrals that you may not know how to solve:

integrate(sin(x)^2, x)
1/2*x - 1/4*sin(2*x)

We can check by differentiating:

diff(1/2*x-1/4*sin(2*x), x)
-1/2*cos(2*x) + 1/2

Wait! That looks different! I wonder if these are actually the same?

fdif = sin(x)^2 - (-1/2*cos(2*x) + 1/2) plot(fdif, (x, 0, 2*pi))

That might be equal to zero. The difference is very small, about 101510^{-15}, which is about what the computer stores for a floating point number.

We can try Sage's trig_simplify function to see if the expression might reduce to zero:

fdif.trig_simplify()

Of course, there are some functions which do not have elementary antiderivatives, such as the following. Sage has no more luck with this than we do:

ingd62ea2(3q8+x4b,b 8)d-1198d0ca
integrate(sqrt(x^4 + 1), x)

Numerical Integration

Sage does however, have excellent built-in numerical integration routines. The command numerical_integral(f, a, b) will give back two numbers. The first is the estimate of the integral of f(x)f(x) from x=ax = a to x=bx = b. (Note that no variable is specified; for this function you must use x.) The second number is an error estimate for the approximation. (That is, an estimate for how far off Sage thinks its estimate might be. You would like the second number to be small.)

For example:

n405cbaa6_etf9gr2ddx81d21)c4663d01
(2.3333333333333335, 2.590520390792032e-14)

We know the actual answer is [ \int_1^2 x^2 , dx = \frac73 \quad \mbox{or} \quad 2 \frac13 ] Sage reports an answer that is nearly this, and reports an error on the order of 101410^{-14}, which is about the machine precision. (The computer does not, by default, store numbers with more accuracy than this.)

Numerical integration will give approximations to definite integrals like the following, which we know we do not have an elementary antiderivative for:

n65m4f51r-ab7e_4in6sqt1b1+ 60)f39c49
(3.653484493139719, 4.056182604394686e-14)

Of course, this represents area under a curve:

plot(sqrt(1+ x^4), (x, 0, 2), fill=true)
x == -1/2*(1/6*sqrt(1/3)*sqrt((27*y - 4)*y) - 1/2*y + 1/27)^(1/3)*(I*sqrt(3) + 1) - 1/18*(-I*sqrt(3) + 1)/(1/6*sqrt(1/3)*sqrt((27*y - 4)*y) - 1/2*y + 1/27)^(1/3) + 1/3
list_plot(_)
pts = [i/10. for i in range(-30, 30, 2)]
pts
[-3.00000000000000, -2.80000000000000, -2.60000000000000, -2.40000000000000, -2.20000000000000, -2.00000000000000, -1.80000000000000, -1.60000000000000, -1.40000000000000, -1.20000000000000, -1.00000000000000, -0.800000000000000, -0.600000000000000, -0.400000000000000, -0.200000000000000, 0.000000000000000, 0.200000000000000, 0.400000000000000, 0.600000000000000, 0.800000000000000, 1.00000000000000, 1.20000000000000, 1.40000000000000, 1.60000000000000, 1.80000000000000, 2.00000000000000, 2.20000000000000, 2.40000000000000, 2.60000000000000, 2.80000000000000]