Tariq Mahmood: Lab:1-2 Introduction To Matlab
Tariq Mahmood: Lab:1-2 Introduction To Matlab
Tariq Mahmood: Lab:1-2 Introduction To Matlab
Tariq Mahmood
Email: tariq_mehmood@comsats.edu.pk
Preview
What is MATLAB?
It is a numerical computing environment ;
A high-level programming language;
Matrix and vector-based methods;
It can be used for:
– Mathematics and computation
– Easy vector and matrix entering and manipulation
– Plotting of information
– Implementation of algorithms
– Interfacing with programs in other languages
– …
Introduction
Where to use MATLAB
– Math and computation
– Algorithm development
– Data acquisition
– Modeling, simulation, and prototyping
– Data analysis, exploration, and visualization
– Scientific and engineering graphics
– Application development, including graphical user interface
building
Introduction
MATLAB system:
Integrative Development Environment (IDE)
MATLAB Mathematical Function Library
– Including Bassel functions and fast Fourier Transformation
MATLAB Language
– high-level matrix/array language
– object-oriented
Graphics
– display vectors and matrices as graphs
API
– C/Fortran program can interact with MATHLAB
Introduction--Desktop
Introduction - Desktop
• Command Window • Command History
– enter variables – View statements
* Multiplication
/ Division
F = 5*ones(3,3)
F=
5 5 5
5 5 5
5 5 5
N = fix(10*rand(1,10))
N=
9 2 6 4 8 7 4 0 8
Mathematics that can be done using Matlab
>> c=[b ; b]
c =
2.0000 3.0000 4.0000 6.0000
1.0000 1.5000 2.0000 3.0000
2.0000 3.0000 4.0000 6.0000
1.0000 1.5000 2.0000 3.0000
Vectors and Matrices
>> D=c(2:3, 2:3)
D =
1.5000 2.0000
3.0000 4.0000
>> who
Your variables are:
D a b c
>> whos
Name Size Bytes Class
D 2x2 32 double array
a 1x2 16 double array
b 2x4 64 double array
c 4x4 128 double array
Grand total is 30 elements using 240 bytes
Deleting Rows and Columns
You can delete rows and columns from
a matrix using just a pair of square
brackets. Start with
A = [16.0 3.0 2.0 13.0; 5.0 A =
10.0 11.0 8.0; 9.0 6.0 7.0
12.0; 4.0 15.0 14.0 1.0 ] 16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
X = A;
Then, to delete the second column of X,
use X =
X(:,2) = []
16 2 13
This changes X to 5 11 8
9 7 12
4 14 1
Deleting Rows and Columns
If you delete a single element from a matrix, the result is not a matrix
anymore. So, expressions like
X(1,2) = []
result in an error.
However, using a single subscript deletes a single element, or sequence
of elements, and reshapes the remaining elements into a row vector.
So
A =
A(2:2:10) = []
A= 16 3 2 13
Columns 1 through 7 5 10 11 8
9 6 7 12
16 9 3 6 2 7 14 4 15 14 1
Columns 8 through 11
13 8 12 1
Linear Algebra
Informally, the terms matrix and array are often used interchangeably.
More precisely, a matrix is a two-dimensional numeric array that
represents a linear transformation. The mathematical operations
defined on matrices are the subject of linear algebra.
Consider a square : A = [2 1 1 ; 1 2 1;1 1 2]
C = A'*A A =
2 1 1
1 2 1
1 1 2
C =
6 5 5
5 6 5
5 5 6
Linear Algebra
The determinant of matrix A is: A =
2 1 1
d = det(A)
1 2 1
d= 4 1 1 2
The inverse of A is Y =
0.7500 -0.2500 -0.2500
Y = inv(A) -0.2500 0.7500 -0.2500
-0.2500 -0.2500 0.7500
M-Files:
M-files are macros of MATLAB commands that are
stored as ordinary text files with the extension "m",
that is filename.m.
An M-file can be either a function with input and output
variables or a list of commands.
M-Files:
The following describes the use of M-files on a PC version of
MATLAB.
MATLAB requires that the M-file must be stored either in the working
directory or in a directory that is specified in the MATLAB path list.
For example, consider using MATLAB on a PC with a user-defined M-
file stored in a directory called "\MATLAB\MFILES";.
Then to access that M-file, either change the working directory by
typing cd\matlab\mfiles from within the MATLAB command window or
by adding the directory to the path.
Permanent addition to the path is accomplished by editing the
\MATLAB\matlabrc.m file.
Temporary modification to the path is accomplished by typing
path(path,'\matlab\mfiles') from within MATLAB.
Programming in MATLAB (1)
If Statement a = 3;
Syntax: b = 5;
if a < b
if expression
statements; a = a + 1;
end b = b - 1;
end
Programming in MATLAB (2)
Syntax: b=3
if expression if a < b
statements1 a=a+1
else
statements2 b=b-1
end
else
a=a-1
b=b+1
end
Programming in MATLAB (5)
Syntax: a = 1
while expression b = 5
while a <= b
statements
end a = a + 1
end
Programming in MATLAB (6)
function y = humps(x)
y = 1./((x-.3).^2 + .01) + 1./((x-.9).^2 + .04) - 6;
x = 0:.002:1;
y = humps(x); Run this as script3.m
plot(x,y)
Specifying the Color and Size of Markers
Resulting
figure
Plotting commands
PLOT(X,Y) plots vector Y versus vector X.
TITLE('text') adds text at the top of the current plot.
XLABEL('text') adds text beside the X-axis on the current axis.
YLABEL('text') adds text beside the Y-axis on the current axis.
GRID, by itself, toggles the major grid lines of the current axes.
GTEXT('string') displays the graph window, puts up a cross-hair, and
waits for a mouse button or keyboard key to be pressed.
SUBPLOT(m,n,p), breaks the Figure window into an m-by-n matrix of small
or SUBPLOT(mnp), axes.
STEM(Y) stemDiscrete sequence or "stem" plot. STEM(Y) plots the
data sequence Y as stems from the x axis terminated with
circles for the data value.
SEMILOGX(...) is the same as PLOT(...), except a logarithmic (base 10)
scale is used for the X-axis.
SEMILOGY(...) is the same as PLOT(...), except a logarithmic (base 10)
scale is used for the Y-axis..
Multiple Graphs
Two ways to make multiple plots on a single graph are illustrated here.
Another way is with the hold command. The command hold freezes the
current graphics screen so that subsequent plots are superimposed on
it. Entering hold again releases the "hold". The commands hold on and
hold off are also available.
One can override the default line types and point types. For example,
% prog3.m
t = 0: 0.01: 2*pi;
y1 = sin(t);
y2 = sin(2*t);
y3 = sin(4*t);
plot(t, y1, '--', y2, ‘*', y3, '+') Graph is on
next page
Multiple Graphs
Results
Plotting colors and line styles
Colors Line Styles
y yellow . point
M magenta o circle
C cyan x x-mark
R red + plus
G green - solid
B blue * star
W white : dotted
K black -. Dashdot
-- dashed
More mark types are; square(s), diamond(d), up-triangle(v), down-
triangle(^), left-triangle(<), right-triangle(>), pentagram(p), hexagram(h)
See also help plot for more line and mark color.
Subplot
The command subplot can be used to partition the
screen so that up to four plots can be viewed
simultaneously. See help subplot.
Example for use of subplot:
% prog4.m
% Line plot of a chirp
x = 0 : 0.05 : 5;
y = sin(x.^2);
subplot(2,1,1), plot(x,y);
% Bar plot of a bell shaped curve
x = -2.9 : 0.2 : 2.9;
subplot(2,1,2), bar(x,exp(-x.*x)); Graph is on
next page
subplots
Results
Various Plotting Functions:
MATLAB supports many types of graph and surface plots:
% prog5.m
t = 0:pi/50:10*pi;
plot3(sin(t),cos(t),t,'LineWidth',3)
Graph is on
axis square; grid on
next page
Line Plots of 3-D Data
Production of helix