Professional Documents
Culture Documents
What is MATLAB?
All problems and solutions in MATLAB are expressed in notation used in linear
algebra and essentially involve operations using matrices and vectors.
Getting started
Help browser
Workspace
Current folder
Access your files.
Command window
Command history
Start button
Getting started
Help browser
Workspace
Current folder
Access your files.
Command window
Command history
View or rerun commands
that you entered at the
command line.
For a simple interactive calculation, just type the expression you want to
evaluate. For example, calculate the expression 1 + 2 * 3.
Type it at the prompt command (>>) as follows:
>> 1+2*3
ans =
7
Note that if you do not specify an output variable, MATLAB uses a default
variable ans, short for answer, to store the results of the current calculation.
Also, the variable ans is overwritten, if it already exists.
x =
7
To make corrections,
o
o
Retype the expressions. But if the expression is lengthy, chances are high to make
more mistakes by typing second time.
Recall a previously typed command with the up-arrow key . When the command is
displayed at the command prompt, it can be modified if needed.
Note that commas (,) allow multiple statements per line without suppressing
the output.
>> a=7; b=cos(a), c=cosh(a)
b =
0.7539
c =
548.3170
For example, the result of 1+2*3 is quite different than the similar expression with
parentheses (1+2)*3.
>> 1+2*3
ans =
>> (1+2)*3
while
Expression
1
3+22
2
5
6
7
>> 1/(3+2^2)+2/5*6/7
ans =
0.4857
ans =
>> 1/3+2^2+(2/5)*(6/7)
while
ans =
4.6762
MATLAB Help
Open the function documentation in a separate window using the doc command.
>> doc mean
Display function hints (the syntax portion of the function documentation) in the
Command Window by pausing after you type the open parentheses for the function
input arguments.
>> mean(
MATLAB Help
Another way to get help is to use the lookfor command which differs from the
help command as follows:
o
o
Suppose that you are looking for a function to take the inverse of a matrix.
Since MATLAB does not have a function named inverse, the command help
inverse will produce nothing.
>> help inverse
inverse not found.
Use the Help browser search field to search the documentation, or
type "help help" for help command options, such as help for methods.
On the other hand, the command lookfor inverse will produce detailed
information, which includes the function of interest, inv.
>> lookfor inverse
inverter
invhilb
ipermute
acos
acosd
acosh
inv
Inverses of Matrices
Inverse Hilbert matrix.
Inverse permute array dimensions.
Inverse cosine, result in radians.
Inverse cosine, result in degrees.
Inverse hyperbolic cosine.
- Matrix inverse.
Mathematical functions
There is a long list of mathematical functions that are built into MATLAB.
o
o
o
Example expressions:
o
640.0061
Mathematical functions
Example expressions:
o
Natural logarithm
>> log(142)
ans =
4.9558
Sine function
>> sin(pi/4)
ans =
0.7071
Exponential function
>> exp(10)
ans =
2.2026e+04
ans
To get more details about the variable which include size, space allocation, and
class of the variables, type whos
>> whos
Name
a
b
c
Size
Bytes
Class
1x1
100x100
1x1
8
80000
8
double
double
double
Attributes
Variables in MATLAB are just like variables in any other programming language
(C, C++ etc.) Only difference is that you do not have to define them by
indicating the type etc.
Vectors are matrices having a single row (a row vector), or a single column (a
column vector).
Put together, all MATLAB variables are multidimensional arrays, no matter what
type of data. A matrix is a two-dimensional array often used for linear algebra.
The simplest way to create a matrix in MATLAB is to use the matrix constructor
operator, [ ].
comma or space results in a row vector, eg. [E1, E2, ..., Em] or [E1 E2 ... Em]
>> a = [1 2 3 4]
a =
1
semicolon results in a column vector, eg. [E1; E;...; Em] or [E1 E2 ... Em]
>> a = [1; 2; 3; 4]
a =
1
2
3
4
10
ans =
4
To create a matrix that has multiple rows, separate the rows with semicolons.
A = [row1; row2; ...; rown]
62
2
17
25
93
87
-72
6
-8
43
95
-11
22
91
6
-30
2
3
4
5
6
7
8
9
10
11
Individual elements of the matrix, for instance the element in the 2nd row, 5th
column can be accessed using the notation:
>> B(2,5)
ans =
9
MATLAB has a number of functions that create different kinds of matrices some
of which are specialized matrices. The functions shown below create matrices
for more general use:
>> ones(2,4)
ans =
1
1
1
1
1
1
1
1
0
1
0
0
0
1
>> zeros(1,4)
ans =
>> eye(3)
ans =
1
0
0
MATLAB has a number of functions that create different kinds of matrices some
of which are specialized matrices. The functions shown below create matrices
for more general use:
>> diag([4 5 6])
ans =
4
0
0
0
5
0
0
0
6
>> rand(4,2)
ans =
0.8147
0.9058
0.1270
0.9134
0.6324
0.0975
0.2785
0.5469
>> randn(4,2)
ans =
3.5784
2.7694
-1.3499
3.0349
0.7254
-0.0631
0.7147
-0.2050
When entering signed numbers into a matrix, make sure that the sign
immediately precedes the numeric value.
>> A = [9 +3 -0.5]
>> A = [9+3-0.5]
while
A =
9.0000
3.0000
A =
-0.5000
11.5000
A =
A =
while
11
3
21
-2
4
20
-20
-6
2
-11
1
43
The basic arithmetic operations +, -, *, / can be used for vectors and matrices.
These would generate corresponding output vectors or matrices.
10
12
Only vectors that have the same number of elements can be added or
subtracted. Similarly, two matrices with identical number of rows and columns
can be subtracted as follows:
>> A = [1:3:20; 21:3:40];
>> B = [2:3:20; 22:3:40];
>> C = A - B
C =
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
For matrix multiplication, use the * operator where the number of columns in
the first matrix should be equal to the number of rows in the second one.
>> A =[1 2 3; 4 5 6]
>> B = A'
>> C =A*B
A =
B =
C =
1
4
2
5
3
6
1
2
3
4
5
6
14
32
4
18
6
21
8
24
32
77
For example:
2
5
8
11
3
6
9
12
>> size(A)
ans =
4
The answer represents the number of rows followed by the number of columns.
Alternatively,
>> [m,n]=size(A)
m =
4
n =
3
The element of row i and column j of the matrix A is denoted by A(i, j). Thus,
A(i, j) in MATLAB refers to the element of matrix A. The first index is the row
number and the second index is the column number.
1
4
7
10
>> A(1,3)
2
5
8
11
3
6
9
12
A =
1
4
7
10
2
5
8
11
3
6
0
12
ans =
3
The colon operator can be used to pick out a certain row or column. For
example, the statement A(m:n, k:l) specifies rows m to n and column k to l.
Subscript expressions refer to portions of a matrix. For example, following shows
the second row elements of A.
>> A(2,:)
ans =
4
The colon operator can also be used to extract a sub-matrix from a matrix A.
Following (on the left below) is a sub-matrix with the last two columns of A.
>> A(:,2:3)
>> A(:,2)=[ ]
>> A
ans =
A =
A =
2
5
8
11
3
6
0
12
1
4
7
10
3
6
0
12
where
1
4
7
10
2
5
8
11
3
6
0
12
>> A(3,:) = [ ]
A =
A =
1
4
10
where
1
4
7
10
3
6
0
12
In the example above, third row of matrix A is deleted. To restore the third row,
we use a technique for creating a matrix (on the left).
>> A = [A(1,:);A(2,:);[7 9];A(3,:)]
A =
A =
1
4
7
10
3
6
12
3
6
9
12
1
4
7
10
2
5
8
11
3
6
9
12
After restoring the second column (on the right), matrix A is now restored to its
original form.
A =
B =
where
4
7
2
5
8
11
3
6
9
12
To interchange rows 1 and 2 of A, use the vector of row indices together with
the colon operator.
>> C = A([2 1 3 4],:)
>> A
C =
A =
4
1
7
10
1
4
7
10
5
8
5
2
8
11
6
3
9
12
where
1
4
7
10
2
5
8
11
3
6
9
12
It is important to note that the colon operator (:) stands for all columns or all
rows.
>> A
>> A(2:end,2:end)
A =
ans =
5
8
11
6
9
12
where
1
4
7
10
2
5
8
11
3
6
9
12
>> A
ans =
A =
12
9
6
3
where
1
4
7
10
2
5
8
11
3
6
9
12
>> A'
For example,
A =
ans =
1
2
3
4
5
6
7
8
9
10
11
12
where
1
4
7
10
2
5
8
11
3
6
9
12
In other words, the transpose of m*n real matrix A is the n*m matrix that results
from interchanging the rows and columns of A.
The period character, (.), distinguishes the array operations from the matrix
operations. However, since the matrix and array operations are the same for
addition (+) and subtraction (-), the character pairs (.+) and (.-) are not used.
For example, let A and B be two matrices of the same size with elements
= [ ]and = [ ]
Original matrices,
2
5
8
3
6
9
10
40
70
20
50
80
30
60
90
B =
Multiplication
10
160
490
40
250
640
90
360
810
>> D = B./A
D =
Division
10
10
10
Power
10
10
10
10
10
10
>> P = A.^E
>> E = ones(3)*2
P =
E =
1
16
49
4
25
64
9
36
81
where
2
2
2
2
2
2
2
2
2
Loops
The for ... end loop (very similar to C expression) is a simple command for
setting up a loop.
For example:
16
25
36
49
64
81
100
All statements between the for and the end statements will be executed as
per the command specifications. Example of a for loop where the increment is
not one (1) would be as follows:
>> for i = 1:3:20;
f = [f,i];
end
>> f
f =
1
10
13
16
19
Loops
The while ... end loop is used when the number of passes is not specified. The
looping continues until a stated condition is satisfied. The while loop has the
following form:
For example:
>> x = 1;
>> while x <= 10;
x = 3*x
end
Conditional Statements
In MATLAB, you can conditionally execute sections by using the keyword if. The
syntax is as follows:
if ... end
if ... elseif ... end
For example:
Plotting
To create two-dimensional line plots, use the plot function. It generates plots
for functions of one variable. (See help for complete details.)
For example, plot the value of the sine function for x on the interval [0,10] as
follows:
Plot of the Sine Function
1
0.8
0.6
0.4
0.2
sin(x)
>> x = [0:0.1:10];
>> y = sin(x);
>> figure(1):plot(x, y);
>> xlabel('x')
>> ylabel('sin(x)')
title('Plot
of
the
Sine
Function')
0
-0.2
-0.4
-0.6
-0.8
-1
5
x
10
You can print a plot using the Print Option under the File Menu in the Figure
Window.
Saving Data
2
5
8
11
3
6
9
12
>> save A
The above example will save the variable A from the current workspace in a
MATLAB formatted binary file (MAT-file) called A.mat which is the default
format.
>> save A.txt A -ascii
Alternatively, the variable can be saved in ASCII format in the file specified
(A.txt under the current directory) so that it can be read by other programs.
Loading Data
For example, it is possible to save all variables from the workspace in binary
MAT-file test.mat. Then, remove the variables from the workspace, and
retrieve the data with the load function.
>> save test.mat
>> clear
>> load test.mat
MATLAB Scripts
MATLAB Scripts
For example, enter some code that plots a vector of random data in the editor
window.
n = 50;
r = rand(n,1);
plot(r)
Save the file in the current folder using the toolbar in the editor window or the
Save option in the File menu.
In matrix notation, the general problem takes the following form: Given two
matrices A and B, does there exist a unique matrix , so that = or =
?
a1 x + b1 y + c1 z = d1
a2 x + b2 y + c2 z = d2
a3 x + b3 y + c3 z = d3
=
P = [a1 b1 c1; a2 b2 c2; a3 b3 c3]
U = [x; y; z]
Q = [d1; d2; d3]
In the symbolic form = , is the 3x3 coefficient matrix and is the 3x1
column vector on the right hand side.
>> A = [ 0 1 2; 1 2 1; 3 5 2]
A =
1.
0
1
3
1
2
5
2
1
2
>> B = [ 1; 3; 7]
2.
B =
1
3
7
3.
>> X=A\B
X =
-2.0000
3.0000
-1.0000
Exercises
References