Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
4Activity
0 of .
Results for:
No results containing your search query
P. 1
Matlab Tutorial

Matlab Tutorial

Ratings: (0)|Views: 14|Likes:
Published by p997080

More info:

Published by: p997080 on Oct 15, 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

07/17/2013

pdf

text

original

 
MatLab Tutorial
 Draft 
Anthony S. MaidaOctober 8, 2001; revised September 20, 2004; March 23, 2006
Contents
1 Introduction 2
1.1 Is MatLab appropriate for your problem? . . . . . . . . . . . . . . . . . . 21.2 Interpreted language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21.2.1 Command-line shell . . . . . . . . . . . . . . . . . . . . . . . . . 21.2.2 Loading scripts from les . . . . . . . . . . . . . . . . . . . . . . 3
2 Vectorizing code 3
2.1 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.1.1 Statement terminators and output suppression . . . . . . . . . . . . 42.1.2 Some matrix operators . . . . . . . . . . . . . . . . . . . . . . . . 42.1.3 Flexible matrix access . . . . . . . . . . . . . . . . . . . . . . . . 42.1.4 Loading data via a script . . . . . . . . . . . . . . . . . . . . . . . 52.2 Vector operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62.2.1 Examining the workspace . . . . . . . . . . . . . . . . . . . . . . 62.3 Applying functions to matrix elements . . . . . . . . . . . . . . . . . . . . 62.3.1 Vectorizing a feedforward network for one epoch . . . . . . . . . . 72.4 Dening functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3 Plotting and visualization 9
3.1 Simple plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93.1.1 Application to neural networks . . . . . . . . . . . . . . . . . . . . 93.2 3D Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103.3 Surface plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113.4 Other plotting commands . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Appendix 11
1
 
A Appendix: Matrix notation 12
A.1 Matrix multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12A.2 Denition of transpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1 Introduction
M
ATLAB
stands for
matrix laboratory
and the name is a trademark of The MathWorksIncorporated. The purpose of this document is to give you enough background so that youeffectively use the M
ATLAB
built-in help system to solve your programming problems.
1.1 Is MatLab appropriate for your problem?
M
ATLAB
is effective in small applications where the appropriate data representations arevectors and matrices. Vectors and matrices are the appropriate data structure for nearly allnon-trivial problems in scientific computing. M
ATLAB
is especially useful for interactivelyplotting functions and visualizing scientific data. M
ATLAB
is commercial software.
1
1.2 Interpreted language
M
ATLAB
is an interpreted language, hence, its programs are called scripts. To achieveefficiency, M
ATLAB
allows the user to vectorize his/her code by using matrices and theirassociated operators. The interpretation overhead for a vectorized instruction is then smallcompared to the amount of computation that the instruction does.
1.2.1 Command-line shell
The user has a choice of using M
ATLAB
interactively via a command-line shell (read-eval-print loop) or by loading prewritten scripts from files. The language is designed (e.g.,variables need not be declared) so that commands evaluated in the read-eval-print loop willalso work when loaded from a file. This facilitates rapid prototyping and debugging becauseyou can test your command before putting it into a file. A simple command-line interactionsession is shown below.
>> 2 + 2ans = 4
The command-line prompt is
>>
. The system reads the expression
2 + 2
, evaluates it,stores it in a default variable
ans
, and prints the value of this variable. The variable
ans
always has the result of the most recent computation that has not already been assigned avalue. For instance, if we continue the session as shown below, the value of 
ans
doesn’tchange.
1
If you cannot afford MatLab, then GNU O
CTAVE
(
www.octave.org
) is an alternative for interactivematrix computations and
GNUPLOT
(
www.gnuplot.org
) is an alternative for plotting and visualizingdata.
2
 
>> x = 2 + 3x = 5>> ansans = 4
At this point the workspace has two variables
x
and
ans
. The development environmenthas workspace editor which allows you to examine the variables in the workspace and howmuch space they consume. You can clear the workspace to its original state by typing thecommand
clear
.
1.2.2 Loading scripts from files
You can use the M
ATLAB
built-in editor to write scripts, save them to files, and then havethem loaded and evaluated as if you typed the commands directly into the command line.Also, while in the command line shell, you can type the file name containing a M
ATLAB
script to achieve the same effect. Text files containing M
ATLAB
code are called m-files andhave the file extension “
.m
”.As you develop a script, you will follow a cycle of editing and loading. When youreload your script, the workspace from the previous cycle will still be in effect unless youclear it. You probably want to clear it to avoid subtle reinitialization errors. The best wayto do this is to put the
clear
command as the first line of the script, so that the script runsin a virgin workspace.
2 Vectorizing code
Interpreted M
ATLAB
scripts achieve efficiency in speed of execution by using large instruc-tions to reduce decoding overhead and by using space to decrease time. For a given problemsize, M
ATLAB
scipts use a lot of memory. Thus M
ATLAB
trades memory to increase speed.
2.1 Matrices
The primary data structure in M
ATLAB
is a matrix. The appendix in this tutorial has moreinformation about matrices. You can create and initialize a matrix by typing the matrixvalues in an assignment statement. The example below creates a
3
×
2
matrix of double-precision floating-point values and then sets the variable
w
to reference the matrix.
2
Noticethat the variable
w
was not previously declared. The rows of the matrix are terminated withsemicolons. All the commands below create the same matrix.
>> w = [1 2 3; 4 5 6]>> w = [1 2 3; 4 5 6;]>> w = [1, 2, 3; 4, 5, 6]>> w = [1, 2, 3; 4, 5, 6;]
2
Numbers in M
ATLAB
are always double-precision floating-point.
3

Activity (4)

You've already reviewed this. Edit your review.
1 hundred reads
1 hundred reads
Rajesh Verma liked this
isaacsamuel2003 liked this

You're Reading a Free Preview

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