Introduction to Sage

4424 days ago by pub

An Introduction to Sage

  • Press shift+enter or click 'execute' to execute a cell
  • Pressing tab will give auto-complete options, or function descriptions after a parens (
  • A '?' after a command gives detailed information, e.g. 'plot?' tells you about plot

Here are some examples:

# Note that '#' suppresses execution of this line, i.e. a comment 2 + 2 
       
4
4
5/2 
       
5/2
5/2
5^2 
       
25
25
# Variables must be explicitly declared before using var('x y') 
       
(x, y)
(x, y)
# Plotting is what we'll do most often with Sage # 2-D plot(x^4 * exp(x), -10, 1) 
       
# 3-D (You need Java installed for this!) # Click and drag to rotate, use your scroll wheel to zoom plot3d(x^2 - y^2, (x, -1, 1), (y, -1, 1)) 
       
# I will heavily use parametric plots when the surface is defined by an equation f(x, y, z) = 0, # not z = f(x, y). # We'll talk about parametric surfaces later in the semester. # Set up the parameters var('u v') # The x, y, & z coordinates as functions of the parameters fx = cos(u) * sin(v) fy = 2 * sin(u) * sin(v) fz = 3 * cos(v) # Plot an ellipsoid # 'aspect_ratio = [1, 1, 1]' makes the scaling of the axes equal # without it, the plot looks like a sphere because the axes are each scaled differently parametric_plot3d([fx, fy, fz], (u, 0, 2 * pi), (v, 0, pi), aspect_ratio = [1, 1, 1]) 
       
# Sage does derivatives and integrals derivative(x^4 * exp(x), x) 
       
x^4*e^x + 4*x^3*e^x
x^4*e^x + 4*x^3*e^x
integral(x^4 * exp(x), x) 
       
(x^4 - 4*x^3 + 12*x^2 - 24*x + 24)*e^x
(x^4 - 4*x^3 + 12*x^2 - 24*x + 24)*e^x
# 'show' makes the output prettier # (You will need the jsMath fonts: clock the tiny 'jsMath' box on the lower right) show(integral(x^4 * exp(x), x)) 
       
{(x^{4} - 4 \, x^{3} + 12 \, x^{2} - 24 \, x + 24)} e^{x}
{(x^{4} - 4 \, x^{3} + 12 \, x^{2} - 24 \, x + 24)} e^{x}
show(integral(sin(exp(x)), x)) 
       
\int \sin\left(e^{x}\right)\,{d x}
\int \sin\left(e^{x}\right)\,{d x}