Learn Octave Basics

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 examples 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 ordinary differential equations (ODE’s)

Installing Octave

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

% 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
clc

%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
clc

%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
clc

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

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

xlabel ("x");
ylabel ("sin (x)");
title ("2 plot example");

Area under a curve

Calculating the area under a curve requires the the installation of the “symbolic” package, see steps in the install Octave section. Below is a command line example to calculate the area enclosed between the x-axis, and the curve y = x^3−2x+5 and the coordinates x = 1 and x = 2. The required area is given by the following expression:

$\huge&space;\int^2_1&space;x^3-2x+5&space;\&space;dx$
pkg load symbolic % enter command to load pkg

x = sym("x"); % Define the variable x
f = x^3 - 2*x +5; %Define the function f(x)
c = [1, 0, -2, 5]; %Define the constants of the function
integral = polyint(c); %declare the integral using the function polyint()

A = polyval(integral, 2) - polyval(integral, 1); %Calculate the definite integral to give A

display('Area: '), disp(double(a)); %Display/print the answer in the command line screen

The above example uses the polynomial functions in Octave. An alternative method is to use the “inline” functions as follows:

pkg load symbolic % enter command to load pkg

x = sym("x"); % Define the variable x
f = inline("x^3 - 2*x +5"); %Define f(x) using the inline() function

[a, ierror, nfneval] = quad(f, 1, 2); % Set limits and solve using the quad() function

display('Area: '), disp(double(a)); %Display/print the answer in the command line screen

Note ignore the error “warning: inline is obsolete; use anonymous functions instead” and check the f appears in the workspace.

Solving Ordinary Differential Equations (ODE’s)

The symbolic package is required to solve ODE’s in Octave. The code below illustrates how to obtain the general and particular solutions for ODE’s of the form:

$\huge&space;\frac{dy}{dt}&space;=&space;a&space;y&space;\newline$

and

$\huge&space;\frac{d^2y}{dt^2}&space;=&space;a&space;y&space;\newline$

%Example of 1st ODE

%Solve the Equation (dy/dt) = a y

syms y(t) a  %declare variables

eqn = diff(y,t) == a*y; %specify the ODE using diff() and the equation by using ==

S = dslove(eqn) %solve the equation using dsolve() %The solution should print on screen

%Example of 1st ODE with Initial Conditions

%Solve the Equation (dy/dt) = a y with initial condition y(0) = 5

syms y(t) a  %declare variables

eqn = diff(y,t) == a*y; %specify the ODE using diff() and the equation by using ==

cond = y(0) == 5; %specify initial condition

S = dslove(eqn,cond) %solve the equation using "dsolve"

%Example of 2nd ODE

%Solve (d2y/dt2) = a y

syms y(t) a

eqn diff(y,t,2) == a*y; %Note the syntax for 2nd order ODE

ySol(t) = dsolve(eqn)