Learn Octave Using Scripts

GNU Octave is software featuring a high-level programming language, primarily intended for numerical computations. Octave helps in solving linear and nonlinear problems numerically, and for performing other numerical experiments using a language that is mostly compatible with MATLAB [Wikipedia]. Prior knowledge of using Octave is recommended. Please refer to the “Using Octave” document [Octave Wiki]. This is required to understand the syntax used in the scripts provided here.

Using the provided scripts will help you getting started with Octave in order to solve equations related to science and engineering problems. The topics covered will include;

  • Solving problems involving matrices and vectors.
  • Plotting graphs and calculating the area under the curve.
  • Solving multiple integral problems.
  • Solving ordinary differential equations (ODE’s)

Installing Octave

  1. Visit [https://www.gnu.org/software/octave/download] and download the file with the extension -installer.exe for Win10.
  2. Solving ODE’s will require the installation of an additional library known as Symbolic. Download this library [https://octave.sourceforge.io/symbolic/]. Note: Do Not attempt to open the file after download.
  3. Move the downloaded file to the Octave working directory. You can find the path to this folder from the Octave GUI interface or by typing “ls” in the command line.
  4. install the package using the command “pkg install” followed by the name [e.g pkg install symbolic-1.0.0.tar.gz].
  5. Check that the package has installed using the command “pkg list”.
  6. To load the package, run ‘pkg load symbolic’ from the Octave prompt. Note that this is required every time you open Octave.
  7. If the library has installed correctly you should be able to load symbolic without any messages.

Matrices and Vectors

MATLAB is in abbreviation for Matrix Laboratory. The core functionality of MATLAB, and it’s clone OCTAVE, is matrix manipulation. The example below shows how to solve systems of equations with linear algebra operations on vectors and matrices. This example could be done using the command line or as a script.

clear all %Heading
clc %Heading

% Solve a matrix of the Ax = b system, where A = 3x3 matrix and b = column matrix 

b = [4; 9; 2] % Input column matrix/vector. Use in command line 
A = [ 3 4 5; 1 3 1; 3 5 9 ] % Input 3x3 matrix. Use in command line 

x = A \ b     % Solve the system Ax = b. Use in command line 

To use the command line, simply launch Octave GUI and type the syntax labeled “Use in command line” followed by enter. Notice that when you press enter your matrix will appear on the workspace. The commands should be executed in the order given above. First declare your matrices, then do the algebraic manipulations. This code above could also be used as a script by copying pasting the above code into the script editor, more details on running scripts are covered in the next section.

Plotting Graphs

Here, we will demonstrate how to plot graphs using a script file. Provided below is an example of a script file used to plot the graph of sin(x).

  1. Launch Octave (GUI)
  2. Click [New Script] which will open the editor window.
  3. Copy and paste the following code, then click on [Save File and Run/Continue] and an output window will appear with the plot
clear all %Headings required by octave

%simple script for plotting y = sin(x) 

x = -10:0.1:10;   %define the range of x 

plot (x, sin (x)); % use the plot(x,y) command to create a 2D plot 
xlabel ("x"); %insert x-axis label
ylabel ("sin (x)"); %insert y-axis label
title ("Simple 2-D Plot"); % insert plot title 

Similarly, we can plot algebraic expressions as well. Notice the the slight difference in defining y for the “plot” command:

clear all %Headings required by octave

%simple script for plotting y = x^2 - 2 

x = -10:0.1:10;  
plot (x, x.^(2)-2); 
xlabel ("x");
ylabel ("y");
title ("Simple 2-D Plot");

Combining two plots on the same graph is done using the “hold” command, as shown in the script below:

clear all

%Plot y = sin (x) and y = sin 2x 

x = -10:0.1:10;
plot (x, sin (x));
plot (x, sin (2*x));

xlabel ("x");
ylabel ("sin (x)");
title ("2 plot example");
%d bloggers like this: