You are on page 1of 40

C

o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Chapter 1
Introduction to MATLAB
Desktop
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
After studying this chapter you should be
able to:
Gain an overview of MATLAB desktop
Use help for executing MATLAB commands /
functions
Write basic MATLAB code and create m-files
Use MATLAB demos
Chapter 1
Introduction to MATLAB
Desktop
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
What is MATLAB?
Matlab is a high level language which has many specialized
toolboxes for making things easier for us. How high?
Assembly
High Level
Languages such as
C, Pascal etc.
Matlab
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
MATLAB = MATrix LABoratory
Official Website:
www.mathworks.com
or
www.mathworks.in
Advantages of MATLAB
Ease of use
Platform independence
Predefined functions in varied domains
Integrates Programing, Plotting, Simulation and
Hardware Interface
Disadvantages of MATLAB
Can be slow
Commercial software
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Getting Started
To start MATLAB, double-click the icon named MATLAB on
your PCs desktop and the MATLAB working environment pops
up. This is an interactive environment, i.e., you can start
entering commands and obtain results instantly. In the
MATLAB environment, you can see a menubar at the top of
the screen and the command window along with some other
windows occupy the rest of the screen. In the MATLAB
command window, a command can be executed by typing it at
the MATLAB prompt ( >> ) and pressing the ENTER key.
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
The MATLAB Desktop
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
The Command Window
A user can assign values to the variables and write
expressions at the command prompt (>>) in the
command window and they can be executed
instantaneously. As a simple example, try entering the
following commands and observe the results in your
MATLAB command window:
>> a = 2
>> b = 4;
>> c = b+a^2
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Variables in MATLAB
No need for types. i.e.,
All variables are created with double precision unless
specified and they are matrices.
After these statements, the variables are 1x1 matrices
with double precision
int a;
double b;
float c;
Example:
>>x=5;
>>x1=2;
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Variables and Arrays
Array: A collection of data values organized into
rows and columns, and known by a single name.
Row 1
Row 2
Row 3
Row 4
Col 1 Col 2 Col 3 Col 4 Col 5
arr(3,2)
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Arrays :
The fundamental unit of data in MATLAB
Scalars are also treated as arrays by MATLAB (1 row and 1
column).
Row and column indices of an array start from 1.
Arrays can be classified as vectors (one dimensional) and
matrices (Two- Dimentional).
Size of an array is specified by the number of rows and the
number of columns, with the number of rows mentioned first
(For example: n x m array).
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Variables in MATLAB
Variable names must begin with a letter, followed by any
combination of letters, numbers and the underscore (_)
character.
The MATLAB language is Case Sensitive: NAME, name and
Name are all different variables.
Give meaningful (descriptive and easy-to-remember) names
for the variables. Never define a variable with the same
name as a MATLAB function or command.
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Common types of MATLAB variables
double: 64-bit double-precision floating-point numbers
They can hold real, imaginary or complex numbers in
the range from 10
-308
to 10
308
with 15 or 16 decimal
digits.
>> var = 1 + i ;
char: 16-bit values, each representing a single character
The char arrays are used to hold character strings.
>> comment = This is a character string ;
The type of data assigned to a variable determines the
type of variable that is created.
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
The Command History Window
The Command History window displays a list of commands
that a user has entered in the command window.
The commands remain in the history until they are
deleted. A section of these commands can be executed
again by selecting it using (Shift + Arrow) keys.
To execute the previously executed commands, press
UP () arrow key at the command prompt.
Typing the first letter/word of any previous entry,
followed by an UP () arrow displays all the pervious
entries beginning with that letter/word in sequence.
The command can then be re-executed by pressing
ENTER.
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
The Command History Window
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Current Directory Browser
You can set the current working directory of MATLAB by
typing the path of a particular folder over the Current
Directory window in the MATLAB Desktop. Alternatively,
you can also use the icon to browse and set
the current working directory. Then set the current
directory to a folder where your files are stored.
MATLAB would look for files for execution in current
working directory.
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Workspace and Array Editor
When you issue commands like:
>> x = 10;
>> y = [1 2 3 4]; % Array variable
MATLAB creates variables named x and y, and saves
them in a part of computer memory known as
Workspace.
The symbol % is used in MATLAB for inserting the
comments for the clarity of the codes.
Anything written after % in a line is not executed (even
if it is an executable statement or expression).
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
You can observe the contents of the Workspace by typing
the whos command
Double-clicking on any variable in the Workspace Browser
Window brings up the Array Editor which allows the user
to observe and modify the information stored in the
variable. Any variable on Workspace can be manipulated
using the array editor.
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
The Help Browser
The ? icon on the MATLAB desktop launches the Help
Window, which provides links to the documentation on
all the components (or toolboxes) for your MATLAB
version. It provides an important and dynamic reference
for working on MATLAB.
There is another simpler way to get help on different
functions e.g. by typing >> help sqrt at the
command prompt you get the help on function sqrt.
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
The Edit/Debug Window
The Editor/Debugger is a single tool that can be used for
editing, debugging, or both.
You can open a New File in the Editor Window using the
toolbar:
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Instead of writing the commands directly in the Command
Window, a series of commands may be placed into a file
known as script file or M-file (saved with extension .m). The
entire file can be executed just by typing its name (without
extension name) at the command prompt. For example,
consider the following file simple.m:
-----------------simple.m--------------------
x = 10;
y = x/4;
z = y*9;
------------------------------------------------
This can be executed in command space in the following
manner (assuming the file is in the current working directory):
>> simple
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Path Browser
The Path Browser allows you to view and modify the
MATLAB search path.
It allows the user to add, delete, or change the order of
directories in the path.
Make sure that the folder where you have saved your M-
file exists in MATLAB Path, otherwise it wont be
executed.
If there are more than one functions, commands or script
files (M-files) with the same name, the first one found in
the search path will be executed and all the others will be
inaccessible.
It is extremely important not to define a variable with
the same name as a standard MATLAB function or
command, which will make that function inaccessible !!
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Getting Help from Command Window
To access help from command window, type help at the
MATLAB prompt and press ENTER. e.g.
>> help
>> help plot
To search a particular topic by title, the lookfor command
proves useful. For example,
>> lookfor integral
The output of this is a list of several functions that are related
to integrals and contain the word integral in their help
documentation.
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Other important commands with similar purpose are:
what : lists the MATLAB related files found in the current
working directory,
dir : lists all files in the current working directory,
which FUNCT : displays the full pathname of the function
with the name FUNCT.
You can also learn how to effectively use help command by
typing the following:
>> help help
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
General Syntax
MATLAB automatically creates a variable in the memory as
soon as it is assigned a value.
The variable names must begin with a letter, followed by
any combination of letters, numbers and the underscore (_ )
character.
Variable names are case sensitive, in the sense that the
uppercase and lowercase letters are distinguished. For
example, MULT, mult and Mult are three different
variables.
MATLAB requires that all variable names (except for those used
by symbolic toolbox) be assigned numeric values prior to being
used in an expression. For example:
>> a=2.3;
>> b = 4.7;
>> d = c + a*b;
MATLAB responds as:
??? Undefined function or variable 'c'.
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Several variables (or expressions) can also be defined in the
same line, separated by a comma (,) or a semicolon (;). For
example, when you type the following:
>> x=1.1, y=3.3, z=5.5
MATLAB responds with the following display:
x =
1.1
y =
3.3
z =
5.5
The use of semicolons in place of the commas would suppress
the outputs, e.g.
>> x=3.5; y=1.9; z=2.75;
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Writing Simple Expressions
Unlike many other programing languages, you need not
declare the type of values a variable can take in MATLAB.
For example, a variable a can take an integer value, and
in the next step, you can assign the same variable a float
value and in the third step, you can assign it a string and
so on. Here is an example:
>> newvar = 5
newvar =
5
>> newvar = 20.197
newvar =
20.1970
>> newvar = 'surabhi'
newvar =
surabhi
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Operators (arithmetic)
+ addition
- subtraction
* multiplication
/ division
^ power
complex conjugate transpose
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Writing Simple Expressions
variable_name = expression :
addition a + b a + b
subtraction a - b a - b
multiplication a x b a * b
division a / b a / b
exponent a
b
a ^ b
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Hierarchy of operations
Guess what is value of this ? :
x = 3 * 2 + 6 / 2
Processing order of operations is important
parentheses (starting from the innermost)
exponentials (from left to right)
multiplications and divisions (from left to right)
additions and subtractions (from left to right)
>> x = 3 * 2 + 6 / 2
x =
9
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
The parentheses ( ) are used in complex operations. Other
braces (such as { } and [ ] ) are reserved for special
purposes in MATLAB. Here are some examples of writing
simple expressions:
Mathematical Expression MATLAB Code
1. x + 2y 3 (y-x)
2. 2 x
3
+ 5/x
5
3. 5 x
1/3
+ 9 y
0.173
4. 3 tan(x)/ (1+ sin
2
(x))
x + 2*y 3* (y-x)
2*x^3 + 5/x^5
5*x^(1/3) + 9*y^(0.173)
3*tan(x)/(1+(sin(x))^2)
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Screen Display Control (The "format"
function):
MATLAB performs numeric computations in double-precision
format. The number of digits after the decimal of a floating
point number can be controlled by using the format command.
>> format % this is same as 'format short', the
% default format
>> pi % pi has a pre-defined value in MATLAB
ans =
3.1416
>> format long % change the display format to long
>> pi
ans =
3.14159265358979
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
>> format long e
>> pi
ans =
3.141592653589793e+000
>> format hex
>> pi
ans =
400921fb54442d18
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
>> format compact
>> x = 2, y = 3,
x =
2
y =
3
This results in the values being displayed, but the
display is compact to save your Desktop area.
See >> help format for more details on the other
display formats available with MATLAB
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Changing the data format
>> value = 12.345678901234567;
format short 12.3457
format long 12.34567890123457
format short e 1.2346e+001
format long e 1.234567890123457e+001
format short g 12.346
format long g 12.3456789012346
format rat 1000/81
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
MATLAB Demonstrations
MATLAB is shipped with a number of demonstration programs.
Use >> help demos to find out more about these. You can
also type >> demo on the MATLAB prompt to enter the
demonstration programs. The number of demos depends upon
the version of MATLAB that you have.
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
How to Quit MATLAB ?
You can leave a MATLAB session by typing quit or by typing
exit at the MATLAB prompt. If MATLAB session enters into an
infinite loop, you can interrupt the running of the program by
a Ctrl+c command the from keyboard, and thus you would
get back to the command prompt.
C
o
p
y
r
i
g
h
t


2
0
1
4

D
o
r
l
i
n
g

K
i
n
d
e
r
s
l
e
y

(
I
n
d
i
a
)

P
v
t
.

L
t
d
Programming in MATLAB Authors: Ram N. Patel & Ankush Mittal
Summary (some important commands)
help command Online help
lookfor keyword Lists related commands
which Version and location info
clear Clears the workspace variable
clc Clears the command window
who, whos Lists content of the workspace
ctrl+c Aborts operation
Continuation
% Comments

You might also like