GNU Octave 3.2.3
Version of implementation GNU Octave of programming language MATLABA version of GNU Octave.
Examples:
Hello, World! - MATLAB (379):
The first function is identical to the C one. The second one is Octave-specific.
printf("Hello, World!\n");
disp("Hello, World!");
Factorial - MATLAB (380):
This example uses built-in function factorial
. Note that at this scale of magnitude the results are exact, but in general case Octave is not meant for arbitrary-precision computations, so huge values of factorial will be calculated approximately.
for i = 0 : 16
printf("%d! = %d\n", i, factorial(i));
endfor
Factorial - MATLAB (381):
This 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 - MATLAB (382):
This 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 - MATLAB (383):
This 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 - MATLAB (384):
Octave is suited for numeric computations, so it has built-in 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
]]>