A few examples of doing Calculus with Sage.
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
!)
Sage can even handle other variables as constants, but be sure to declare them as symbolic:
If you define a function and ask Sage to differentiate that, Sage will return another function. The output can look a little weird:
But on the other hand, you can define a new function using your derivative, and it will work as you would hope:
Note that the above is correct, since if , then , and so .
So here (for example) is a shortcut way to make a plot of a function and its first derivative on the interval :
Sage can also take the second, third, or higher derivative of an expression or function:
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 to of the function using variable with the command integrate(f, (x, a, b))
:
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:
(But notice it does not include the 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 should be the area under as goes from to , or [ \int_{-2}^{2} \sqrt{4 - x^2} , dx ] as the graph below shows:
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:
Similarly with indefinite integrals that you may not know how to solve:
We can check by differentiating:
Wait! That looks different! I wonder if these are actually the same?
That might be equal to zero. The difference is very small, about , 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:
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:
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 from to . (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:
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 , 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:
Of course, this represents area under a curve: