Octave:2> Scripts and Algorithm

This entry is part 7 of 11 in the series GNU Octave

Working on the command line is not always practical. We often make typos, and just to correct that we need to rewrite the expression. In almost all cases re-usability is of great concern. We need to keep our program steps safely saved in correct sequence for reuse as well as future reference. We can look it into command history, but neither its practical nor it has an infinite amount of memory. Also it is hard to find a particular instance among all listed one below another.We need to change the settings, modify a variable. It is not convenient to retype everything. Scripts are the perfect solution or such cases which contains expressions in correct sequence. It can be easily edited, and all commands/functions in a script run together in sequential manner. This allow us to repeat same calculations or complete the work at the later time. The correct sequence of commands written in script to achieve an specific goal are algorithms. In this article we are going to discuss how to use scripts to implement algorithm. We encourage readers with the intention to learn octave to execute and test the tasks marked as “Ex:” in the article for better understanding.

Script

A scripting or script language is a programming language for a special run-time environment that automates the execution of tasks. That is correct, Octave is a scripting language. Unlike “C/C++” each expression written in a script file can be run independently on command line. Like python, each expression is compiled one at a time.

Editor

When Octave is being used interactively, Editor window can be found, tabbed along with command window. If not, it can be made visible using window menu. All expressions that are supposed to run sequentially should be typed and file should be saved before running the script by clicking play button on top of editor.

Editor Octave 5.2.0: Script to calculate Area of circle
Editor Octave 5.2.0: Script to calculate Area of circle.

It is not always needed to run octave interactively. We can do all the steps needed from command-line itself.

octave:1> mkdir basic
ans = 1
octave:2> cd basic
octave:3> edit areaCircle.m
octave:4> sh: 1: emacs: not found

As I am not using emacs hence a error is visible. But a empty file is already created, which can be edited using editor of choice. run command can be used to execute the script. Or directly calling the nae of script without extension also does the job.

octave:5> run areaCircle.m

Area =98.5203
octave:6> areaCircle

Area =98.5203

Guidelines for writing script

  • It is better to start script for credential (as comment), e.g. Author name, Date, Description etc. This will help in future while revisiting your own or others script.
  • Octave accepts both “%” and “#” as indicator of comment, but for Matlab compatibility it is advised to use only percent sign “%” to indicate start of comment.
  • Octave and Matlab will save all the values of variables in workspace, hence to remove any conflict it is better to clear the workspace with clear all. This is important but not necessary. Same goes or clc (clear the screen) and close all.
  • Unless it is intended to debug the script at each stage, use semicolon “;” to suppress the output.
  • Always document your script with proper comments for ease of use at later stage.
  • Once script is written completely, it should be saved with “.m” extension.
  • Octave works in a certain workspace, so it is recommended to create separate directory for new script. This is a good practice but not necessary.

Help

From first line of comment till a empty line is observed can be seen as help. Hence it is wise to write Top few lines of script to explain the algorithm which can act as help to user.

octave:7>help areaCircle
'areaCircle' is a script from the file /home/ubuntu/xxxxxxxxxx/Octave/basic/areaCircle.m

 Comments are written with a "%" sign in Octave or matlabroot
 Although Octave also support "#" for comment as python.
 Any expression when followed by semicolon ";" have their output suppressed. Use this for clean output.


Additional help for built-in functions and operators is
available in the online version of the manual.  Use the command
'doc <topic>' 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.
Ex: Edit the script so that output from help function can be useful to user.
Ex: Current script uses fixed input parameter Radius, edit the script to get a value from user, using input() function. for help run help input at command line.

Algorithm

What is algorithm?
An algorithms a finite sequence of unambiguous operations or instructions which allows to solve a problem and get an output.

By definition, everything written in script that is interpreted by interpreter is algorithm. That means comments are not part of it. Any algorithm can be divided in three parts:

  1. Input Parameter
  2. Required calculations
  3. Output
Ex: In the above image separate the expressions as per the three parts of algorithm. Ignore the part where workspace is cleared.

The example given in image is a simple one where only 3 expression are used. In current article we worked with the users script that helps in organizing the work in relation to the input of the algorithms. In the next article we will look into conditional execution, the essential tools for coding more sophisticated algorithms. Let us know your query and questions in comment section.