Академический Документы
Профессиональный Документы
Культура Документы
MATLAB
1
2
The name MATLAB stands for
MATrix LABoratory.
MATLAB is a high-performance
language for technical computing. It
integrates
computation, visualization, and
programming environment.
3
Matlab is a program for doing
numerical computation. It was
originally designed for solving linear
algebra type problems using matrices.
4
Some other aspects of Matlab
Matlab is an interpreter -> not as fast as compiled
code
Typically quite fast for an interpreted language
Often used early in development -> can then
convert to C (e.g.,) for speed
Can be linked to C/C++, JAVA, SQL, etc
Commercial product, but widely used in industry
and academia
Many algorithms and toolboxes freely available
5
Starting MATLAB
6
Matlab Screen
Command Window
type commands
Current Directory
View folders and m-files
Workspace
View program variables
Double click on a variable
to see it in the Array Editor
Command History
view past commands
save a whole session
using diary
7
Expression
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.60210e20
6.02252e231i -3.14159j 3e5i
8
MATLAB Math & Assignment Operators
Addition + a+b
Subtraction - a-b
Assignment = a=b (assign b to a)
9
MATLAB Relational Operators
10
MATLAB Logical Operators
11
Other MATLAB symbols
>> prompt
... continue statement on next line
, separate statements and data
% start comment which ends at end of line
; (1) suppress output
(2) used as a row separator in a matrix
: specify range
12
Hierarchy of arithmetic operations
13
Variables
14
MATLAB Special Variables
15
Expression
Functions
MATLAB provides a large number of standard elementary
mathematical functions, including abs, sqrt, exp, and sin. For a
list of the elementary mathematical functions, type :
help elfun
For a list of more advanced mathematical and matrix functions,
type:
help specfun
help elmat
16
17
HELP
Within Matlab
Type help at the Matlab prompt or help
followed by a function name for help on a
specific function
Online
Online documentation for Matlab at the
MathWorks website
http://www.mathworks.com/access/helpdesk
/help/techdoc/matlab.html
There are also numerous tutorials online that
are easily found with a web search.
18
Array, Matrix
a vector x = [1 2 5 1]
x=
1 2 5 1
a matrix x = [1 2 3; 5 1 4; 3 2 -1]
x=
1 2 3
5 1 4
3 2 -1
transpose y = x y=
19
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
20
MATLAB Matrices
rowvec =
12 14 63
21
MATLAB Matrices
colvec =
13
45
-2
22
MATLAB Matrices
EDU matrix = [1 , 2 , 3 ; 4 , 5 ,6 ; 7 , 8 , 9]
matrix =
1 2 3
4 5 6
7 8 9
23
Generating Vectors from functions
zeros(M,N) MxN matrix of zeros x = zeros(1,3)
x =
0 0 0
24
Operators (arithmetic)
+ addition
- subtraction
* multiplication
/ division
^ power
complex conjugate transpose
25
Matrices Operations
Given A and B:
26
Some powerful matrix functions in Matlab
X = A % Transposed matrix
X = inv(A) % Inverse matrix squared matrix
X = pinv(A) % Pseudo inverse
X = chol(A) % Cholesky decomp.
d = det(A) % Determinant
[X,D] = eig(A) % Eigenvalues and
eigenvectors
[Q,R] = qr(X) % QR decomposition
[U,D,V] = svd(A) % singular value decomp.
27
Operators (Element by Element)
.* element-by-element multiplication
./ element-by-element division
.^ element-by-element power
28
Scalar - Matrix Addition
EDU a=3;
EDU b=[1, 2, 3;4, 5, 6]
b=
1 2 3
4 5 6
EDU c= b+a % Add a to each element of b
c=
4 5 6
7 8 9
29
Scalar - Matrix Subtraction
EDU a=3;
EDU b=[1, 2, 3;4, 5, 6]
b=
1 2 3
4 5 6
EDU c = b - a %Subtract a from each element of b
c=
-2 -1 0
1 2 3
30
Scalar - Matrix Multiplication
EDU a=3;
EDU b=[1, 2, 3; 4, 5, 6]
b=
1 2 3
4 5 6
EDU c = a * b % Multiply each element of b by a
c=
3 6 9
12 15 18
31
Scalar - Matrix Division
EDU a=3;
EDU b=[1, 2, 3; 4, 5, 6]
b=
1 2 3
4 5 6
EDU c = b / a % Divide each element of b by a
c=
0.3333 0.6667 1.0000
1.3333 1.6667 2.0000
32
The use of . Element Operation
A = [1 2 3; 5 1 4; 3 2 1]
A=
1 2 3
5 1 4
3 2 -1
b = x .* y c=x./y d = x .^2
x = A(1,:) y = A(3 ,:)
b= c= d=
x= y= 3 8 -3 0.33 0.5 -3 1 4 9
1 2 3 3 4 -1
K= x^2
Erorr:
??? Error using ==> mpower Matrix must be square.
B=x*y
Erorr:
??? Error using ==> mtimes Inner matrix dimensions must agree.
33
Evaluate expressions using MATLAB :
Exercise1 :
34
35
Exercise 2 :
36
37
Exercise 3,4 :
38
Exercise 5 :
39
2-D Plots
Exercise 6 :
Example:
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
Now label the axes and add a title. The characters \pi
create the symbol pi.
xlabel('x = 0:2\pi')
ylabel('Sine of x')
title('Plot of the Sine Function','FontSize',12)
40
Dr. Keshav Patidar 40
Multiple Data Sets in One Graph
Multiple x-y pair arguments create multiple graphs
with a single call to plot.
MATLAB automatically cycles through a predefined
(but user settable) list of colors to allow discrimination
between each set of data.
Exercise7 :
For example, these statements plot three related
functions of x, each curve in a separate
distinguishing color.
y2 = sin(x-.25);
y3 = sin(x-.5);
plot(x,y,x,y2,x,y3)
The legend command provides an easy way to identify
the individual plots.
legend('sin(x)','sin(x-.25)','sin(x-.5)')
Dr. Keshav Patidar 41
Specifying Line Styles and Colors
It is possible to specify color, line styles, and markers
(such as plus signs or
circles) when you plot your data using the plot
command.
plot(x,y,'color_style_marker')
color_style_marker is a string containing from one to
four characters
(enclosed in single quotation marks) constructed
from a color, a line style, and a marker type:
Color strings are 'c', 'm', 'y', 'r', 'g', 'b', 'w', and 'k'.
These correspond
to cyan, magenta, yellow, red, green, blue, white, and
Dr. Keshav Patidar 42
black.
The marker types are '+', 'o', '*', and 'x' and the
filled marker types 's for square, 'd' for diamond,
'^' for up triangle, 'v' for down triangle, '>'for right
triangle, '<' for left triangle, 'p' for pentagram, 'h' for
hexagram,
and none for no marker.
Exercise9 :
t = 0:pi/10:2*pi;
plot(exp(i*t),'-o')
draws a 20-sided polygon with little circles at the vertices.
Dr. Keshav Patidar 46
Dr. Keshav Patidar 47
Adding Plots to an Existing Graph
The hold command enables you to add plots to
an existing graph. When you type
hold on
subplot(m,n,p)
Plot(x, y, z)
Plot3(x, y, z)
X(t)= (e^(-0.2t))cos2t
Y(t)= (e^(-0.2t))sin2t
0
1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6
0
1 2 3 4 5 6
0
0 1 2 3 4 5 6
10%
29%
14%
19%
24%
6
150 30
4
180 0
210 330
240 300
270
y= (e^(-x))sin(x)
Where x range is 0 to 2 in steps of 0.1. create the
following plot types (a) stem plot, (b) stair plot, (c)
bar plot, (d) compass plot. Use suitable labels,
titles and legend.
0.8
0.6
0.4
0.2
2
1 2
0 1
0
-1 -1
Dr. Keshav Patidar 64
-
2
-2
0.8
0.6
0.4
0.2
2
1 2
0 1
0
-1 -1
-2 -2
v(t)= 10e^(-0.2+j)t
Where t range is 0 to 10, use command plot3,
where the three dimensions are the real part,
imaginary part of function and time. Use suitable
labels, titles and legend.
z(t)= e^(x+jy)
Where x range is -1 to 1, and y range is -2pi to
2pi.plot real part of z versus x and y. Use suitable
labels, titles and legend.