Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 4665

SageMath is similar to Mathematica, Matlab, Maple, or other mathematical software programs. It simplifies numerical and algebraic operations. Each worksheet (this page is a worksheet) is broken into cells. The two types of cells are input cells and output cells. You type your calculations into into an input cell and then hit Shift+Enter to evaluate the cell. SageMath provides the answers to your calculations in an output cell. Click on the input cell below and hit Shift+Enter to evaluate the calculations.

Evaluate a cell

  • Place the cursor in the input cell and hit Shift+Enter to tell SageMath to evaluate a cell.

#This is an input cell #Any text that appears after a hashtag is a comment and is not processed by SageMath #Click in this cell and hit Shift+Enter to evaluate it 10+20+30 2*17*21 5^5 1/3 3/4 (2^7)/(3^2+1)
60 714 3125 1/3 3/4 64/5

Notice how 1/31/3, 3/43/4, and 64/564/5 are left as ratios and not converted to decimals. This is because SageMath leaves results in their exact form and does not convert the results to decimal-form unless you tell it to. To evaluate 1/31/3 and get the answer to three significant figures you would use the numerical approximation command n(1/3,digits=3).

Get a numerically approximate answer and specify the number of significant figures

  • Use n() to get an approximate answer

    • Place the equation to evaluate inside the parentheses

    • Include digits=5 to get a numerical result to five significant figures

      • Don't forget to put a comma between your equation and digits=5

#Try changing the number of significant figures for each of the calculations n(1/3,digits=1) n(3/4, digits=1) n(7/4,digits=2) n(pi,digits=4) n(e,digits=4)
0.33 0.75 1.8 3.142 2.718

Try it yourself

Evaluate the following to three significant figures

  1. 68443.0\frac{684}{43.0}

  2. 28.00.0458\frac{28.0}{0.0458}

  3. 2.68426.0\frac{2.68}{426.0}

#Insert your work here and then hit Shift+Enter to evaluate

SageMath comes with units built in. The units are created by calling the units method but I find it is easiest to define units in terms of the standard abbreviations.

The standard SI units are the following:

  • Length: meter (abbreviated as m)

  • Time: second (abbreviated as s)

  • Mass: kilogram (abbreviated as kg)

  • Force: Newton (abbreviated as N)

The customary U.S. units of measure are:

  • Length: foot (ft)

  • Time: second (s)

  • Mass: slug (slug)

  • Force: pound (lb)

An easy way to convert units is to define new units in terms of the original units. For instance, if your results are given in centimeters you could add the code cm=m/100. Then typing in an answer in cm it would be converted into m.

Defining units

  • Some of the units defined in SageMath include:

    • units.length.meter

    • units.length.centimeter

    • units.length.millimeter

    • units.length.micron

    • units.length.kilometer

    • units.length.foot

    • units.length.inch

    • units.length.mile

    • units.length.yard

  • A complete list of all units can be found in the units.py file.

m=units.length.meter ft=units.length.foot 13*ft 2.0*m #To convert results in centimeters or nanometers into meters you could do the following: dm=m*10^(-1) #decimeter nm=m*10^(-9) #nanometer distance_1=13*dm distance_1 distance_2=55*nm distance_2 #The numerical approximation command n() cannot handle variables so you must divide out variables and then add them back in by hand #Divide distance_1 by m to get rid of the m symbol, then multiply the m back on n(distance_1/m,digits=2)*m #Divide distance_2 by m when using the n() command, then multiply the m back on to the answer n(distance_2/m,digits=3)*m
13*foot 2.00000000000000*meter 13/10*meter 11/200000000*meter 1.3*meter (5.50e-8)*meter

Try It Yourself

Evaluate the following to four significant figures and convert all answers to standard SI units (m, s, N, kg)

  1. 684μm43ms\frac{684 \mu m}{43 ms}

  2. 28ms0.0458Mm\frac{28 ms}{0.0458 Mm}

  3. 2.68mm426Mg\frac{2.68 mm}{426 Mg}

Remember that:

  • μ=\mu = micro =1/1000000=106 = 1/1000000= 10^{-6}

  • m=m = milli =1/1000=103 = 1/1000 = 10^{-3}

  • M=M = Mega =1000000=106 = 1000000 = 10^{6}

#Insert your work here

Unit conversion can be accomplished using the .substitute() command. You can tell SageMath to substitute one variable in for another. For example, to convert from meters to feet, substitute in m=3.28084*ft and evaluate it.

Substitute one variable for another

  • The .substitute() command is added to the end of a variable

    • To replace ft with 0.3048*m in the variable called d, you would type d.substitute(ft=0.3048*m)

  • The arguments in the parentheses sets the orignal variable equal to the new value

  • You can specify several substitutions at once by separating substitutions with commas

    • To replace ft with 0.3048*m and ns with s*10^(-9) in the variable v you would type v.substitute(ft=0.3048*m, ns=s*10^(-9))

  • This isn't just for unit conversion, this works for any two variables in a function

    • To replace y with 3*x-5 in function f you would type f.substitute(y=3*x-5)

  • There is a convert feature built in to SageMath but I find substitution more intuitive

#Convert "dist" from meters to feet dist=71*m #Define variable "dist" dist.substitute(m=3.28084*ft) #Convert "dist" from meter to feet #One trick you can use to get the correct number of significant figures is to leave off the new unit, use n(), then add the new unit on to the end n(dist.substitute(m=3.28084),digits=2)*ft #Notice how the new units are OUTSIDE the n() command #Convert "vel" from miles/hour to meters/second var('mi,hr') #I want to use miles and hours so I need to define these as variables vel=17.0*mi/hr #Define the velocity variable called "vel" new_vel=vel.substitute(mi=1609*m,hr=60*60*s) #Notice that SageMath doesn't print out a result when you assign an evaluation to a variable... new_vel #... but by evaluating the new variable SageMath will print out the results #To get the correct number of sig. figs.: n(vel.substitute(mi=1609,hr=60*60),digits=2)*m/s
232.939640000000*ft 230.*ft (mi, hr) 7.59805555555556*m/s 7.6*m/s

Try It Yourself

  1. Calculate the following quantities using the correct number of signficant figures:

  • 32.01 ft22.3 s\frac{32.01\ ft}{22.3\ s}

  • 17 mi27 min\frac{17\ mi}{27\ min}

  • 1208 m0.1 hr\frac{1208\ m}{0.1\ hr}

Hint: Units of time include:

  • units.time.second

  • units.time.minute

  • units.time.hour

#Input your work here

You can define functions in SageMath by giving the function a name and specifying the arguments of the function in parentheses after the function name. For example, to define a function f(x)f(x) you might type f(x)=3*x^2+2*x-1. You can also have functions of multiple variables such as h(x,y,z)=3*x*y-z^3

Defining Functions

  • Function names follow same convention as variables; Must start with a letter, can contain numbers, can't contain spaces (only underscores _), and no special characters

  • Format is function_name(variable1,variable2,)= equation such as g(a,b)=2*a+3*b

  • Functions can be evaluated at specific points by entering numbers in for the arguments such as g(1,2)

f(x)=3*x^2+7*x-17 #Defining a function f of x #Try evaluating this function at the following points: x=0, x=5, x=7 #For example, type f(0) to evaluate at x=0