Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Lecture4 Octave Introduction

Octave is a high-level language designed for numerical computations, including solving equations and statistical analysis, and can be used for automated data processing. It features a graphical user interface with an Integrated Development Environment (IDE) and a command-line interface, providing extensive help options and a workspace for managing variables. The software is freely redistributable and supports various mathematical operations, matrix creation, and graphical displays.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lecture4 Octave Introduction

Octave is a high-level language designed for numerical computations, including solving equations and statistical analysis, and can be used for automated data processing. It features a graphical user interface with an Integrated Development Environment (IDE) and a command-line interface, providing extensive help options and a workspace for managing variables. The software is freely redistributable and supports various mathematical operations, matrix creation, and graphical displays.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Octave Introduction

A Brief Introduction to Octave

 Octave is a high-level language primarily intended for numerical computations.


 Typically used for such problems as solving linear and nonlinear equations,
numerical linear algebra, statistical analysis, and for performing other numerical
experiments.
 Can also be used as a batch-oriented language for automated data processing.
 Octave is freely redistributable software.
 This manual provides comprehensive documentation on how to install, run, use,
and extend GNU Octave. https://octave.org/doc/v6.2.0/
 Octave is a good language for prototyping in order to get your machine learning
algorithms working quickly.
 The current version of Octave executes in a graphical user interface (GUI).
 The GUI hosts an Integrated Development Environment (IDE) which includes a
code editor with syntax highlighting, built-in debugger, documentation browser, as
well as the interpreter for the language itself.
 A command-line interface for Octave is also available.

© Prof Suvendi Rimer, UJ


Octave Overview

 On most systems, Octave is started with the shell command ‘octave’.


 This starts the graphical user interface. The central window in the GUI is the
Octave command-line interface.
 In this window Octave displays an initial message and then a prompt
indicating it is ready to accept input.
 If you have chosen the traditional command-line interface then only the
command prompt appears in the same window that was running a shell. In
either case, you can immediately begin typing Octave commands.
 If you get into trouble, you can usually interrupt Octave by typing Control-C
(written C-c for short). C-c gets its name from the fact that you type it by holding
down CTRL and then pressing c. Doing this will normally return you to Octave’s
prompt.
 To exit Octave, type quit or exit at the Octave prompt.
 On systems that support job control, you can suspend Octave by typing Ctrl-Z.

© Prof Suvendi Rimer, UJ


© Prof Suvendi Rimer, UJ
Help menu

 Octave has an extensive Help system.


 Use the Help Browser by selecting the help icon from the desktop toolbar;
 This provides a normal help environment with complete documentation.
 It is possible to search the online manuals for any combination of keywords using
logical operators.

© Prof Suvendi Rimer, UJ


Help on command line

 There are also command-line orientated ways to get help:


 If you know the function but not how to use it, or want to see a list of commands,
type “help” or “help functionname” where functionname is the name of the
function. This will show a description of the function.
 Typing help disp or help list_in_columns to display output on terminal.

© Prof Suvendi Rimer, UJ


Help on command line

 However, if you know what you want to do, but not the function name, use the
command “lookfor xxx”, where xxx is the text you are looking for.
 This looks through the descriptions of all the functions, and returns a list of
possible functions to use.
 At this stage, you can use the help function to get details on using a specific
function.

© Prof Suvendi Rimer, UJ


Octave Desktop
 When Octave executes, it displays the desktop that contains tools for managing
files, variables and applications associated with Octave.
 The desktop consists of a menu bar, a toolbar, and a window space where
several different windows can be displayed.
 Possible windows that can be displayed on the desktop are the following:
 Launch Pad; Command History; Command; Current Directory Browser;
Workspace Browser and Array Editor; Edit/Debug; Figure; and Help Browser.
 If no answer variable defined, Octave stores the last answer in the variable ans.
 If a statement is too long to type on single line, it can be continued on successive
lines by typing an ellipsis (…) at end of line and continuing on the second line.
 Once a command is typed and Enter pressed, Octave responds with an answer.
To override this, a semicolon (;) can be used at the end of the line.
 To clear the Command Window (not the results!), the “clc” (for clear console)
command can be used.
 To abort a command or a script, type in control-c (^C) in the Command Window.
 You can use the up and down arrow keys to recall previous executed command
lines. This is a great time saving tool in that you can change the parameters of
an equation several times by using the up arrow and the editing the line.
 ©You can Rimer,
Prof Suvendi alsoUJuse the Command History Window.
Octave Workspace
 A statement like “z = 10” creates a variable named z, stores the value 10 in it,
and saves it in a part of computer memory known as the workspace.
 The workspace is the collection of all the variables and arrays that can be used
by Octave when a particular command, m-file, or function is executed.
 All commands executed in Command Window share a common workspace, so
