# Introduction¶

Sage was initially created by William Stein in 2004-2005, using open source programs released under the GPL or a

GPL-compatible license. The main goal of the project was to create a viable open source alternative to proprietary mathematical software to

be used for research and teaching. The rst release of the program was in February 2005. By the end of the year, Sage included Pari, GAP

and Singular libraries as standard and worked as a common interface to other mathematical programs like Mathematica and Magma.

## 3D PLotting¶

Sage can also be used to create three-dimensional plots. In both the notebook and the REPL, these plots will be displayed by default using the open source package [Jmol], which supports interactively rotating and zooming the figure with the mouse.

Use plot3d to graph a function of the form \( z = f(x,y) . \)

```
sage: x, y = var('x,y')
sage: plot3d(x^2 + 2*y^2, (x,-2,2), (y,-2,2))
```

```
sage: u, v = var('u,v')
sage: f_x(u, v) = u
sage: f_y(u, v) = v
sage: f_z(u, v) = u^2 + 2 * v^2
sage: parametric_plot3d([f_x, f_y, f_z], (u, -2, 2), (v, -2, 2))
```

```
sage: x, y, z = var('x,y,z')
sage: implicit_plot3d(x^2 + 2*y^2 + z^2 - 4, (x,-2, 2), (y,-2, 2), (z,-2, 2))
```

# More Examples¶

Yellow Whitney's umbrella:

```
sage: u, v = var('u,v')
sage: fx = u*v
sage: fy = u
sage: fz = v^2
sage: parametric_plot3d([fx, fy, fz], (u, -1, 1), (v, -1, 1),
....: frame=False, color="yellow")
```

Cross cap:

```
sage: u, v = var('u,v')
sage: fx = (1+cos(v))*cos(u)
sage: fy = (1+cos(v))*sin(u)
sage: fz = -tanh((2/3)*(u-pi))*sin(v)
sage: parametric_plot3d([fx, fy, fz], (u, 0, 2*pi), (v, 0, 2*pi),
....: frame=False, color="red")
```

Twisted Torus:

```
sage: u, v = var('u,v')
sage: fx = (3+sin(v)+cos(u))*cos(2*v)
sage: fy = (3+sin(v)+cos(u))*sin(2*v)
sage: fz = sin(u)+2*cos(v)
sage: parametric_plot3d([fx, fy, fz], (u, 0, 2*pi), (v, 0, 2*pi),
....: frame=False, color="red")
```

```
sage: x, y, z = var('x,y,z')
sage: f(x, y, z) = 4*x^2 * (x^2 + y^2 + z^2 + z) + y^2 * (y^2 + z^2 - 1)
sage: implicit_plot3d(f, (x, -0.5, 0.5), (y, -1, 1), (z, -1, 1))
```

## Basic Arithmetic Operations¶

The goal of this document is not to teach you numerical analysis, but

to explain how to express your ideas in Sage and Python. By numerical

computation we essentially mean machine precision floating point

computations.

Type in the first cell, hold the shift key down, and press the enter key.

`sage: a=1`

assigned to the variable a, but this result was not echoed. Repeat on the cell

below; click on the cell, then use "shift-enter" or click on "evaluate". So if you type

`sage: a`

`sage: b=2; b`

This time the value 2 was assigned to b and then the value of b was echoed. The

semicolon is used to separate commands appearing on the same line. The same thing

could be done by typing "b=2", pressing enter, typing "b".

Feel free to use whichever of these approaches is most comfortable to you.

To echo a floating point number, you have to use the float() function with the

variable you assigned the expression or fraction.

`sage: b=2/3`

sage: float(b)

0.6666666666666666

Basic functions which normally are built-in are also available in Sage. Such as:

gcd()

sqrt()

`sage: q = cos(2*pi/50)`

sage: numerical_approx(q))

0.992114701314478

sage: q.n()

0.992114701314478

sage: q.n(prec=100) # bits

0.99211470131447783104979304279

sage: q.n(digits=50) # decimal digits

sage: n(q)

0.992114701314478

sage: N(q)

0.992114701314478
sage: RealField(100) # Real Field with 100 bits of precision

sage: RealField(100)(q)

0.99211470131447783104979304279

sage: RR # Real Field with 53 bits of precisio

sage: RR(q)

0.992114701314478

sage: RealField(100) # Real Field with 100 bits of precision

0.99211470131447783104979304279

`sage: a=2/pi*sqrt(3)`

sage: RealField(100)(a)

1.1026577908435840990226529966

`sage: 2+3/5; 34*18-33`

3/5

579

`sage: factor(_) `

3 * 193

`sage: 3*pi; n(_); n(e); I^2`

3*pi

9.42477796076938

2.71828182845905

-1

`sage: e^2; exp(2) `

e^2

`sage: u = var('u')`

sage: diff(sin(u), u)

cos(u)

`sage: diff(sin(x^2), x, 4)`

16*x^4*sin(x^2) - 48*x^2*cos(x^2) - 12*sin(x^2)

## Complex Numbers¶

```
sage: z = complex(-3,4);
sage: z
```

(-3+4j)

sage: real(z)

-3.0

sage: imag(z)

4.0

sage: abs(z)

5.0

sage: arg(z)

(2.214297435588181+0j)

sage: conjugate(z)

(-3-4j)

sage: z1 = complex(-3,4);

sage: z1

(-3+4j)

sage: z2=z1*i;

sage: z2

(-4-3j)

sage: a = arrow((0,0),(real(z1),imag(z1)))

sage: a

sage: plot(a)

`sage: b = arrow((0,0),(real(z2),imag(z2)))`

