GNU Octave
Implementation of programming language MATLABGNU Octave (named after the former professor of lead developer) is a system for numerical computations which uses a highlevel programming language compatible with MATLAB.
Development of Octave started in 1988; initially it was intended for use in chemical reactor design course. In 1992 it became a separate software product, and started to be developed as such. In 1997 it became GNU Octave, i.e., it joined GNU Project. Since then it is distributed under GNU GPL license.
GNU Octave is written in C++ with STL. The scripts in this language are interpreted.
Octave syntax repeats the one of MATLAB in most cases, but extends it with some new possibilities:
 strings can be both singlequoted and doublequoted (MATLAB allows only single quotes).

MATLAB allows only
%
and%{ ... %}
comments, Octave adds#
и#{ ... #}
. 
Octave adds Clike operators
+= = *= /= \= ^= .*= ./= .\= .^= ! != ++ 
.  functions can be defined both in scripts and in the interpreter itself (MATLAB requires that functions are defined in special .m files).

Octave allows and recommends to use specific block ends:
endif
,endfor
,endwhile
etc. 
Octave’s rules of lines continuations are less strict: they require explicit continuations only when the lines are ambiguous. Both
\
and...
can be used as continuations. 
Octave has not only
trycatch
construct but alsounwind_protect
inspired by Lisp. This allows to createtrycatchfinally
structures for error handling. 
Octave allows indexing of temporary variables. For example,
x = 1:10; y = sin(x)(1:3)
is a valid sequence of commands in Octave, while MATLAB would require a temporary variable to storesin(x)
.
Links:
Examples:
Hello, World!:
Example for versions GNU Octave 3.2.3The first function is identical to the C one. The second one is Octavespecific.
printf("Hello, World!\n");
disp("Hello, World!");
Factorial:
Example for versions GNU Octave 3.2.3This example uses builtin function factorial
. Note that at this scale of magnitude the results are exact, but in general case Octave is not meant for arbitraryprecision computations, so huge values of factorial will be calculated approximately.
for i = 0 : 16
printf("%d! = %d\n", i, factorial(i));
endfor
Factorial:
Example for versions GNU Octave 3.2.3This example uses iterative factorial definition. Semicolons at the end of lines suppress the automated output of the calculated values (in this case of fact
) in interactive mode, so that the formatted output doesn’t get littered.
fact = 1;
for i = 0 : 16
printf("%d! = %d\n", i, fact);
fact *= i+1;
endfor
Factorial:
Example for versions GNU Octave 3.2.3This example uses recursive factorial definition.
function f = fact(n)
if (n <= 1)
f = 1;
else
f = n * fact(n  1);
endif
endfunction
for i = 0 : 16
printf("%d! = %d\n", i, fact(i));
endfor
Fibonacci numbers:
Example for versions GNU Octave 3.2.3This example uses recursive definition of Fibonacci numbers.
function f = fib(n)
if (n <= 1)
f = n;
else
f = fib(n  1) + fib(n  2);
endif
endfunction
for i = 1 : 16
printf("%d, ", fib(i));
endfor
disp("...");
Quadratic equation:
Example for versions GNU Octave 3.2.3Octave is suited for numeric computations, so it has builtin methods of solving typical problems, including finding the roots of a polynomial. To do this one can call roots
function for a vector of polynomial coefficients listed in order of descending powers (so the coefficient at the highest power is listed first).
roots([2 3 1])
Comments
]]>blog comments powered by Disqus
]]>