they can all share variables. Octave functions differ from script files - each
function has its own separate workspace.
 “whos” command - list of all the variables and arrays in the current workspace.
 View the content of the workspace in the Workspace Browser Window.
 Contents of any variable or array - type name in the Command Window.
 A specific variable may be deleted from the workspace with the “clear” command
in the form “clear var1 var2 …”
 Contents of workspace persist between the executions of separate commands.
 possible for results of one problem to affect the next one
 To avoid - issue clear command at start of each new independent calculation.
 The contents of the Workspace can be saved to disk using the save command.
 The 'load' command can read data stored in Octave's text and binary formats,
and Octave's binary format.
© Prof Suvendi Rimer, UJ
Workspace browser and Edit/Debug window
 Use the Workspace browser to perform operations on the Octave workspace.
 In Workspace Browser - activate the Variable Editor for a visual representation of
one or two-dimensional numeric arrays, strings and cell arrays of strings.
 To open it, select the variable you want to open.
 Right click and select Open Selection.
 Alternatively, from the Command Window prompt, use openvar with the name of
the variable you want to open, e.g. openvar(‘m’).
 Edit/Debug window: is a programming text editor, with the Octave languages
features highlighted in different colours.
 Instead of typing commands directly in Command Window, a series of commands
can be placed into a file and the file executed by typing its name in the Command
Window. Scripts and functions known as M-files and have file extensions of “.m”.
 After m-file saved - executed by typing its name in the Command Window.
 Create new M-file: File -> New -> Function” from the menu or Ctrl-Shift-N.
 A separate Edit/Debug window is created automatically when you create a new
M-file or open an existing one.

© Prof Suvendi Rimer, UJ


Comments
 Comments in an M-file is indicated by the symbol “%” appearing first in a line
 The comments that appear in the first few lines of m-files (functions) will be
displayed with the help command.
 Single Line Comments: The comment starts with either the sharp sign
character, ‘#’, or the percent symbol ‘%’ and continues to the end of the line. Any
text following the sharp sign or percent symbol is ignored by the Octave
interpreter and not executed. The following example shows whole-line and
partial-line comments.
function countdown
# Count down for main rocket engines
disp (3);
disp (2);
disp (1);
disp ("Blast Off!"); # Rocket leaves pad
endfunction

© Prof Suvendi Rimer, UJ


Comments
 Block Comments: Entire blocks of code can be commented by enclosing the
code between matching ‘#{’ and ‘#}’ or ‘%{’ and ‘%}’ markers. For example,
function quick_countdown
# Count down for main rocket engines
disp (3);
#{
disp (2);
disp (1);
#}
disp ("Blast Off!"); # Rocket leaves pad
endfunction

 outputs '3' and "Blast Off" as lines "disp (2);" and "disp (1);" won’t be executed.
 The block comment markers must appear alone as the only characters on a line
(excepting whitespace) in order to be parsed correctly.

© Prof Suvendi Rimer, UJ


Figure Window
 A Figure Window is used to display Octave
graphics.
 A figure can be a 2- or 3-dimensional plot of
data, an image, or a GUI.
 Simple script file that calculates and plots the
function sin x:
% sin_x.m: This M-file calculates and
% plots the function sin(x)
% 0 <= x <=6.
x = 0:0.1:6;
y = sin(x);
plot(x,y);
 When this script is executed in the Command
Window, a Figure Window is opened and the
plot is shown:
 The current Figure Window can be cleared at
any time using the “clf” command in the
Command Window.
© Prof Suvendi Rimer, UJ
Elementary Calculations: Summary
 Octave knows about arithmetic operations (+,-,*,/), exponentiation (^), natural
logarithms/exponents (log, exp), and the trigonometric functions (sin, cos, …).
 Octave calculations work on real or imaginary numbers (i,j).
 Some mathematical constants such as the base of the natural logarithm (e) and
the ratio of a circle’s circumference to its diameter (pi) are pre-defined.
 Creating a Matrix
 Vectors and matrices are the basic building blocks for numerical analysis. To
create a new matrix and store it in a variable so that you can refer to it later,
type the command
 >> A = [ 1, 1, 2; 3, 5, 8; 13, 21, 34 ]

© Prof Suvendi Rimer, UJ


Elementary Calculations: Summary
 Octave will respond by printing the matrix in neatly aligned columns. Octave uses
a comma or space to separate entries in a row, and a semicolon or carriage
return to separate one row from the next.
 Ending a command with a semicolon tells Octave not to print the result of the
command. For example: >> B = rand (3, 2); will create a 3 row, 2 column matrix
with each element set to a random value between zero and one.
 To display the value of a variable, simply type the name of the variable at the
prompt. For example, to display the value stored in the matrix B, type the
command: >> B

© Prof Suvendi Rimer, UJ

You might also like