MATLAB
•Amal Prasad
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
What is MATLAB
• The name MATLAB stands for MATrix LABoratory
• MATLAB was written originally to provide easy access to
matrix software developed by the LINPACK (linear system
package) and EISPACK (Eigen system package) projects
• A high-performance language for technical computing
• It integrates computation, visualization, and programming
environment
• It has sophisticated data structures, contains built-in editing
and debugging tools, and supports object-oriented
programming.
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Advantages of MATLAB
• MATLAB has many advantages compared to conventional
computer languages (e.g., C, FORTRAN) for solving
technical problems
• MATLAB is an interactive system whose basic data element
is an array that does not require dimensioning
• Has powerful built-in routines that enable a very wide variety
of computations
• Easy to use graphics commands that make the visualization
of results immediately available
• There are toolboxes for signal processing, symbolic
computation, control theory, simulation, optimization, and
several other fields of applied science and engineering
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Starting MATLAB
• When you start MATLAB, a special window called the
MATLAB desktop appears
• The desktop is a window that contains other windows
• The Command Window
• The Command History
• The Workspace
• The Current Directory
• The Help Browser
• The Start button
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
MATLAB Workspace
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Using MATLAB as a calculator
• >>1+2*3
ans =
7
• >> x = 1+2*3
x=
7
• >> 4*x
ans =
28.0000
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Quitting MATLAB
• Type quit in the Command Window,
• or select File -> Exit MATLAB in the desktop main menu
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Creating MATLAB variables
• The syntax of variable assignment is
• variable name = a value (or an expression)
• For example,
• >> x = expression
• Overwriting variable
• Once a variable has been created, it can be reassigned.
• If you do not wish to see the intermediate results, you can suppress
the numerical output by putting a semicolon (;) at the end of the line
• >>t = 5;
• >>t = t+1
t=
6
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Error Messages
• If we enter an expression incorrectly, MATLAB will return an
error message.
• >>x = 10;
• >>5x
???5x
Error: Unexpected MATLAB expression
• Making corrections
• A previously typed command can be recalled with the up-arrow
key
• When the command is displayed at the command prompt, it
can be modified if needed and executed
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Controlling the hierarchy of operations
• >> (1+2)*3
ans = 9
• >> 1+2*3
ans = 7
• 1/(2+3^2)+4/5*6/7
ans = 0.7766
• 1/2+3^2+4/5*6/7
ans =10.1857
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Controlling the appearance of Floating Point
• MATLAB by default displays only 4 decimals in the result of the
calculations
• MATLAB does numerical calculations in double precision, which is
15 digits.
• >> format short
• x=-163.6667
• If we want to see all 15 digits, we use the command format long
• >> format long
• x= -1.636666666666667e+002
• To return to the standard format, enter format short, or simply
format.
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Managing the workspace
• >> clear
• The command clear or clear all removes all variables from the
workspace. This frees up system memory
• In order to display a list of the variables currently in the
memory, type
• >> who
• >> whos
• will give more details which include size, space allocation, and
class of the variables
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Entering multiple statements per line
• Use commas (,) or semicolons (;) to enter more than one
statement at once
• Commas (,) allow multiple statements per line without
suppressing output.
• >> a=7; b=cos(a), c=cosh(a)
b = 0.6570
c = 548.3170
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Miscellaneous Commands
• To clear the Command Window, type clc
• To abort a MATLAB computation, type ctrl-c
• To continue a line, type . . .
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Getting help
• >> help sqrt
• >> lookfor inverse
• The help command searches for an exact function name
match, while the lookfor command searches the quick
summary information in each function for a match
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Mathematical Functions
• MATLAB offers many predefined mathematical functions for
technical computing which contains a large set of
mathematical functions
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Pre-defined Constant Values
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Mathematical Functions Example
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Basic plotting
• MATLAB has an excellent set of graphic tools
• Plotting a given data set or the results of computation is
possible with very few commands
• Example
• The MATLAB command to plot a graph is plot(x,y)
• The vectors x = (1, 2, 3, 4, 5, 6) and y = (3, -1,2, 4, 5, 1)
• >> x = [1 2 3 4 5 6];
• >> y = [3 -1 2 4 5 1];
• >> plot(x,y)
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Adding titles, axis labels, and annotations
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Multiple data sets in one plot
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Specifying line styles and colors
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Matrix
• Matrices are the basic elements of the MATLAB environment
• A matrix is a two-dimensional array consisting of m rows and
n columns
• Special cases are
• column vectors (n = 1) and row vectors (m = 1)
• MATLAB supports two types of operations, known as
• matrix operations
• array operations
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Matrix Generation
• Entering a vector
• A vector is a special case of a matrix
• The elements of vectors in MATLAB are enclosed by square brackets
and are separated by spaces or by commas
• >> v = [1 4 7 10 13]
v = 1 4 7 10 13
• Column vectors are created in a similar way, however, semicolon (;) must
separate the components of a column vector
• w = [1;4;7;10;13]
w=
1
4
7
10
13
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Matrix Generation
• A row vector is converted to a column vector using the transpose
operator
• The transpose operation is denoted by an apostrophe or a single quote
(')
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Entering a matrix
• A matrix is an array of numbers. To type a matrix into MATLAB you must
• begin with a square bracket, [
• separate elements in a row with spaces or commas (,)
• use a semicolon (;) to separate rows
• end the matrix with another square bracket, ].
• >> A = [1 2 3; 4 5 6; 7 8 9]
A =
1 2 3
4 5 6
7 8 9
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Matrix indexing
• A(2,1)
ans = 4
• A(1,3)
ans = 3
• Correcting any entry is easy through indexing. Here we substitute
A(3,3)=9 by A(3,3)=0.
• >> A(3,3) = 0
• A=
1 2 3
4 5 6
7 8 0
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Long Array, Matrix
• t =1:10
t =
1 2 3 4 5 6 7 8 9 10
• k =2:-0.5:-1
k =
2 1.5 1 0.5 0 -0.5 -1
• B = [1:4; 5:8]
x =
1 2 3 4
5 6 7 8
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Generating Vectors from functions
• zeros(M,N) MxN matrix of zeros x = zeros(1,3)
x =
0 0 0
• ones(M,N) MxN matrix of ones
x = ones(1,3)
x =
1 1 1
• rand(M,N) MxN matrix of uniformly
distributed random x = rand(1,3)
numbers on (0,1) x =
0.9501 0.2311 0.6068
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Matrix Index
• The matrix indices begin from 1 (not 0 (as in C))
• The matrix indices must be positive integer
Given:
A(-2), A(0)
Error: ??? Subscript indices must either be real positive integers or logicals.
A(4,2)
Error: ??? Index exceeds matrix dimensions.
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Concatenation of Matrices
• x = [1 2], y = [4 5], z=[ 0 0]
A = [ x y]
1 2 4 5
B = [x ; y]
1 2
4 5
C = [x y ;z]
Error:
??? Error using ==> vertcat CAT arguments dimensions are not consistent.
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Matrices Operations
Given A and B:
Addition Subtraction Product Transpose
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Introduction to programming in MATLAB
• Commands entered in the Command Window cannot be saved and
executed again for several times
• Therefore, a different way of executing repeatedly commands with
MATLAB is:
• to create a file with a list of commands,
• save the file, and
• run the file
• The files that are used for this purpose are called script files or scripts for
short (M-File Scripts)
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
M-File Scripts Example
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
If…Else
discr = b*b - 4*a*c;
if discr < 0
disp('Warning: discriminant is negative, roots are imaginary');
else
disp('Roots are real, but may be repeated')
end
discr = b*b - 4*a*c;
if discr < 0
disp('Warning: discriminant is negative, roots are imaginary');
elseif discr == 0
disp('Discriminant is zero, roots are repeated') else
disp('Roots are real')
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
end
Input to a script File
• The assignment of a value to a variable can be done in three
ways:
• The variable is defined in the script file.
• The variable is defined in the command prompt.
• The variable is entered when the script is executed.
• game1 = input('Enter the points scored in the first game ');
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Output Commands
• Two commands that are frequently used to generate output
are:
• disp and fprintf
• disp(‘ Hello World ‘)
• fprintf('a is less than 20\n' );
• fprintf('value of a is : %d\n', a);
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
While…end Loop
while( a < 20 )
fprintf('value of a: %d\n', a);
a = a + 1;
end
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
For..end Loop
for ii=1:5
x=ii*ii
end
x=1
while x <= 10
x = 3*x
end
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.
Questions
• ?
• ?
• ?
• ?
• ?
• Contact information:
• amal.prasad2010@gmail.com
• +91-91790 40166
Copyright © 2005 – 2016, Amal Prasad. All rights reserved.