Commonly used functions in $\textit{GNU Octave}$

In [1]:
graphics_toolkit qt; # when using Octave-GUI include this line. If working with octave-cli on jupyter exclude this
error: graphics_toolkit: qt toolkit is not available
error: called from
    graphics_toolkit at line 81 column 5
  • Create an array as input for the functions
In [2]:
x = [-2, 1.5, 0, 1/2*pi, pi]; disp(["x = : ", num2str(x)]);
x = : -2         1.5           0      1.5708      3.1416
  • Find out the absolute value of all the elements in array $\textbf{x}$
In [3]:
xa = abs(x);  disp(["x = : ", num2str(x), "\nabs(x) = :", num2str(xa)]);
x = : -2         1.5           0      1.5708      3.1416
abs(x) = :2         1.5           0      1.5708      3.1416
  • Trignometric operations with element of array $\textbf{x}$ as input
In [4]:
b = cos(x); disp(b);
c = sin(x); disp(c);
d = tan(x); disp(d);
  -4.1615e-01   7.0737e-02   1.0000e+00   6.1230e-17  -1.0000e+00
  -0.90930   0.99749   0.00000   1.00000   0.00000
   2.1850e+00   1.4101e+01   0.0000e+00   1.6331e+16  -1.2246e-16
  • Inverse trignometric functions with $\textbf{b}$, $\textbf{c}$ and $\textbf{d}$ as input
In [5]:
bi = acos(b); disp(bi); 
ci = asin(c); disp(ci);
di = atan(d); disp(di);
 disp(["x = : ", num2str(x)]);
   2.00000   1.50000   0.00000   1.57080   3.14159
  -1.14159   1.50000   0.00000   1.57080   0.00000
   1.14159   1.50000   0.00000   1.57080  -0.00000
x = : -2         1.5           0      1.5708      3.1416
  • Inverse tan has another function: $\textbf{atan2}$, which takes appropriate quadrant into consideration.
In [6]:
e = atan2(-1, 1); disp(e);
f = atan2(1, 1); disp(f);
disp(pi/4)
-0.78540
 0.78540
 0.78540
  • Hyperbolic functions can also be computed: $\cosh (x) = \frac{\left( \exp(x) + \exp(-x)\right)}{2}$
In [7]:
y = [-2, -1, 1, 2];
In [8]:
ys = sinh((y)); disp(ys);
  -3.6269  -1.1752   1.1752   3.6269
In [9]:
ysp1 = exp(y); ysp2 = exp(-y);
ys2 = 1/2*(ysp1 - ysp2); disp(ys); disp(ys2);
  -3.6269  -1.1752   1.1752   3.6269
  -3.6269  -1.1752   1.1752   3.6269
  • In python $\textbf{j}$ is used for complex part
  • Let us create $\textbf{c}$ variable with complex value and perform the following operations:
    • Absolute value of $\textbf{c}$
    • Real part of $\textbf{c}$
    • Imaginary part of $\textbf{c}$
    • Conjugate of $\textbf{c}$
In [10]:
c = 2 + 3j;
disp(abs(c));
disp(sqrt(2^2 + 3^2));
disp(real(c));
disp(imag(c));
disp(conj(c));
 3.6056
 3.6056
 2
 3
 2 - 3i
  • We can also perform the same operations on an array of values
In [11]:
d = [2+3j, 1-1j, 4+pi*1j]; disp(d);
   2.0000 + 3.0000i   1.0000 - 1.0000i   4.0000 + 3.1416i
In [12]:
dc = conj(d); disp(dc)
   2.0000 - 3.0000i   1.0000 + 1.0000i   4.0000 - 3.1416i
  • The above example shows the use of inbuilt functions of $\textit{Octave}$ to obtain the conjugate of a complex number.

Plotting

  • Let us see how plotting works in Octave
In [13]:
x = linspace(-2, 2, 100);
y = cos(2*pi*x);
z = sin(2*pi*x);
  • Let us plot $\textrm{sin}(\textbf{x})$ and $\textrm{cos}(\textbf{x})$
  • The plotting tool is quite flexible and you can modify various things to make your plots presentable
  • Let's look at some of the modifications
In [14]:
# We specify the figure window in which plot will be present
figure(1);

