Tuesday, October 18, 2011

OCTAVE

OCTAVE TUTORIAL




REVISED: Sunday, March 3, 2013





You will learn some of the fundamental Octave concepts.

I.  INTRODUCTION TO OCTAVE

A.  DOWNLOADING OCTAVE

Octave is a free download. Select the appropriate system at Octave-Forge and they will let you download an .exe setup wizard file that will prompt you through the Octave installation on your computer. When using the wizard select all the defaults unless you have expertise to do otherwise.

Click this link to go to the Octave-Forge download page.


B.  OCTAVE PROMPT

The Octave prompt will start off as:

octave-3.2.4.exe:1>

To exit Octave, type quit, or exit at the Octave prompt then press Enter.

If you want a shorter prompt type
PS1('>> ');
>>

>> Will become your new Octave prompt.

This prompt will only stay active while you are signed on.

When you quit, or exit and sign back on the Octave prompt will start off again as:

octave-3.2.4.exe:1>

C.  OCTAVE SCRIPT FILES


Octave script files are normal text files that must have a .m extension to the file name. The script files are also referred to as M-files. Script files or M-files, contain the basic commands for an Octave program. The content of the script file must not begin with the Octave keyword function.

To run script "test.m" you would type "test" into Octave and then press Enter.

You can create a script in any text editor; e.g., Notepad.


You can start-up a text editor in a new window from within Octave by typing edit and then press Enter.

Type as if you were typing at the Octave prompt.

% Script that creates a an identity matrix.
A=eye(5);
A=A*2;
A

Then File Save As test.m to the current directory.

Next, at the Octave prompt type test and press return.

Octave will display:

2  0  0  0  0
0  2  0  0  0
0  0  2  0  0
0  0  0  2  0
0  0  0  0  2


If you want to edit an existing script include the name of the script.  If you have a script called "test.m" typing edit test would open the text editor and load up that file for editing.

Use % to place notes to yourself, in the form of comments, in each file.  Any text on a line after a % is ignored by Octave.  Use the first % comment to describe the purpose and contents of the file.

When you type help followed by a file name, Octave will read back to you the information following the first % comment.  Therefore, always reserve the first % comment to describe the purpose and contents of the file.

Make sure you save the file each time you edit the file.

The what command will give you a list of all your script and data files in the current directory.

D.  OCTAVE MATH OVERVIEW

4+4          % addition
ans = 8

4-3          % subtraction
ans 1

4*4          % multiplication
ans 16

4/2          % division
ans 2

2^3          % exponent
ans 8

E.  OCTAVE LOGIC OVERVIEW

Octave uses "ans = 0" for False, and "ans = 1" for True.

When you want to type a comment; start the comment with %.

3 == 3     % == is the logical EQUAL
ans = 1

5 ~= 8    % ~=  is NOT EQUAL
ans = 1

1 && 0  % && is the logical AND; e.g., True && False
ans = 0

1 || 0      % || is the logical OR; e.g.,  True || False
ans = 1

F.  OCTAVE OUTPUT DISPLAY OVERVIEW

>> x = 9  % Assigns 9 to the variable x.
x = 9
>>

>> x = 9;  % A semicolon suppresses Octave output.

>> z = 'Hello world.'  % Assigns a character string to z.
z = Hello world.
>>

To find out what value is assigned to a variable type the variable and press enter.

>> x                    % To ask Octave for the value assigned to x.
x = 9
>>

>> y = (x >= 1)  % To assign a logical value to a variable.
y = 1                  % Now y has been assigned True.
>>

>> h = pi;            %  To assign the value of pi to h.
>> h                     % To ask Octave for the value assigned to h.
h = 3.1416
>>

>> disp(h);          % To display the value assigned to h
3.1416

>> disp(sprintf('1 decimal: %0.1f', h))     % Display format commands can be used to format the output.
1 decimal: 3.1
>>

>> disp(sprintf('8 decimals:%0.8f', h))  
8 decimals:3.14159265
>>

>> format long
>> h
h = 3.14159265358979

>> format short
>> h
h = 3.1416

G.  OCTAVE MATRIX

A matrix is just a two dimensional grid of numbers.

>> X = [ 1 2; 3 4; 5 6]          % The ; means go to next row.
X =
        1        2
        3        4
        5        6

>> X = [ 1 2;                       % Another way to get 3 by 2 matrix.
> 3 4;
> 5 6]
A =
        1        2
        3        4
        5        6

H.  OCTAVE VECTOR

A vector is a matrix with only one row or only one column.

A vector is just a list of numbers.

A list of numbers separated by spaces or commas, inside square brackets, defines a row vector.

A list of numbers separated by semicolons or carriage returns defines a column vector.

>> v = [  1   2   3  ]
v =
     1     2     3

>> v = [  1;   2;   3  ]

v =
     1
     2
     3

I.  BASIC OCTAVE FUNCTIONS

size function tells you the dimensions of a matrix.

eye creates an identity matrix.

inv creates an inverse of a matrix.

zeros creates a matrix of zeros.

ones creates a matrix of ones.

rand creates a matrix filled with random numbers.

You have learned some of the fundamental Octave concepts.

Elcric Otto Circle






-->




-->




-->









How to Link to My Home Page

It will appear on your website as:

"Link to: ELCRIC OTTO CIRCLE's Home Page"