% These are the commands used for the first example. Text after the "%" % in each line is not interpreted by octave; this is a mechanism for % including comments. octave a = [5, 7, sqrt(2)] % Example of an array. a.*a % Product of arrays, element-by-element. a./a % Division element-by-element. b = [1,2,3;4,5,6] % Example of a matrix (rank 2 tensor). b(1,2) % Array indeces are row, column; UNIT OFFSET (sorry!) b' % b' is the transpose of b. b'(1,2) % b'*b % Matrix product. a*a' % Dot product. x = 0:0.2:2 % An array going from 0 to 2 in increments of 0.2. pi % What a surprise! x *= pi % This means the same as x = x*pi. pi = 3 % Constants can be overwritten. y = sin(x); % The semicolon suppresses output. z = cos(x); plot(x,y) % Just what you might expect. plot(x,z) % Nice, but the original plot disappeared. hold on % Subsequent plots will appear on the same axes. plot(x,y) hold off % Subsequent plots will replace the existing figure. plot(z,y) M = [x,y,z]; % What happens when we do this? size(M) % The result has 1 row and 33 columns. M % Aha! they were concatenated into a single array. M = [x',y',z']; % A matrix (rank 2 tensor) with 3 columns. size(M) M M(1:5,1) % Octave addresses arrays in "row, column" order. dlmwrite("myfile.txt", M, "\t") % Write M to a tab-delimited text file. N = dlmread("myfile.txt", "\t") % Read data pack in to array N. P = dlmread("myfile.txt", "\t", 2, 1) % 2, 1 are row, column offsets. sqrt(-1) % Octave variables can take complex values. i % j is also defined. exp(i*x') % complex numbers work as they should. c = [1, 0.1; 0.1, 1] % A square matrix. c^(-1) % Matrix inverse. (What would c.^(-1) mean?) eye(2) % Identity matrix (2x2). eye(2)/c % Another way to express the inverse of c. help plot % Accessing Octave's built-in documentation. % Below is the complete example with input and output, starting % from the Unix prompt (in Mac OS X; should look identical, or % very nearly so, in Linux). (base) bash 🚀 octave GNU Octave, version 6.4.0 Copyright (C) 2021 The Octave Project Developers. This is free software; see the source code for copying conditions. There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, type 'warranty'. Octave was configured for "x86_64-apple-darwin18.7.0". Additional information about Octave is available at https://www.octave.org. Please contribute if you find this software useful. For more information, visit https://www.octave.org/get-involved.html Read https://www.octave.org/bugs.html to learn how to submit bug reports. For information about changes from previous versions, type 'news'. ans = ********** If you can see this, ~kankel/.octaverc ran. ********* octave:1> example1 a = 5.0000 7.0000 1.4142 ans = 25.0000 49.0000 2.0000 ans = 1 1 1 b = 1 2 3 4 5 6 ans = 2 ans = 1 4 2 5 3 6 ans = 4 ans = 17 22 27 22 29 36 27 36 45 ans = 76 x = Columns 1 through 8: 0 0.2000 0.4000 0.6000 0.8000 1.0000 1.2000 1.4000 Columns 9 through 11: 1.6000 1.8000 2.0000 ans = 3.1416 x = Columns 1 through 8: 0 0.6283 1.2566 1.8850 2.5133 3.1416 3.7699 4.3982 Columns 9 through 11: 5.0265 5.6549 6.2832 pi = 3 ans = 1 33 M = Columns 1 through 8: 0 0.6283 1.2566 1.8850 2.5133 3.1416 3.7699 4.3982 Columns 9 through 16: 5.0265 5.6549 6.2832 0 0.5878 0.9511 0.9511 0.5878 Columns 17 through 24: 0.0000 -0.5878 -0.9511 -0.9511 -0.5878 -0.0000 1.0000 0.8090 Columns 25 through 32: 0.3090 -0.3090 -0.8090 -1.0000 -0.8090 -0.3090 0.3090 0.8090 Column 33: 1.0000 ans = 11 3 M = 0 0 1.0000 0.6283 0.5878 0.8090 1.2566 0.9511 0.3090 1.8850 0.9511 -0.3090 2.5133 0.5878 -0.8090 3.1416 0.0000 -1.0000 3.7699 -0.5878 -0.8090 4.3982 -0.9511 -0.3090 5.0265 -0.9511 0.3090 5.6549 -0.5878 0.8090 6.2832 -0.0000 1.0000 ans = 0 0.6283 1.2566 1.8850 2.5133 N = 0 0 1.0000 0.6283 0.5878 0.8090 1.2566 0.9511 0.3090 1.8850 0.9511 -0.3090 2.5133 0.5878 -0.8090 3.1416 0.0000 -1.0000 3.7699 -0.5878 -0.8090 4.3982 -0.9511 -0.3090 5.0265 -0.9511 0.3090 5.6549 -0.5878 0.8090 6.2832 -0.0000 1.0000 P = 9.5106e-01 3.0902e-01 9.5106e-01 -3.0902e-01 5.8779e-01 -8.0902e-01 1.2246e-16 -1.0000e+00 -5.8779e-01 -8.0902e-01 -9.5106e-01 -3.0902e-01 -9.5106e-01 3.0902e-01 -5.8779e-01 8.0902e-01 -2.4493e-16 1.0000e+00 ans = 0 + 1i ans = 0 + 1i ans = 1.0000 + 0i 0.8090 + 0.5878i 0.3090 + 0.9511i -0.3090 + 0.9511i -0.8090 + 0.5878i -1.0000 + 0.0000i -0.8090 - 0.5878i -0.3090 - 0.9511i 0.3090 - 0.9511i 0.8090 - 0.5878i 1.0000 - 0.0000i c = 1.0000 0.1000 0.1000 1.0000 ans = 1.0101 -0.1010 -0.1010 1.0101 ans = Diagonal Matrix 1 0 0 1 ans = 1.0101 -0.1010 -0.1010 1.0101 'plot' is a function from the file /usr/local/Cellar/octave/6.4.0/share/octave/6.4.0/m/plot/draw/plot.m -- plot (Y) -- plot (X, Y) -- plot (X, Y, FMT) -- plot (..., PROPERTY, VALUE, ...) -- plot (X1, Y1, ..., XN, YN) -- plot (HAX, ...) -- H = plot (...) Produce 2-D plots. Many different combinations of arguments are possible. The simplest form is plot (Y) where the argument is taken as the set of Y coordinates and the X coordinates are taken to be the range '1:numel (Y)'. If more than one argument is given, they are interpreted as plot (Y, PROPERTY, VALUE, ...) or plot (X, Y, PROPERTY, VALUE, ...) or plot (X, Y, FMT, ...) and so on. Any number of argument sets may appear. The X and Y values are interpreted as follows: * If a single data argument is supplied, it is taken as the set of Y coordinates and the X coordinates are taken to be the indices of the elements, starting with 1. * If X and Y are scalars, a single point is plotted. * 'squeeze()' is applied to arguments with more than two dimensions, but no more than two singleton dimensions. * If both arguments are vectors, the elements of Y are plotted versus the elements of X. * If X is a vector and Y is a matrix, then the columns (or rows) of Y are plotted versus X. (using whichever combination matches, with columns tried first.) * If the X is a matrix and Y is a vector, Y is plotted versus the columns (or rows) of X. (using whichever combination matches, with columns tried first.) * If both arguments are matrices, the columns of Y are plotted versus the columns of X. In this case, both matrices must have the same number of rows and columns and no attempt is made to transpose the arguments to make the number of rows match. Multiple property-value pairs may be specified, but they must appear in pairs. These arguments are applied to the line objects drawn by 'plot'. Useful properties to modify are "linestyle", "linewidth", "color", "marker", "markersize", "markeredgecolor", "markerfacecolor". The full list of properties is documented at Line Properties. The FMT format argument can also be used to control the plot style. It is a string composed of four optional parts: "<;displayname;>". When a marker is specified, but no linestyle, only the markers are plotted. Similarly, if a linestyle is specified, but no marker, then only lines are drawn. If both are specified then lines and markers will be plotted. If no FMT and no PROPERTY/VALUE pairs are given, then the default plot style is solid lines with no markers and the color determined by the "colororder" property of the current axes. Format arguments: linestyle '-' Use solid lines (default). '--' Use dashed lines. ':' Use dotted lines. '-.' Use dash-dotted lines. marker '+' crosshair 'o' circle '*' star '.' point 'x' cross 's' square 'd' diamond '^' upward-facing triangle 'v' downward-facing triangle '>' right-facing triangle '<' left-facing triangle 'p' pentagram 'h' hexagram color 'k' blacK 'r' Red 'g' Green 'b' Blue 'y' Yellow 'm' Magenta 'c' Cyan 'w' White ";displayname;" Here "displayname" is the label to use for the plot legend. The FMT argument may also be used to assign legend labels. To do so, include the desired label between semicolons after the formatting sequence described above, e.g., "+b;Key Title;". Note that the last semicolon is required and Octave will generate an error if it is left out. Here are some plot examples: plot (x, y, "or", x, y2, x, y3, "m", x, y4, "+") This command will plot 'y' with red circles, 'y2' with solid lines, 'y3' with solid magenta lines, and 'y4' with points displayed as '+'. plot (b, "*", "markersize", 10) This command will plot the data in the variable 'b', with points displayed as '*' and a marker size of 10. t = 0:0.1:6.3; plot (t, cos(t), "-;cos(t);", t, sin(t), "-b;sin(t);"); This will plot the cosine and sine functions and label them accordingly in the legend. If the first argument HAX is an axes handle, then plot into this axes, rather than the current axes returned by 'gca'. The optional return value H is a vector of graphics handles to the created line objects. To save a plot, in one of several image formats such as PostScript or PNG, use the 'print' command. See also: axis, box, grid, hold, legend, title, xlabel, ylabel, xlim, ylim, ezplot, errorbar, fplot, line, plot3, polar, loglog, semilogx, semilogy, subplot. Additional help for built-in functions and operators is available in the online version of the manual. Use the command 'doc ' to search the manual index. Help and information about Octave is also available on the WWW at https://www.octave.org and via the help@octave.org mailing list.