sage: b

sage: plot(b)

`sage: plot(a+b)`

`sage: z = 1+i`

sage: a = arg(z)

sage: a

1/4*pi

sage: b = abs(z)

sage: b

sqrt(2)

sage: c = abs(z)*e^(I*(arg(z)));

sage: c

sqrt(2)*e^(1/4*I*pi)

sage: simplify(c)

I + 1

sage: sqrt(-8,all=true)

[2*sqrt(-2), -2*sqrt(-2)]

sage: solve( x^3 == -8, x )

[x == I*sqrt(3)*(-1)^(1/3) - (-1)^(1/3), x == -I*sqrt(3)*(-1)^(1/3) - (-1)^(1/3), x == 2*(-1)^(1/3)]

## I. How to define functions

To define a function, just type in the formula. We need to use a special form for the left hand side, which includes an underscore after

`sage: f(x)=(cos(x)-1)/x^2`

# f(x) is now defined and values can be passed

sage: f(3)

1/9*cos(3) - 1/9

sage: f(pi)

-2/pi^2

sage: f(pi).n()

-0.202642367284676

`sage: var('a b')`

(a, b)

sage: f(a+b)

(cos(a + b) - 1)/(a + b)^2

`sage: f(x).plot()`

`# plot(function, (starting x pass to function, ending x pass to function), ymin = graphical range min, ymax = graphical range max`

sage: plot(f(x), (-2*pi, 2*pi),ymin = -1, ymax = 1)

To define a discontinous function, intervals must be continuos and individually discrete i.e., intervals must connect to next but must be over a defined interval; variable definition is optional.

`sage: f1 = x^2`

sage: f2 = 4-x

sage: f = piecewise([[(0,2),f1],[(2,4),f2]], x)

# examples

sage: f(1)

1

sage: f(2.5)

1.50000000000000

sage: f(1.5)

2.25000000000000

#important note!!!!!

sage: f(2)

3

#function at the interval junction returns (f1(2)+f2(2))/2, true for all junctions

Plot of piecewise function

`sage: f.plot()`

`sage: f.derivative()`

Piecewise defined function with 2 parts, [[(0, 2), x |--> 2*x], [(2, 4), x |--> -1]]

# Usefule attributes

# Critical points only defined over individual functions, does not include interval points

`sage: f.critical_points()`

[]

`sage: f.domain()`

(0, 4)

sage: f.intervals()

[(0, 2), (2, 4)]

sage: f.end_points()

[0, 2, 4]

sage: f.which_function(1)

x |--> x^2

sage: f.which_function(3)

x |--> -x + 4

# Calculus

Sage knows how to differentiate and integrate many functions. For example, to

differentiate \( \sin (u) \) with respect to u, do the following:

`sage: u = var('u')`

sage: diff(sin(u), u)

cos(u)

To compute the fourth derivative of \( \sin(x^2) \):

`sage: diff(sin(x^2), x, 4)`

16*x^4*sin(x^2) - 48*x^2*cos(x^2) - 12*sin(x^2)

`sage: integral(x*sin(x^2), x)`

-1/2*cos(x^2)

sage: integral(x/(x^2+1), x, 0, 1)

1/2*log(2)

To compute the partial fraction decomposition of \( \frac{1}{x^2-1}: \)

`sage: f = 1/((1+x)*(x-1))`

sage: f.partial_fraction(x)

1/2/(x - 1) - 1/2/(x + 1)

Numerical integration:

`sage: numerical_integral(sin(1/x)*sin(x),0,pi)`

(1.1839699090568376, 7.755356113813495e-07)

sage: a=2/pi

sage: RealField(100)(a)

0.63661977236758134307553505349

sage: (_)*1.1839699090568376

0.7537386539938299

# 2. Part 1: Matrix Algebra

Part I. Plotting

Plotting functions

Implicit plot

Vertical and horizontal lines

Lables and texts

Arrows

Pollar plot

Direction fields

# 2. Part 2: Linear Systems of Ordinary Differential Equations

Solving ODEs

Direction fields

Separable equations

Equations reducible to separable equations.

Exact equations

Integrating Factors

Linear and Bernoulli equations

Riccati equation

Existence and Uniqueness of solutions

Qualitative analysis

Applications

# 2. Part 3: Non-linear Systems of Ordinary Differential Equations

Recurrences

Numerical solutions

a) Euler methods

b) Polynomial approximations

c) Runge-Kutta methods

d) Multistep methods

4) Numerov's method

Applications

# 2. Part 4: Numerical Methods

Second order differential equations

Fundamental set of solutions. Wronskian

General solution

Reduction of order

Non-homogeneous equations.

Lagrange's method

Method of undetermined coefficients

Operator methods (not sure yet)

Applications

# 2. Part 5: Fourier Series

Laplace transform

Heaviside function

Laplace Transform of Discontinuous Functions

Inverse Laplace transformation

Laplace transformation in differential equations

Mechanical and Electrical Vibrations

Other applications

# 2. Part 6: Partial Differential Equations

Laplace transform

Heaviside function

Laplace Transform of Discontinuous Functions

Inverse Laplace transformation

Laplace transformation in differential equations

Mechanical and Electrical Vibrations

Other applications

Return to Sage page

Return to the main page (APMA0340)

Return to the Part 1 (Matrix Algebra)

Return to the Part 2 (Linear Systems of Ordinary Differential Equations)

Return to the Part 3 (Non-linear Systems of Ordinary Differential Equations)

Return to the Part 4 (Numerical Methods)

Return to the Part 5 (Fourier Series)

Return to the Part 6 (Partial Differential Equations)