1,852
edits
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. | 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 | 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 | 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: | ||
<syntaxhighlight lang="octave"> | |||
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 | 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 | ==How do I close a figure?== | ||
To close the current figure type {{manual|close}} in the Octave command prompt. | |||
==How do I set the number of displayed decimals?== | ==How do I set the number of displayed decimals?== | ||
You can control the number of displayed decimals using the {{manual|format}} command: | |||
= | <syntaxhighlight lang="octave"> | ||
>> format long | |||
>> pi | |||
pi = 3.14159265358979 | |||
>> format short | |||
>> pi | |||
pi = 3.1416 | |||
</syntaxhighlight> | |||
==How do I call an Octave function from C++?== | |||
Please read the manual https://www.gnu.org/software/octave/doc/interpreter/Calling-Octave-Functions-from-Oct_002dFiles.html. | |||
==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?== | ||
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)?== | ||
<syntaxhighlight lang="octave"> | |||
figure (1, "visible", "off"); | |||
plot (sin (1:100)); | |||
print -deps "/tmp/sin.eps" | |||
</syntaxhighlight> | |||
One can set that behavior as default: | |||
= | <syntaxhighlight lang="octave"> | ||
set (0, "defaultfigurevisible", "off"); | |||
</syntaxhighlight> | |||
Octave's | ==How do I increase Octave's precision?== | ||
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: | |||
< | {| 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> | |||
|} | |||
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( | <syntaxhighlight lang="Octave"> | ||
>> uint64 (2^53 + 1) | |||
ans = 9007199254740992 | |||
>> uint64 (2^53) + 1 | |||
ans = 9007199254740993 | |||
</syntaxhighlight> | |||
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. | |||
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. | |||
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 | 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 | 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 | 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: | ||
>> lookfor correlation | |||
corr Compute matrix of correlation coefficients. | |||
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 | |||
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 | 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: | ||
>> corr | |||
corr2 corrcoef | corr2 corrcoef | ||