Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
0 of .
Results for:
P. 1
Octave Quick Reference

# Octave Quick Reference

Ratings: (0)|Views: 18|Likes:

### Availability:

See more
See less

03/20/2014

pdf

text

original

Matlab/Octave Tutorialhttp://volga.eng.yale.edu/sohrab/matlab_tutorial.html1 of 1103/11/2008 10:56 PM
The basicsOperators Variables Vectors and matricesComplex numbers and complex conjugationTransposition and Hermitian conjugationReducing and paging matlab screen output Aborting a commandCreating a program file: .m filesGetting helpSome useful mathematical operationsCreating a vector of equally spaced numbersSelecting parts of vectors and matrices: the colon operatorCreating identity and zero matricesCreating a vector or matrix full of onesCreating a diagonal matrixCreating off diagonal/triangular matricesInverting a matrixDiagonalizing a matrixSome useful matlab/octave programmingProgram or .m filesLoops Visualizing and plottingMaking a two-dimensional plotGetting some grid linesPlotting the rows/columns of a matrixSuperimposing multiple plotsClearing the plotting screenChoosing symbols and colors for plotsLogarithmic plotsPutting titles and labels on the plotPrintingExamplesPlotting a function of xCreating a tridiagonal matrixDiagonalizing a matrix and testing orthogonality of eigenvectorsCalculating an outer productPlotting a vector on a log plotWhat follows below is a short and very basic practical introduction to matlab/octave. You can find a great deal more information about matlab or octave on the web.Octave is a free/openware (GNU) version of matlab: it is not completely identical withmatlab and its graphing facilities are not as fancy, but it works well and is free (bothprice wise and following the free/openware philosophy).
This document was created by Sohrab Ismail-Beigi in November, 2003, primarily for use in the ENAS 856a/PHYS 650 course.This web page was last modified on 07/19/2005 14:37:55.
The basics
Matlab is a program that is geared for performing numerical calculations in an easyand straightforward way. It is text-based so you type in commands, hit return, andthis creates output or plots. For example, if you type
2+2
and hit return, you get
4
. If

Matlab/Octave Tutorialhttp://volga.eng.yale.edu/sohrab/matlab_tutorial.html2 of 1103/11/2008 10:56 PM
you type
cos(pi)
you get
-1
.
Operators
The usual mathematical operations of interest are
+
,
-
,
*
(multiply),
/
(divide), and
^
(exponentiation). Whether these act on numbers or vectors or matrices depends onthe case (see below). So, just to be clear, 2^3=8, (3+4)/14=0.5, etc.
Variables
You will have noticed that matlab spits out an
ans=
before giving you the answer inthe two examples above. This means it is printing the value of the variable
ans
whichholds the answer to the calculation. If you type the name of a variable by itself and hitreturn, matlab prints its value (e.g. type
ans
and see its value).We can create and assign variables ourselves. For example, typing
x=2*3+4
will resultin
x=10
. From now on, the variable
x
is defined and currently has value 10. If you type
x
by itself, matlab will print its value.To see a list of variables currently defined, use the
who
command. The
whos
command is a longer version of the same thing showing the type of variable and itssize.
Vectors and matrices
We are not limited to simple numbers. We can define vectors or matrices easily. Forexample,
v=[1 2 3]
sets the variable
v
to be a row vector of size 3 whereas
w=[1 2 ;3 4]
creates a 2x2 matrix.
z=[5 6 7]'
creates a 3D column-vector (the prime
'
denotes Hermitian transpose). The creation of w and z could also have beenaccomplished with the longer but more clear:
w=[1 23 4]
and
z=[567]
We can now take a dot product of v and z by typing
v*z
. We can do matrix-vectormultiplication with
w*[1 1]'
or
[1 1]*w
(they are different!)
Complex numbers and complex conjugation
If you do not define a variable named
i
, then it is by default the unit imaginarynumber, sqrt(-1). Creating complex numbers is easy:
z = 2+3*i
does the trick. Complex conjugation can be done in two ways: either by using thefunction
conj
, or by using the Hermitian conjugation operator
'
(single quote):
z = 2+3*i;conj(z)

Matlab/Octave Tutorialhttp://volga.eng.yale.edu/sohrab/matlab_tutorial.html3 of 1103/11/2008 10:56 PM
ans =2.000 - 3.000iz'ans =2.000 - 3.000i
Transposition and Hermitian conjugation
For real matrices these are the same and most easily accomplished with the the
'
operator (single quote): if
M
is a real matrix, then
M'
is its transpose.For complex matrices, the transpose is given by the
transpose
function; Hermitianconjugation by the
'
operator, as above. Thus we have that
M' =transpose(conj(M))
.
Reducing and paging matlab screen output
If your variables are large, you probably do not want matlab to print the values to thescreen. You can silence matlab by putting a semicolon
;
at the end of a command: e.g.
v=[1 2 3];
will set the variable
v
quietly. A more useful example would be
a=linspace(0,1,100);
which sets
a
to be a linearly spaced vector of 100 numbersfrom 0 to 1 (inclusive).If you want to see the output but it has scrolled by too fast, then you need to turn onpaging with the
more
command:
more on
. When paging is on, each full page of textshows a
--more--
and waits for you to press a key: press space for another page,return for another line, and
q
to quit the output. You can turn paging off with
moreoff
.
Aborting a command
You can always abort any matlab command by pressing control-c (the control or``Ctrl'' key and the ``c'' key at the same time).
Creating a program file: .m files
When you type a name that is not a built-in command or variable, matlab looks at thecurrent directory for any program files that have the name you typed plus a ``.m''added at the end and will execute them if found. You can find the current directory byusing the
pwd
command and change directories with the
cd
. A matlab .m file is nothing more that a list of commands put into a file, one line at atime. When you type the file's name, the commands get executed. As an example, letus say we have created a file called
myfirst.m
containing the four lines
cleara=2;b=3;c=a+b;
The
clear
command clears out any existing variables in memory. When you type
myfirst
at the matlab prompt, nothing seems to happen, but you can check that the variable
c
now contains the value 5 and that
a
and
b
are defined (with the
who
command).