Octave basic

By xngo on February 21, 2019

Variables

octave-3.2.4.exe:5> x=2;
octave-3.2.4.exe:6> y=3;
octave-3.2.4.exe:7> x*y
ans =  6


Vectors

octave-3.2.4.exe:15> v=[1 2 3]
v =
 
   1   2   3
 
octave-3.2.4.exe:16> v*2
ans =
 
   2   4   6


Matrix

octave-3.2.4.exe:13> m=[1 2 3; 3 4 5; 6 7 8]
m =
 
   1   2   3
   3   4   5
   6   7   8
 
octave-3.2.4.exe:14> m*3
ans =
 
    3    6    9
    9   12   15
   18   21   24


User-defined functions

octave-3.2.4.exe:18> function retVal_1 = my_sum( a, b )
> retVal_1 = a + b;
> endfunction
octave-3.2.4.exe:19> my_sum(2,3)
ans =  5


Solving a linear function

octave-3.2.4.exe:30> function y = f(x)
> y=3*x + 4;
> endfunction
octave-3.2.4.exe:31> f(2)
ans =  10
octave-3.2.4.exe:32> f([1 2 3 4])
ans =
 
    7   10   13   16
 
octave-3.2.4.exe:33> 2*f([1 2 3 4])
ans =
 
   14   20   26   32


Plot functions

octave-3.2.4.exe:38> # -15 = lower bound, 15 = upper bound, 3 = increment value
octave-3.2.4.exe:39> x = -15:3:15;
octave-3.2.4.exe:40> plot(x, f(x));
octave-3.2.4.exe:41>


About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.