FAQ: Difference between revisions

Jump to navigation Jump to search
343 bytes added ,  28 July 2017
→‎Octave usage: Overhaul section. Make use of Template:Manual and syntax highlighting.
m (→‎What is Octave?: remove strange paragraph)
(→‎Octave usage: Overhaul section. Make use of Template:Manual and syntax highlighting.)
Line 263: Line 263:
==How do I execute an Octave script?==
==How do I execute an Octave script?==


First of all, make sure you understand [http://www.gnu.org/software/octave/doc/interpreter/Script-Files.html the difference between script files and function files]. If you want to execute a function defined in a file, just call the function like any other Octave function: <code>foo(arg1, arg2);</code>
First of all, make sure you understand [http://www.octave.org/doc/interpreter/Script-Files.html the difference between script files and function files]. If you want to execute a function defined in a file, just call the function like any other Octave function: <code>foo(arg1, arg2);</code>


To execute a script from within Octave, just type its name without the .m extension. Thus, if you have a script called <code>foo.m</code>, just type <code>foo</code> from within Octave to execute it. You have to make sure that the script is in your current path. Type <code>path</code> in Octave to see what this path is, and type <code>pwd</code> to print the working directory (where you're currently standing). The current working directory is referred to as "." in the <code>path</code>.
To execute a script from within Octave, just type its name without the <code>.m</code> extension. Thus, if you have a script called <code>foo.m</code>, just type <code>foo</code> from within the Octave command prompt to execute it. You have to make sure that the script is in your current working directory or in Octave's load path. Type {{manual|pwd}} to get the current working directory or type {{manual|path}} to see which paths belong to Octave's load path. The current working directory is referred to as "." in the output of {{manual|path}}.


If the script name has characters that are not valid for an Octave identifier, or if you do not want to use addpath to add the script's location to the current path, you can use the <code>run</code> function instead:
If the script name has characters that are not valid for an Octave identifier, or if you do not want to use {{manual|addpath}} to add the script's location to the current path, you can use the {{manual|run}} function instead:


  octave> run("Script Name With Spaces.m")
<syntaxhighlight lang="octave">
  octave> run("/opt/local/foo.m")
run ("Script Name With Spaces.m")
run ("/opt/local/foo.m")
</syntaxhighlight>


An alternative is to run the script from outside Octave by calling Octave from your operating system shell. Unlike calling the script from inside Octave, this also allows you to pass arguments from the shell into the script, which the script can access using the <code>argv</code> command:
An alternative is to run the script from outside Octave by calling Octave from your operating system shell. Unlike calling the script from inside Octave, this also allows you to pass arguments from the shell into the script, which the script can access using the {{manual|argv}} command:


   $ octave the-script.m arg1 arg2
   $ octave the-script.m arg1 arg2
Line 282: Line 284:
If you call the script from the shell and it's plotting, please note [[#When I try plotting from a script, why am I not seeing anything?|how to plot when running a script from the shell]].
If you call the script from the shell and it's plotting, please note [[#When I try plotting from a script, why am I not seeing anything?|how to plot when running a script from the shell]].


==How do I erase a figure?==  
==How do I close a figure?==  


closeplot();
To close the current figure type {{manual|close}} in the Octave command prompt.
closefig(number)


==How do I set the number of displayed decimals?==
==How do I set the number of displayed decimals?==


    octave:1> format long
You can control the number of displayed decimals using the {{manual|format}} command:
    octave:2> pi
    pi = 3.14159265358979
    octave:3> format short
    octave:4> pi
    pi = 3.1416


==How do I call an octave function from C++?==
<syntaxhighlight lang="octave">
>> format long
>> pi
pi = 3.14159265358979
>> format short
>> pi
pi = 3.1416
</syntaxhighlight>


* Here is an untested code snippet for calling rand([9000,1]), modified from a post by Herber Farnsworth? to help-octave on 2003-05-01:
==How do I call an Octave function from C++?==


#include <octave/oct.h>
Please read the manual https://www.gnu.org/software/octave/doc/interpreter/Calling-Octave-Functions-from-Oct_002dFiles.html.
...
ColumnVector NumRands(2);
NumRands(0) = 9000;
NumRands(1) = 1;
octave_value_list f_arg, f_ret;
f_arg(0) = octave_value(NumRands);
f_ret = feval("rand",f_arg,1);
Matrix unis(f_ret(0).matrix_value());


==How do I change color/line definition in gnuplot postscript?==
==How do I change color/line definition in gnuplot postscript?==
Line 350: Line 345:
==How do I tell if a file exists?==
==How do I tell if a file exists?==


Look at functions like exist, file_in_path.. and the other functions that their descriptions point to.
One can use the function {{manual|exist}} to tell if a regular file, say <code>foo.txt</code> exist in Octave's load path, or the current directory:
 
<syntaxhighlight lang="octave">
>> exist ("foo.txt", "file")  # 2, if file exists, 0 otherwise
ans = 2
</syntaxhighlight>


==How do I create a plot without a window popping up (plot to a file directly)?==
==How do I create a plot without a window popping up (plot to a file directly)?==


'''This only works with gnuplot as graphics_toolkit, NOT with fltk. See [https://savannah.gnu.org/bugs/?33180 Bug#33180]'''
<syntaxhighlight lang="octave">
 
figure (1, "visible", "off");
  figure(1, "visible", "off");
plot (sin (1:100));
  plot(sin(1:100));
print -deps "/tmp/sin.eps"
  print -deps "/tmp/sin.eps"
</syntaxhighlight>
 
One can set that behaviour as default:


  set(0, 'defaultfigurevisible', 'off');
One can set that behavior as default:


==How do I make Octave use more precision?==
<syntaxhighlight lang="octave">
set (0, "defaultfigurevisible", "off");
</syntaxhighlight>


Octave's default numerical type is IEEE 754 doubles, a.k.a. hardware floats. This type has 52 bits of precision or about 16 decimal digits. It's implemented in your computer's hardware, in your CPU, so it's '''fast'''. This type is assumed throughout for Octave's calculations.
==How do I increase Octave's precision?==


You can use a few other built-in types. The int64 type will have 63 bits of precision. One bit is used for the sign, but if you don't want to lose that bit, uint64 can be used instead. These types, however, cannot represent numbers as large as the default double type, and can only represent integers. Furthermore, there is no way to represent integer literals, so if you do
Octave's default numerical type is [https://en.wikipedia.org/wiki/IEEE_754 IEEE 754] binary64 , a.k.a. "double" or "hardware floats".  This type has a precision of 53 bits or about 16 decimal digits. It is supported by each modern computer hardware, so it is really '''fast'''. This type is assumed throughout for Octave's calculations.  If more precision was required, one can obtain a "few bits more" by using integer types, e.g. {{manual|uint64}}, but in general one cannot expect more precision from any '''fast''' numerical software. Just to visualize "how big" those numerical limits are, consider the following table:


<syntaxhighlight lang="Octave">uint64(18446744073709551610);</syntaxhighlight>
{| class="wikitable"
|+ Limits of some of Octave's data types obtained by {{manual|intmax}} and {{manual|flintmax}}.
|-
| <code>intmax ("uint64")</code>
| style="text-align: right;" | <code>18,446,744,073,709,551,615</code>
| <code>2^64-1</code>
|-
| <code>intmax ("int64")</code>
| style="text-align: right;" | <code>9,223,372,036,854,775,807</code>
| <code>2^63-1</code>
|-
| <code>flintmax ("double")</code>
| style="text-align: right;" | <code>9,007,199,254,740,992</code>
| <code>2^53</code>
|-
| <code>flintmax ("single")</code>
| style="text-align: right;" | <code>16,777,216</code>
| <code>2^24</code>
|}


the literal "18446744073709551610" first gets converted to a double precision type, so <code>uint64</code>'s additional precision is lost. Instead, initialize the <code>uint64</code> with smaller numbers and perform a computation to get the larger number you want. E.g.,
When working with other types than "double" in Octave, one has to make sure, that the first operand is converted to the desired type:


<syntaxhighlight lang="Octave">uint64(999999999999999) * 10000</syntaxhighlight>
<syntaxhighlight lang="Octave">
>> uint64 (2^53 + 1)
ans = 9007199254740992
>> uint64 (2^53) + 1
ans = 9007199254740993
</syntaxhighlight>


would produce value 9999999999999990000, which is close to the maximum possible value for the uint64 type, but can't be at the moment input directly, doing uint64(9999999999999990000), due to the mentioned error of rounding.
Notice the difference, in the first line the addition within the brackets is performed using double precision, therefore the result gets "truncated" to the maximum possible value without a warning.  The third line uses throughout uint64 precision.


Alternatively, one may use arbitrary precision arithmetic, which has as much precision as is practical to hold in your computer's memory. The ''symbolic'' package has a vpa() function for arbitrary precision arithmetic. Note that arbitrary precision arithmetic must be implemented '''in software''' which makes it much slower than hardware floats.
Consider carefully if your problem really needs more precision.  Often if you're running out of precision the problem lies fundamentally in your methods being [https://en.wikipedia.org/wiki/Numerical_stability numerically unstable], thus more precision will not help you here.


Consider carefully if your problem really needs more precision. Often if you're running out of precision the problem lies fundamentally in your methods being [http://en.wikipedia.org/wiki/Numerical_stability numerically unstable], so more precision will not help you here. If you absolutely must use arbitrary-precision arithmetic, you're at present better off using a CAS instead of Octave. An example of such a CAS is [http://sagemath.org Sage].
If you absolutely must have more precision, you're at present better off using a [https://en.wikipedia.org/wiki/Computer_algebra_system CAS] instead of Octave.  However, CAS or symbolic computations must be implemented '''in software''' which makes it much slower than hardware floats. An example of such a CAS is [http://www.sagemath.org/ Sage] or have a look at Octave's [[Symbolic package]].


==How do I run a Matlab P-file in Octave?==
==How do I run a Matlab P-file in Octave?==


You can't. Matlab P-files (files with a .p file extension), also known as P-code, are [https://en.wikipedia.org/wiki/Obfuscation_%28software%29 obfuscated] files than cannot be run outside of Matlab itself. The original source Matlab m-files that were used to generate the P-files should be used in Octave instead.
You can't. Matlab P-files (files with a <code>.p</code> file extension), also known as P-code, are [https://en.wikipedia.org/wiki/Obfuscation_%28software%29 obfuscated] files than cannot be run outside of Matlab itself. The original source Matlab m-files that were used to generate these P-files should be used in Octave instead.


There are no plans to support running P-files produced by Matlab in Octave.
There are no plans to support running P-files produced by Matlab in Octave.
Line 390: Line 413:
==How does Octave solve linear systems?==
==How does Octave solve linear systems?==


In addition to consulting Octave's source for the precise details, you can read the Octave manual for a complete high-level description of the algorithm that Octave uses to decide how to solve a particular linear system, e.g. how the backslash operator {{Codeline|A\x}} will be interpreted.  Sections [http://www.octave.org/doc/interpreter/Techniques-Used-for-Linear-Algebra.html Techniques Used for Linear Algebra] and [http://www.octave.org/doc/interpreter/Sparse-Linear-Algebra.html Linear Algebra on Sparse Matrices] from the manual describe this procedure.
In addition to consulting Octave's source for the precise details, you can read the Octave manual for a complete high-level description of the algorithm that Octave uses to decide how to solve a particular linear system, e.g. how the backslash operator <code>A \ x</code> will be interpreted.  Sections [http://www.octave.org/doc/interpreter/Techniques-Used-for-Linear-Algebra.html Techniques Used for Linear Algebra] and [http://www.octave.org/doc/interpreter/Sparse-Linear-Algebra.html Linear Algebra on Sparse Matrices] from the manual describe this procedure.


==How do I do X?==
==How do I do X?==


You are probably looking for the function ''lookfor''. This function searches the help text of all functions for a specific string and returns a list of functions. Note that by default it will only search the first line of the help text (check ''help lookfor'' at the octave prompt for more). The following example helps to find the function to calculate correlation coefficient in a matrix:
You are probably looking for the function {{manual|lookfor}}. This function searches the help text of all functions for a specific string and returns a list of functions. Note that by default it will only search the first line of the help text (check <code>help lookfor</code> at the octave prompt for more). The following example helps to find the function to calculate correlation coefficient in a matrix:


   octave> lookfor correlation
   >> lookfor correlation
   corr2              Returns the correlation coefficient between I and J.
   corr                Compute matrix of correlation coefficients.
  cor                Compute correlation.
   corrcoef            Compute a matrix of correlation coefficients.
   corrcoef            Compute correlation.
   spearman            Compute Spearman's rank correlation coefficient RHO.
   spearman            Compute Spearman's rank correlation coefficient RHO for each of the variables sp
  autocor            Return the autocorrelations from lag 0 to H of vector X.


Also, there's a high chance that the function name has a suggestive name, and so you can try auto-completion to get some hints. For the previous example, typing ''corr'' at the octave promp followed by pressing [Tab] twice would suggest the following:
Also, there's a high chance that the function name has a suggestive name, and so you can try auto-completion to get some hints. For the previous example, typing <code>corr</code> at the octave prompt followed by pressing the "Tab"-Key twice would suggest the following:


   octave> corr
   >> corr
   corr2    corrcoef
   corr2    corrcoef


Navigation menu