Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Github repo cloud-examples: https://github.com/sagemath/cloud-examples

Views: 7807
License: MIT

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)

# Set the default mode to octave, which makes this an octave worksheet. %auto %default_mode octave

Vectors and matrices

w = [1 2 3 4]
w = 1 2 3 4
z = [5 6 7]
z = 5 6 7

Complex numbers and complex conjugation

z = 2+3*i conj(z)
z = (2,3) ans = (2,-3)
z'
ans = (2,-3)

Transposition and Hermitian conjugation

M=[1+i,2;3,4] M'
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.

v=[1 2 3];

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...

cd(); cd('cloud-examples/octave'); pwd()
ans = /projects/1041134f-47fb-4a2c-8158-7ba2729f4bc0/cloud-examples/octave
myfirst
a, b, c
a = 2 b = 3 c = 5

Creating a vector of equally spaced numbers

[3:3:12]
ans = 3 6 9 12
linspace(3, 12, 3)
ans = 3 7.5 12

Selecting parts of vectors and matrices: the colon operator

v = 1:10; w = v(2:5); v(1:4) = w; v, w
v = 2 3 4 5 5 6 7 8 9 10 w = 2 3 4 5
M = rand(5)
M = 0.941285 0.153281 0.074534 0.952117 0.802027 0.829158 0.0334255 0.19148 0.42151 0.0711905 0.880278 0.973486 0.290043 0.616042 0.177844 0.29837 0.963968 0.987599 0.142489 0.331631 0.196745 0.080216 0.674406 0.332727 0.680742
M(2:5,1:3) % a 4 x 3 submatrix of M
ans = 0.829158 0.0334255 0.19148 0.880278 0.973486 0.290043 0.29837 0.963968 0.987599 0.196745 0.080216 0.674406
M(:,4) % 4th column of M
ans = 0.952117 0.42151 0.616042 0.142489 0.332727
M(2:4,:) % the second through fourth rows of M as a submatrix
ans = 0.829158 0.0334255 0.19148 0.42151 0.0711905 0.880278 0.973486 0.290043 0.616042 0.177844 0.29837 0.963968 0.987599 0.142489 0.331631

Creating identity and zero matrices

eye(4)
ans = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
zeros(2,5)
ans = 0 0 0 0 0 0 0 0 0 0

Creating a vector or matrix full of ones

ones(2,5)
ans = 1 1 1 1 1 1 1 1 1 1

Creating a diagonal matrix

diag([1,2,3])
ans = 1 0 0 0 2 0 0 0 3

Creating off diagonal/triangular matrices

M = diag([10 20 30 40 50]) + diag([-3 -4 -5 -6],1)
M = 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 = inv(M); A
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

M = rand(4); e = eig(M)
e = (1.97833,0) (-0.414242,0) (0.325217,0.147848) (0.325217,-0.147848)
[v,e] = eig(M)
v = (0.344989,0) (0.805598,0) (0.231467,-0.0348854) (0.231467,0.0348854) (0.750594,0) (-0.588339,0) (0.0539678,0.198473) (0.0539678,-0.198473) (0.296671,0) (-0.0183577,0) (0.59778,-0.277168) (0.59778,0.277168) (0.479143,0) (0.0673176,0) (-0.684645,0) (-0.684645,-0) e = (1.97833,0) (0,0) (0,0) (0,0) (0,0) (-0.414242,0) (0,0) (0,0) (0,0) (0,0) (0.325217,0.147848) (0,0) (0,0) (0,0) (0,0) (0.325217,-0.147848)

Programming

for j=1:5 2*j+1 end
ans = 3 ans = 5 ans = 7 ans = 9 ans = 11
n = 3 v = 0 for j=1:n v(j) = j^2; end v
n = 3 v = 0 v = 1 4 9
n = 3; M = 0; for j=1:n for k=1:n M(j,k) = 2*j^2 + cos(k*j*pi/50); end end M
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

cd(); cd('cloud-examples/octave'); pwd() h = figure('visible', 'off'); surf(peaks); saveas(h,"figure1.png");
ans = /projects/1041134f-47fb-4a2c-8158-7ba2729f4bc0/cloud-examples/octave
%sage salvus.file("figure1.png")
cd(); cd('cloud-examples/octave'); pwd() M = rand(10); h = figure('visible', 'off'); plot(M); saveas(h,"figure2.png");
ans = /projects/1041134f-47fb-4a2c-8158-7ba2729f4bc0/cloud-examples/octave
%sage salvus.file("figure2.png")