Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
P. 1
Octave Tutorial

# Octave Tutorial

Ratings: (0)|Views: 194|Likes:
Published by Thiago VedoVatto

### More info:

Published by: Thiago VedoVatto on Jun 09, 2010
Copyright:Attribution Non-commercial

### Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

10/27/2011

pdf

text

original

Introduction to Octave
Dr. P.J.G. LongDepartment of EngineeringUniversity of CambridgeBased on the Tutorial Guide toMatlab written by Dr. Paul SmithSeptember 2005
This document provides an introduction to computing using Octave. It will teachyou how to use Octave to perform calculations, plot graphs, and write simple programs.The close compatibility of the open-source Octave
1
package with MATLAB
2
, whichis heavily used in industry and academia, gives the user the opportunity to learn thesyntax and power of both packages where funding and licence restrictions prevent theuse of commercial packages.To maintain the ideal of learning both Octave and Matlab from this tutorial, thediﬀerences between Octave and Matlab have been highlighted and details of any mod-iﬁcations etc. required to run a function/program with Matlab described in footnotes.In a number of cases additional functions have had to be written or startup optionsset, these are included as default on the MDP
3
distribution and documented in anappendix.
Draft version 0.1.0, please email any errors to mdp-support@eng.cam.ac.uk
1
www.octave.org
2
MATLAB
R
The Mathworks, www.mathworks.com
3
Multidisciplinary Design Project
1

Contents
1 Introduction 4
1.1 What is
Octave
? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.2 What
Octave
is not . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.3 Who uses
Octave
? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.4 Why not use a ‘normal’ highlevel language, e.g. C++ . . . . . . . . . . . . 4
2 Simple calculations 5
2.1 Starting
Octave
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.2
Octave
as a calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.3 Built-in functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3 The Octave environment 6
3.1 Named variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83.2 Numbers and formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93.3 Number representation and accuracy . . . . . . . . . . . . . . . . . . . . . . 103.4 Loading and saving data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103.5 Repeating previous commands . . . . . . . . . . . . . . . . . . . . . . . . . 113.6 Getting help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113.7 Cancelling a command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133.8 Semicolons and hiding answers . . . . . . . . . . . . . . . . . . . . . . . . . 13
4 Arrays and vectors 13
4.1 Building vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134.2 The colon notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144.3 Displaying large vectors and matrices . . . . . . . . . . . . . . . . . . . . . . 144.4 Vector creation functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154.5 Extracting elements from a vector . . . . . . . . . . . . . . . . . . . . . . . 154.6 Vector maths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5 Plotting graphs 17
5.1 Improving the presentation . . . . . . . . . . . . . . . . . . . . . . . . . . . 185.2 Multiple graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195.3 Multiple gures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205.4 Manual scaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215.5 Saving and printing gures . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
6 Octave programming I: Script les 23
6.1 Creating and editing a script . . . . . . . . . . . . . . . . . . . . . . . . . . 236.2 Running and debugging scripts . . . . . . . . . . . . . . . . . . . . . . . . . 246.3 Remembering previous scripts . . . . . . . . . . . . . . . . . . . . . . . . . . 24
7 Control statements 25
7.1
if
...
else
selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257.2
switch
selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267.3
for
loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277.4
while
loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282

7.5 Accuracy and precision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
8 Octave programming II: Functions 29
8.1 Example 1: Sine in degrees . . . . . . . . . . . . . . . . . . . . . . . . . . . 308.2 Creating and using functions . . . . . . . . . . . . . . . . . . . . . . . . . . 308.3 Example 2: Unit step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
9 Matrices and vectors 33
9.1 Matrix multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349.2 The transpose operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359.3 Matrix creation functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359.4 Building composite matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . 379.5 Matrices as tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379.6 Extracting bits of matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
10 Basic matrix functions 3811 Solving
A
x
=
b 40
11.1 Solution when
A
is invertible . . . . . . . . . . . . . . . . . . . . . . . . . . . 4011.2 Gaussian elimination and LU factorisation . . . . . . . . . . . . . . . . . . . 4111.3 Matrix division and the slash operator . . . . . . . . . . . . . . . . . . . . . 4111.4 Singular matrices and
rank
. . . . . . . . . . . . . . . . . . . . . . . . . . . 4111.5 Ill-conditioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4311.6 Over-determined systems: Least squares . . . . . . . . . . . . . . . . . . . . 4411.7 Example: Triangulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
12 More graphs 45
12.1 Putting several graphs in one window . . . . . . . . . . . . . . . . . . . . . 4512.2 3D plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4612.3 Changing the viewpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4612.4 Plotting surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4712.5 Images and Movies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
13 Eigenvectors and the Singular Value Decomposition 49
13.1 The
eig
function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4913.2 The Singular Value Decomposition . . . . . . . . . . . . . . . . . . . . . . . 5013.3 Approximating matrices: Changing rank . . . . . . . . . . . . . . . . . . . . 5113.4 The
svd
function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5113.5 Economy SVD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
14 Complex numbers 53
14.1 Plotting complex numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5414.2 Finding roots of polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . 54
15 Appendix - Setup conditions 5516 Further reading 5517 Acknowledgements 56
3

## Activity (6)

You've already reviewed this. Edit your review.
1 thousand reads
1 hundred reads
cmtinv liked this
sijon liked this
Dr. Varga Istvan liked this
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->