# We plot cos(X) as a dotted line. We also specify the size of the "." marker as 10
plot(x, y,'.',"markersize",10);
hold on;

# We plot tan(x) and sin(x) as continuous lines with a specified linewidth of 5
plot(x, z,"linewidth", 5);
plot(x, tan(x),"linewidth", 5);

# We specify the labels for x and y axis with a specified fontsize and font name
xlabel("x", "fontsize",20,"fontname", "TimesNewRoman");
ylabel("y","fontsize",20,"fontname", "TimesNewRoman"); 

# We specify the plot title with a specified fontsize and font name
title("Some functions plotted", "fontsize",20,"fontname", "TimesNewRoman");

# We specify the extent of x and y axis 
ylim([-1,1]);
xlim([0, 2]);

# We specify the legend values and also change the position of the legend
h = legend ("cos(x)", "sin(x)", "tan(x)");
legend (h, "location", "southwest");

# We specify the font size and font name for the legend and the axes
set (h, "fontsize", 14,"fontname", "TimesNewRoman");
set(gca, "fontsize",20,"fontname", "TimesNewRoman")

Special functions in Octave

  • Octave has many predefined functions which are used repeatedly in scientific computations
    • Let us look at the $\textrm{0th}$ order bessel function of first kind
      • $\textbf{jv(n,x)}$ outputs the $\textbf{nth}$ order bessel function of first kind queried at $\textbf{x}$
In [15]:
x = linspace(0, 10, 100);
y = besselj(0,x);
In [16]:
figure(1);
plot(x, y, "linewidth", 5); 
xlabel("x","fontsize", 20,"fontname", "TimesNewRoman"); ylabel("J_0(x)","fontsize", 20,"fontname", "TimesNewRoman")
set(gca,'FontSize',20,"fontname", "TimesNewRoman")

# Enable the grid
grid on;
  • We can now plot functions of different orders
    • We loop over the order using for loop
  • Here we make use of the $\textbf{sprintf}$ command to specify the legend values while plotting
    • The syntax for legend entries needs to followed exactly as shown i.e. $\textrm{"-;legend entry;"}$
  • We also make us of $\textbf{hold}$ command to plot various functions in the same plot window
In [17]:
for i = 0:4
    y = besselj(i ,x);
    figure(2);
    plot(x, y, sprintf("-;J_{%d};",i), "linewidth",5); # specify the legend entry inline
    hold on     # hold on the plot window for plotting all the functions in same window    
end

# plot the y=0 line i.e. the x axis
plot(x, 0.*x, '--r', "linewidth",2);

hold off;

ax = gca();
set(gca,'FontSize',20,"fontname", "TimesNewRoman");

# Enable the legend and modify its position font size and font name
h = legend();
legend (h, "location", "southwest");
set (h, "fontsize", 10,"fontname", "TimesNewRoman");
legend boxoff

# specify the x and y labels
ylabel("Bessel function","fontsize", 20,"fontname", "TimesNewRoman");
xlabel("x","fontsize", 20,"fontname", "TimesNewRoman");
grid on;

Defining your own function

  • We can also define our own functions
  • Let us define a function to calculate the circumference and area of a circle with the radius $\textbf{r}$ as input
  • $\textbf{NOTE}$ while writing a function inside an octave script you need to first execute a token that is not the function keyword.
    • Hence in your script.m files you can write "1;" as a token before writing the function definition so that octave interprets it as a script and not as a functionfile
In [18]:
function [area,circ] = circ_properties(r)
    # local variables - scope is limited to the definition of the function
    area  = pi*r.^2;
    circ = 2*pi*r;
end
  • Here $\textrm{circ_properties}$ the name of the function with input as $\textbf{r}$
  • We calculate the area and circumference and store them in variables and then return those values

  • We can now call the function for any radius

  • We store the return value in the variable $\textrm{x,y}$
In [19]:
[x ,y] = circ_properties(1)
x =  3.1416
y =  6.2832
In [20]:
printf("For a circle with radius: %f\n", 1)
printf("The area is: %f\n", x);
printf("Circumferance: %f\n", y);
For a circle with radius: 1.000000
The area is: 3.141593
Circumferance: 6.283185
  • Let's create a function which takes two inputs, $\textbf{x}$ and $\textbf{c}$ and creates a ploynomial $x^2 + cx + 2c$
  • We return the value of function for the given $\textbf{x}$ and $\textbf{c}$
  • We also return a scalar to show how to return mixed type of variables
