Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168703
Image: ubuntu2004

Using Octave as simple Calculator

%octave 1+1
ans = 2
3*4
ans = 12
2^2
ans = 4
3/4
ans = 0.75

Using arrays:

Defining an array:

x = [1,2,3]
x = 1 2 3

using the ":" operator

y = [1:3]
y = 1 2 3

vectors can be added:

x+y
ans = 2 4 6

vectors can be multiplied/diviede by scalars:

x*2
ans = 2 4 6
x/2
ans = 0.5 1 1.5

We can do elementwise operations with help of "." :

x.*y
ans = 1 4 9
x.^2
ans = 1 4 9
x./y
ans = 1 1 1

Matrices

Everything is a Matrix....

Defining a Matrix is quite the same as with vectors:

A = [5 1 0;1 5 1; 0 1 5]
A = 5 1 0 1 5 1 0 1 5

There are several other ways to build a Matrix:

B1 = zeros(2,3) B2 = ones(3) B3 = rand(3,4) Identity = eye(3)
B1 = 0 0 0 0 0 0 B2 = 1 1 1 1 1 1 1 1 1 B3 = 0.842419 0.474979 0.873249 0.579034 0.474746 0.920388 0.899179 0.558687 0.981904 0.403228 0.258319 0.15043 Identity = 1 0 0 0 1 0 0 0 1

A Matrix can also build as vector of vectors:

A = [[1,5,0];[1 5 1];[0,1,5]]
A = 1 5 0 1 5 1 0 1 5

We can multiply a matrix with another matrix:

A*B3
ans = 3.21615 5.07692 5.36914 3.37247 4.19805 5.48015 5.62746 3.5229 5.38427 2.93653 2.19077 1.31084

or with a vector:

x*A
ans = 3 18 17

but we have to be careful with dimensions!

A*x
error: operator *: nonconformant arguments (op1 is 3x3, op2 is 1x3)

use ' for adjoint and .' for transpose:

A*x.'
ans = 11 14 17
(I*B3)'
ans = (0,-0.842419) (0,-0.474746) (0,-0.981904) (0,-0.474979) (0,-0.920388) (0,-0.403228) (0,-0.873249) (0,-0.899179) (0,-0.258319) (0,-0.579034) (0,-0.558687) (0,-0.15043)
(I*B3).'
ans = (0,0.842419) (0,0.474746) (0,0.981904) (0,0.474979) (0,0.920388) (0,0.403228) (0,0.873249) (0,0.899179) (0,0.258319) (0,0.579034) (0,0.558687) (0,0.15043)

Programming

Comparisons

3==4
ans = 0
3<1
ans = 0
3>2
ans = 1
3!=4
ans = 1

the if statement

if condition blabla endif
if condition bla bla else bli bli endif

for loop:

for k = 1:N bla bla endfor

while:

while condition bla bla endwhile

defining of functions

function [val1,...valn] = my_func(input1,...,inputm) bla bla endfunction

examples:

function y = foo(x) y = x^2; endfunction
foo(2)
ans = 4

Let's do more complicated ones:

function y = my_sign(x) if x > 0 y = 1; else y = -1; endif endfunction
if x endif
my_sign(2)
ans = 1
my_sign(-1)
ans = -1
function y = add(n) y = 0; for k = 1:n y++; endfor endfunction
ans = -1 y = 0;
add(5)
ans = 5

Remark: The ++ Statement above is something, which differs octave from matlab. In matlab you would have to write y = y+1

Octave also supports the +=, -=, *=, /= statements

Another important difference: In Matlab every loop or function is closed with "end", in octave you can use end also, but the provided endif, endwhile, endfor etc. statements make the code more readible!

Of course octave has also disadvanteges, especially matlab has more features like simulink included!

To call a function in octave/matlab save it to an *.m file with the name of the function. If you start octave in the folder you can call the function just with it's name.

Another thing: Matlab has it's own nice interface. But for octave there are also some nice interfaces like QOctave which is written in Qt, or the good old emacs also supports octave.

Have Fun!