Octave Sage Worksheet Tutorial
- This is simply all the code from http://volga.eng.yale.edu/sohrab/matlab_tutorial.html
- Go to the above site for more explanations.
- Press shift-enter to evaluate code below and move to the next cell; press alt-enter to evaluate in place.
- Double click on text between cells to edit it (it’s markdown format)
Vectors and matrices
w =
1 2
3 4
z =
5
6
7
Complex numbers and complex conjugation
z = (2,3)
ans = (2,-3)
ans = (2,-3)
Transposition and Hermitian conjugation
M =
(1,1) (2,0)
(3,0) (4,0)
ans =
(1,-1) (3,-0)
(2,-0) (4,-0)
Reducing and paging matlab screen output
Put semicolon to silence output.
Creating a program file: .m files
- Using the editor, I wrote a small file
myfirst.m
- Load it as follows, which sets
a
,b
,c
. - NOTE: the sage octave interface doesn’t correctly set the current working directory for loading scripts, which is why we do the cd below. You have to set the path below to wherever you are running this worksheet. This is very ugly, and will get fixed…
ans = /mnt/home/teaAuZ9M/sage-cloud-templates/octave
a = 2
b = 3
c = 5
Creating a vector of equally spaced numbers
ans =
3 6 9 12
ans =
3 7.5 12
Selecting parts of vectors and matrices: the colon operator
v =
2 3 4 5 5 6 7 8 9 10
w =
2 3 4 5
0.936444 0.408223 0.0792802 0.120729 0.686016
0.648213 0.329586 0.842031 0.343852 0.505076
0.103871 0.205018 0.564111 0.720999 0.0475961
0.111617 0.584033 0.0621617 0.805288 0.358781
0.638088 0.328756 0.812207 0.686526 0.50059
M =
0.74116 0.357223 0.796885 0.642807 0.44094
0.324165 0.209321 0.780547 0.638626 0.48057
0.52199 0.466731 0.363482 0.270519 0.680753
0.973528 0.161067 0.793242 0.427292 0.0214446
0.289777 0.734638 0.423491 0.510815 0.658672
ans =
0.324165 0.209321 0.780547
0.52199 0.466731 0.363482
0.973528 0.161067 0.793242
0.289777 0.734638 0.423491
ans =
0.642807
0.638626
0.270519
0.427292
0.510815
Creating identity and zero matrices
ans =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
ans =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
Creating a vector or matrix full of ones
ans =
0 0 0 0 0
0 0 0 0 0
Creating a diagonal matrix
ans =
1 1 1 1 1
1 1 1 1 1
Creating off diagonal/triangular matrices
ans =
10 -3 0 0 0
0 20 -4 0 0
0 0 30 -5 0
0 0 0 40 -6
0 0 0 0 50
Inverting a matrix
A =
0.1 0.015 0.002 0.00025 3e-05
0 0.05 0.00666667 0.000833333 0.0001
0 0 0.0333333 0.00416667 0.0005
0 0 0 0.025 0.003
0 0 0 0 0.02
Diagonalizing a matrix
A =
0.1 0.015 0.002 0.00025 3e-05
0 0.05 0.00666667 0.000833333 0.0001
0 0 0.0333333 0.00416667 0.0005
0 0 0 0.025 0.003
0 0 0 0 0.02
e = eig(M)
1.71399
0.183105
-0.269172
-0.130517
Programming
ans = 3
ans = 5
ans = 7
ans = 9
ans = 11
2*j+1
n = 3
v = 0
v =
1 4 9
M =
2.99803 2.99211 2.98229
8.99211 8.96858 8.92978
18.9823 18.9298 18.8443
Drawing graphics
- it’s not smooth yet
- use the figure command, then show the image in another cell.
- see comment above about setting this path

ans = /mnt/home/teaAuZ9M/sage-cloud-templates/octave