In [21]:
function [y1, y2] = fx(x, c)
    y1 =  x.^2 + c*x + 2*c; 
    y2= exp(c);
end
In [22]:
x = linspace(-3, 3);
for i=-2:3
    [y, e] = fx(x, i);
    plot(x, y, sprintf("-;c = %d;", i), "linewidth",5);
    hold on;
end
disp(e);
disp(exp(2));
set(gca,'FontSize',20, "fontname", "TimesNewRoman");

h = legend();
legend (h, "location", "northwest");
set (h, "fontsize", 10,"fontname", "TimesNewRoman");
 20.086
 7.3891

File IO

In [23]:
disp(x);
 Columns 1 through 7:

  -3.000000  -2.939394  -2.878788  -2.818182  -2.757576  -2.696970  -2.636364

 Columns 8 through 14:

  -2.575758  -2.515152  -2.454545  -2.393939  -2.333333  -2.272727  -2.212121

 Columns 15 through 21:

  -2.151515  -2.090909  -2.030303  -1.969697  -1.909091  -1.848485  -1.787879

 Columns 22 through 28:

  -1.727273  -1.666667  -1.606061  -1.545455  -1.484848  -1.424242  -1.363636

 Columns 29 through 35:

  -1.303030  -1.242424  -1.181818  -1.121212  -1.060606  -1.000000  -0.939394

 Columns 36 through 42:

  -0.878788  -0.818182  -0.757576  -0.696970  -0.636364  -0.575758  -0.515152

 Columns 43 through 49:

  -0.454545  -0.393939  -0.333333  -0.272727  -0.212121  -0.151515  -0.090909

 Columns 50 through 56:

  -0.030303   0.030303   0.090909   0.151515   0.212121   0.272727   0.333333

 Columns 57 through 63:

   0.393939   0.454545   0.515152   0.575758   0.636364   0.696970   0.757576

 Columns 64 through 70:

   0.818182   0.878788   0.939394   1.000000   1.060606   1.121212   1.181818

 Columns 71 through 77:

   1.242424   1.303030   1.363636   1.424242   1.484848   1.545455   1.606061

 Columns 78 through 84:

   1.666667   1.727273   1.787879   1.848485   1.909091   1.969697   2.030303

 Columns 85 through 91:

   2.090909   2.151515   2.212121   2.272727   2.333333   2.393939   2.454545

 Columns 92 through 98:

   2.515152   2.575758   2.636364   2.696970   2.757576   2.818182   2.878788

 Columns 99 and 100:

   2.939394   3.000000
In [24]:
disp(y)
 Columns 1 through 8:

    6.0000    5.8219    5.6511    5.4876    5.3315    5.1827    5.0413    4.9073

 Columns 9 through 16:

    4.7805    4.6612    4.5491    4.4444    4.3471    4.2571    4.1745    4.0992

 Columns 17 through 24:

    4.0312    3.9706    3.9174    3.8714    3.8329    3.8017    3.7778    3.7612

 Columns 25 through 32:

    3.7521    3.7502    3.7557    3.7686    3.7888    3.8163    3.8512    3.8935

 Columns 33 through 40:

    3.9431    4.0000    4.0643    4.1359    4.2149    4.3012    4.3949    4.4959

 Columns 41 through 48:

    4.6042    4.7199    4.8430    4.9734    5.1111    5.2562    5.4086    5.5684

 Columns 49 through 56:

    5.7355    5.9100    6.0918    6.2810    6.4775    6.6814    6.8926    7.1111

 Columns 57 through 64:

    7.3370    7.5702    7.8108    8.0588    8.3140    8.5767    8.8466    9.1240

 Columns 65 through 72:

    9.4086    9.7006   10.0000   10.3067   10.6208   10.9421   11.2709   11.6070

 Columns 73 through 80:

   11.9504   12.3012   12.6593   13.0248   13.3976   13.7778   14.1653   14.5601

 Columns 81 through 88:

   14.9624   15.3719   15.7888   16.2130   16.6446   17.0836   17.5298   17.9835

 Columns 89 through 96:

   18.4444   18.9128   19.3884   19.8714   20.3618   20.8595   21.3646   21.8770

 Columns 97 through 100:

   22.3967   22.9238   23.4582   24.0000
