Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Octave Quick Reference

Octave Quick Reference

Ratings: (0)|Views: 20|Likes:
Published by oscarsat

More info:

Published by: oscarsat on May 11, 2012
Copyright:Attribution Non-commercial


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





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
and hit return, you get
. If 
Matlab/Octave Tutorialhttp://volga.eng.yale.edu/sohrab/matlab_tutorial.html2 of 1103/11/2008 10:56 PM
 you type
you get
The usual mathematical operations of interest are
(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.
 You will have noticed that matlab spits out an
before giving you the answer inthe two examples above. This means it is printing the value of the variable
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
and see its value).We can create and assign variables ourselves. For example, typing
will resultin
. From now on, the variable
is defined and currently has value 10. If you type
by itself, matlab will print its value.To see a list of variables currently defined, use the
command. The
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
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]
We can now take a dot product of v and z by typing
. We can do matrix-vectormultiplication with
w*[1 1]'
[1 1]*w
(they are different!)
Complex numbers and complex conjugation
If you do not define a variable named
, 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
, 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 
is a real matrix, then
is its transpose.For complex matrices, the transpose is given by the
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
quietly. A more useful example would be
which sets
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 on
. When paging is on, each full page of textshows a
and waits for you to press a key: press space for another page,return for another line, and
to quit the output. You can turn paging off with
 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
command and change directories with the
. 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
containing the four lines
command clears out any existing variables in memory. When you type
at the matlab prompt, nothing seems to happen, but you can check that the variable
now contains the value 5 and that
are defined (with the

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->