Professional Documents
Culture Documents
Rahal
Introduction to MATLAB
Dr. Salah M. Rahal
22/8/1435H
1
What is MATLAB ?
MATLAB stands for MATrix LABoratory and the software is built up around
vectors and matrices that has evolved over a period of years.
It is a high-level programming language. It is an interpreted language - the
source code is not compiled but interpreted on the fly.
It is one of the easiest programming languages for writing mathematical
programs.
MATLAB is also a great tool for solving algebraic and differential equations
and for numerical integration.
MATLAB has powerful graphic tools and can produce nice pictures in both 2D
and 3D.
MATLAB also has some tool boxes useful for signal processing, image
processing, optimization, etc.
It is developed by The Mathworks, Inc.
(http://www.mathworks.com), where comprehensive documentation is
available.
The language, tools, and built-in math functions enable you to explore multiple
approaches and to reach a solution faster than with spreadsheets or traditional
programming languages, such as C/C++ or Java.
Key Features
- High-level language for numerical computation, visualization, and
application development.
- Interactive environment for iterative exploration, design, and problem
solving.
- Mathematical functions for linear algebra, statistics, Fourier analysis,
filtering, optimization, numerical integration, and solving ordinary
differential equations.
- Built-in graphics for visualizing data and tools for creating custom plots.
- Development tools for improving code quality and maintainability and
maximizing performance.
- Tools for building applications with custom graphical interfaces.
- Functions for integrating MATLAB based algorithms with external
applications and languages such as C/C++ , Java, .NET, and Microsoft
Excel.
Typical uses of MATLAB include: Math and computation, Algorithm
development, Modeling, simulation, and prototyping, Data analysis,
exploration, and visualization, Scientific and engineering graphics, Application
development, including GUI building.
Toolboxes
- a family of application-specific solutions. They are
comprehensive collections of MATLAB functions (M-files*) that extend the
2
First printing
Second printing
Third printing
Fourth printing
Online only
Online only
Fifth printing
S-th printing
Online only
Online only
S-th printing
Online only
Online only
E-th printing
9-th printing
10-th printing
11-th printing
12 -th printing
13-th printing
14-th printing
15-th printing
16-th printing
Online only
17-th printing
18-th printing
19-th printing
20-th printing
21-th printing
22-th printing
*R- Release.
MATLAB 5
MATLAB 5.1
MATLAB 5.3
MATLAB 6 (R* 12)
MATLAB6.1 (R 12.1)
MATLAB 6.5 (R 13)
MATLAB 6.5
MATLAB 7.0 (R 14)
MATLAB 7.0.1 (R 14SP1)
MATLAB 7.0.4 (R 14SP2)
MATLAB 7.0.4
MATLAB 7.1 (R 14SP3)
MATLAB 7.2 (R 2006a)
MATLAB 7.3 (R 2006b)
MATLAB 7.4 (Release 2007a)
MATLAB 7.5 (Release 2007b)
MATLAB 7.6 (Release 2008a)
MATLAB 7.7 (Release 2008b)
MATLAB 7.8 (Release 2009a)
MATLAB 7.9 (Release 2009b)
MATLAB 7.10 (Release 2010a)
MATLAB 7.11 (Release 2010b)
MATLAB 7.12 (R2011a)
MATLAB 7.13 (R2011b)
MATLAB 7.14 (R2012a)
MATLAB 8.0 (R2012b)
MATLAB 8.1 (R2013a)
MATLAB 8.2 (R2013b)
MATLAB 8.3 (R2014a)
System Requirements
Operating System
Windows 8.1
Windows 8
Windows 7 Service
Pack 1
Windows Vista
Service Pack 2
Windows XP Service
Pack 3
Windows XP x64
Edition Service
Pack 2
Processors
Any Intel or
AMD x86
processor
Supporting
SSE2*
instruction set
RAM
Disk Space
1024 MB
1 GB for
(At least
MATLAB only,
2048 MB
recommended) 34 GB for a
typical
installation
* Single-Precision
Floating-Point
Instructions (SSE)
MATLAB Documentation
MATLAB provides extensive documentation, in both Printed &Online Format.
Getting Started - MathWorks
Development Environment
Starting MATLAB
On Windows platforms, start MATLAB by double-clicking the MATLAB
shortcut icon on your Windows desktop.
Quitting MATLAB
To end your MATLAB session, select Exit MATLAB from the File menu in the
desktop, or type quit in the Command Window.
MATLAB Desktop
When you start MATLAB, the MATLAB desktop appears, containing tools
(graphical user interfaces) for managing files, variables, and applications
associated with MATLAB.
You can customize the arrangement of tools and documents to suit your needs.
You can specify certain characteristics for the desktop tools by selecting
Preferences from the File menu. For example, you can specify the font
characteristics for Command Window text.
Desktop Tools
- Command Window: Use the Command Window to enter variables and
run functions and M-files.
5
- Command History: Lines you enter in the Command Window are logged
in the Command History window.
In the Command History, you can view previously used functions, and
copy and execute selected lines.
To save the input and output from a MATLAB session to a file, use the
diary function.
- Workspace: It displays the Workspace browser, a graphical user interface
that allows you to view and manage the contents of the MATLAB
workspace. It provides a graphical representation of the whos display,
and allows you to perform the equivalent of the clear, load, open, and
save functions.
Matlab Desktop is shown in the flg. Fig.
On-line Help
MATLAB provides on-line help for all built-in functions and commands. To
browse a list of help topics type
>> help
To get help on a specific topic, for example the cosine function, type
>> help cos
Using help is one way to learn about the variety of built-in functions. The help
command is most useful when you know the name of the function, but are
6
unsure how to use it. The MATLAB manuals provide cross-referenced and
indexed descriptions of all aspects of using MATLAB
To open the Help browser, click the help button in the toolbar, or type
helpbrowser in the Command Window: See flg. fig.
The Help browser consists of two panes, the Help Navigator pane, which you
use to find information, and the display pane, where you view the information.
Using MATLAB as a calculator
Simply type a mathematical expression into the command window.
The command window prompt is >>.
To enter an expression, type it after the prompt (correct any mistakes by
backspacing) and press return.
MATLAB prints the result back to the command window.
For example:
>> a=6
a=
6
>> b=5
b=
5
>> c=a*b
c=
30
or,
7
>> 6*5
ans =
30
Note: When you do not specify an output variable, MATLAB uses the variable
ans, short for answer, to store the results of a calculation.
>>pi
MATLAB responds with
ans =
3.1416
pi is a built-in MATLAB variable.
Manipulating
Matrices
Manipulating Matrices
If you start by entering matrix as a list of its elements. You only have to follow
a few basic conventions:
- Separate the elements of a row with blanks or commas.
- Use a semicolon, ; , to indicate the end of each row.
- Surround the entire list of elements with square brackets, [ ].
Example:
To enter the flg. Matrix:
1 2 3
4 5 6
7 8 9
Simply type in the Command Window:
A = [1 2 3; 4 5 6; 7 8 9]
MATLAB displays the matrix you just entered:
A=
1 2 3
4 5 6
7 8 9
Once you have entered the matrix, it is automatically remembered in the
MATLAB workspace. You can refer to it simply as A.
Sum, Transpose, and diag
Now that you have A in the workspace, take a look at what makes it so
interesting: For example, if you want to do the sum of each column, only you
have to enter
sum (A)
MATLAB replies with
ans =
12 15 18
The transpose is given by A; when you write:
A
MATLAB replies with
ans =
1 4 7
2 5 8
3 6 9
When you do not specify an output variable, MATLAB uses the variable ans, short for
answer, to store the results of a calculation.
10
0:pi/4:pi
is
0 0.7854 1.5708 2.3562 3.1416
Expressions
Like most other programming languages, MATLAB provides mathematical
expressions, but unlike most programming languages, these expressions
involve entire matrices. The building blocks of expressions are:
- Variables.
- Numbers.
- Operators.
- Functions.
Overview of MATLAB Variables
- MATLAB variables are created when they appear on the left of an equal
sign. The generic statement >> variable = expression
creates the ''variable'' and assigns to it the value of the expression on the
right hand side.
- You do not need to define or declare a variable before it is used.
- Matrices do not need to explicitly dimensioned, and MATLAB allows
you to increase the size of a matrix as you work.
Variable name
- Begins with a LETTER, e.g., A2z.
- Can be a mix of letters, digits, and underscores_.
- Is case sensitive, e.g., NAME, Name, name are 3 distinct variables. Also
X and x 2 distinct variables.
- Can be of any length.
Although the length can be of any length, MATLAB uses only the first N
characters of the name (where N is the number returned by the function
namelengthmax), and ignores the rest. Hence, it is important to make each
variable name unique in the first N characters to enable MATLAB to
distinguish variables.
All numerical variables in MATLAB are matrices (a mathematical data type
corresponding to a two-dimensional array of numbers).
MATLAB is very careful to follow the rigorous rules of linear algebra, and it
only allows legal operations between matrices, vectors and scalars.
But what about the statement that all numerical variables in MATLAB are
matrices?
MATLAB keeps track of the dimension of each variable. Thus, the statement
>> x = 2
12
x=5
>> z = y + x^0.25
>> y = pi/4
y = 0.7854
z = 2.2807
Note that z exists only as a numerical value, not as the formula with which it
was created.
Use the who command to list the currently active variables. For the preceding
session this results in
>> who
Your variables are:
ans x y z
To view (print) the matrix assigned to any variable, simply enter the variable
name at the prompt.
>> z
z=
2.2807
13
Special Variables
ans variable which is automatically created whenever a mathematical
expression is not assigned to another variable.
The built-in eps variable* & other variables are given in the flg. table.
*It is used as a tolerance for internal calculations. Initially eps is equal to machine epsilon
You can reset the value of eps, but this is not recommended unless you are very sure you
know what you are doing.
In general you should not try to (re)assign the values of any built-in variables listed in the
table below.
The realmax, realmin, Inf and NaN built-in variables are used to handle
floating point exceptions.
All numerical variables in MATLAB are stored as 32 bit floating point
numbers. The Inf and NaN values appear if a floating point exception occurred
during the calculations.
Strings
MATLAB variables may also be strings, which are matrices of individual
characters. For example, the following statements assign a numerical value to x
and a string value to y.
>> x = 5.2
>> y = 'Mohammad'
The type of variable (numerical or string) is determined when the variable is
created.
Variables
MATLAB does not require any type declarations or dimension statements.
When MATLAB encounters a new variable name, it automatically creates the
variable and allocates the appropriate amount of storage. If the variable already
exists, MATLAB changes its contents and, if necessary, allocates new storage.
For example, num_students = 25
14
creates a 1-by-1 matrix named num_students and stores the value 25 in its
single element.
Although variable names can be of any length, MATLAB uses only the first N
characters of the name (where N is the number returned by the function
namelengthmax), and ignores the rest. Hence, it is important to make each
variable name unique in the first N characters to enable MATLAB to
distinguish variables.
N = namelengthmax
N=
63
Numbers
MATLAB uses conventional decimal notation, with an optional decimal point
and leading plus or minus sign, for numbers.
Scientific notation uses the letter e to specify a power-of-ten scale factor.
Imaginary numbers use either i or j as a suffix.
Some examples of legal numbers are:
3
-99
0.0001
9.6397238 1.60210e-20
6.02252e23
1i
-3.14159j
3e5i
Operators
Expressions use familiar arithmetic operators and precedence rules.
+ Addition.
- Subtraction.
* Multiplication.
/ Division.
\ Left division.
Note: .+ .- .* ./ .^ .\ : Element by element operation.
- abs(x)
% absolute value of x.
- exp(x) % e to the x-th power.
- log10(x) % logarithm of x (base 10) .
- sqrt(x) % square root of x.
- sin(x)
% sine of x; x in radians.
- sind(x) % sine of x; x in degrees.
For a list of the elementary mathematical functions, type:
help elfun
MATLAB also provides many more advanced mathematical functions,
including Bessel and gamma functions. Most of these functions accept
complex arguments.
15
16
-0.3210
1.2366
-0.6313
-2.3252
-1.2316
1.0556
-0.1132
0.3792
1 3
7 9
If you delete a single element from a matrix, the result is not a matrix anymore.
So, expressions like
>> A(1,2) = [ ]
??? Subscripted assignment dimension mismatch.
Special Characters
0
>> y
y=
1.7321
The last two lines do not end in semicolons so MATLAB prints the results of
z and y.
, command delimiter, e.g.,
20
Graphics
21
Review
- MATLAB provides a wide variety of techniques to display data
graphically.
- MATLAB provides interactive tools enabling you to manipulate graphs to
achieve results that reveal the most information about your data.
- You can also annotate and print graphs for presentations, or export graphs
to standard graphics formats for presentation in web browsers or other
media.
Creating a Graph
The type of graph depends on the nature of the data and the purpose of the
graph. MATLAB predefines many graph types, such as:
- line, bar, histogram, and pie graphs.
- 3-D graphs.
Exploring Data
Once a graph is created, specific information about the data can be extracted
such as peak value in a plot, average value,..
Annotating Graphs
Annotations are the text, arrows, and other labels added to graphs to help
viewers see what is important about the data.
The print preview dialog lets you control the size, layout, and other
characteristics of the graph (select Print Preview from the figure File menu).
Exporting a graph means creating a copy of it in a standard graphics file
format, such as TIF (TIFF- Tag Image File Format), JPEG (Joint Photographic
Experts Group, or EPS (EPS-Encapsulated PostScrip). You can then import the
file into a word processor, include it in an HTML document, or edit it in a
drawing package (select Export Setup from the figure File menu).
Graph Components
- MATLAB displays graphs in a special window known as a figure.
- To create a graph, you need to define a coordinate system.
- The actual visual representation of the data is achieved with graphics
objects like lines and surfaces.
- The objects are drawn within the coordinate system defined by the axes,
which MATLAB automatically creates specifically to accommodate the
range of the data.
- The actual data is stored as properties of the graphics objects.
- xdata & ydata must be the same length and both must be the same type,
i.e., both must be either row or column vectors.
- Additional arguments to the plot command provide other options
including the ability to plot multiple data sets, and a choice of colors,
symbols and line types for the data.
Plot type
Loglog(x,y)
Semilogx(x,y)
y versus log(x)
Semilogy(x,y)
log(y) versus x
24
26
27
Generating M-file
From File menu, select Generate M-File as shown below.
28
29
References
30