In [25]:
disp(size(x)); disp(size(y))
     1   100
     1   100
  • The following line shows how the data can be put inside the file "datafile.txt"
In [26]:
fid = fopen('Datafile.txt', 'w+');
fprintf(fid,"%f \t %f\n", x, y);
fclose(fid);
In [27]:
d = load('Datafile.txt');
disp(size(d))
   100     2
  • Concatenate the two arrays in a column wise fashion to output them to a file in a "human readable" format.
In [28]:
e = [x; y]; disp(e); 

fid = fopen('data_human.txt', 'w+');
fprintf(fid,"%1.4f %1.4f \n", e);
fclose(fid);
 Columns 1 through 6:

   -3.000000   -2.939394   -2.878788   -2.818182   -2.757576   -2.696970
    6.000000    5.821855    5.651056    5.487603    5.331497    5.182736

 Columns 7 through 12:

   -2.636364   -2.575758   -2.515152   -2.454545   -2.393939   -2.333333
    5.041322    4.907254    4.780533    4.661157    4.549128    4.444444

 Columns 13 through 18:

   -2.272727   -2.212121   -2.151515   -2.090909   -2.030303   -1.969697
    4.347107    4.257117    4.174472    4.099174    4.031221    3.970615

 Columns 19 through 24:

   -1.909091   -1.848485   -1.787879   -1.727273   -1.666667   -1.606061
    3.917355    3.871442    3.832874    3.801653    3.777778    3.761249

 Columns 25 through 30:

   -1.545455   -1.484848   -1.424242   -1.363636   -1.303030   -1.242424
    3.752066    3.750230    3.755739    3.768595    3.788797    3.816345

 Columns 31 through 36:

   -1.181818   -1.121212   -1.060606   -1.000000   -0.939394   -0.878788
    3.851240    3.893480    3.943067    4.000000    4.064279    4.135904

 Columns 37 through 42:

   -0.818182   -0.757576   -0.696970   -0.636364   -0.575758   -0.515152
    4.214876    4.301194    4.394858    4.495868    4.604224    4.719927

 Columns 43 through 48:

   -0.454545   -0.393939   -0.333333   -0.272727   -0.212121   -0.151515
    4.842975    4.973370    5.111111    5.256198    5.408632    5.568411

 Columns 49 through 54:

   -0.090909   -0.030303    0.030303    0.090909    0.151515    0.212121
    5.735537    5.910009    6.091827    6.280992    6.477502    6.681359

 Columns 55 through 60:

    0.272727    0.333333    0.393939    0.454545    0.515152    0.575758
    6.892562    7.111111    7.337006    7.570248    7.810836    8.058770

 Columns 61 through 66:

    0.636364    0.696970    0.757576    0.818182    0.878788    0.939394
    8.314050    8.576676    8.846648    9.123967    9.408632    9.700643

 Columns 67 through 72:

    1.000000    1.060606    1.121212    1.181818    1.242424    1.303030
   10.000000   10.306703   10.620753   10.942149   11.270891   11.606979

 Columns 73 through 78:

    1.363636    1.424242    1.484848    1.545455    1.606061    1.666667
   11.950413   12.301194   12.659320   13.024793   13.397612   13.777778

 Columns 79 through 84:

    1.727273    1.787879    1.848485    1.909091    1.969697    2.030303
   14.165289   14.560147   14.962351   15.371901   15.788797   16.213039

 Columns 85 through 90:

    2.090909    2.151515    2.212121    2.272727    2.333333    2.393939
   16.644628   17.083563   17.529844   17.983471   18.444444   18.912764

 Columns 91 through 96:

    2.454545    2.515152    2.575758    2.636364    2.696970    2.757576
   19.388430   19.871442   20.361800   20.859504   21.364555   21.876951

 Columns 97 through 100:

    2.818182    2.878788    2.939394    3.000000
   22.396694   22.923783   23.458219   24.000000
In [ ]: