Sage Commands


On this page you will find the SageMath commands used in these labs.

Operations

The symbol * must be used to denote multiplication.
Example: \(2x\) must be entered as 2 * x.

The symbol / is used to denote division.
Example: \( \pi/3 \) is entered as pi/3.

The symbol ^ is used to denote exponentiation.
Example: \( 2^3 \) is entered as 2^3.

Predefined functions

Function SageMath Syntax
\( |x| \) abs(x)
\( \sqrt{x} \) sqrt(x)
\( e^x, \exp(x) \) exp(x)
\( \ln(x) = \log_e(x) \) log(x)
\( \log(x) = \log_{10}(x) \) log(x,10)
\( \log_{b}(x) \) log(x,b)
\( \sin(x) \) sin(x)
\( \cos(x) \) cos(x)
\( \tan(x) \) tan(x)
\( \sec(x) \) sec(x)
\( \arcsin(x) = \sin^{-1}(x) \) arcsin(x) or asin(x)
\( \arccos(x) = \cos^{-1}(x) \) arccos(x) or acos(x)
\( \arctan(x) = \tan^{-1}(x) \) arctan(x) or atan(x)
\( \mathrm{arcsec}(x) = \sec^{-1}(x) \) arcsec(x) or asec(x)

Predefined constants

\( \pi = 3.14159\dots \)    as pi
\( e = 2.71828\dots \)     as e
\( i = \sqrt{-1} \)     as I

Numerical Approximations

SageMath can perform symbolic computations on some predefined constants and values of functions. In order to convert the value to a numerical approximation, use the function N( ). For example, to find the numerical approximation to \(\pi\cdot\sin(1)\) enter N(pi*sin(1)).

Evaluating Limits

SageMath has the ability to compute the limits of certain functions. For example, to compute \(\lim\limits_{x\to1}\dfrac{x^2-2x+1}{x-1}\) you would type: limit( (x^2-2*x+1)/(x-1), x=1)

Graphing

The plot command takes 3 inputs: the function rule, the minimum input value to be displayed, and the maximum input value to be displayed. The command is

plot(function rule, xmin, xmax).

Example: To graph \(\dfrac{x^2 – 2x+1}{x-1}\) on the interval [-1, 4] enter:  plot( (x^2-2*x+1)/(x-1), -1,4)

Define a Function

Defining a named function in SageMath is quite straightforward.  It follows the format FunctionName(x) = expression in the variable x. By typing something like this on one line, SageMath will use FunctionName(x) as a shorthand for the value of the expression at x (whatever you use for x).
For example, if you want to evaluate \(f(3)\) where \(f(x)=3x^2 +7\), you may enter the commands

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

to return the output, \(34\). In a more complicated example, that would save a lot of time and possible errors, if you wish to know the quotient expression \(\dfrac{myF(x) – myF(2)}{x – 2}\)
for some function given by the expression \(myF(x) = x^4-3x^3+\frac13x^2-15x+7\), you will get the correct output by entering

myF(x) = x^4 - 3*x^3 + (1/3)*x^2 - 15*x + 7
(myF(x) - myF(2))/(x-2)

A couple of points to remember.
•  The names of functions are sensitive to capitalization. This means that typing myf(x) - myf(2) in the second line above would produce an error, unless you used myf on the first line also.
•  On this site, function definitions are not kept from one SageMath cell to another. This means that if you want to use a function you defined, it must be defined in that very computation cell.

Derivatives

The command diff(f, x) will differentiate the function \(f\) with respect to \(x\). Note that you must first define the function \(f\).
Example:

myF(x) = 3*x^2 + 7
diff(myF, x)

This will return the function rule 6*x.If you want to evaluate the derivative at a specified input, for example \(f'(3)\) you must first define the function \(f\), define the function \(f’\), and then evaluate \(f'(3)\). The second line of the example below follows the format of function definition in SageMath.
Example:This will return \(18\), the value of \(f'(3)\).

f(x)=3*x^2+7
df(x)=diff(f,x)
df(3)
Skip to toolbar