Beruflich Dokumente
Kultur Dokumente
Introduction to MATLAB
What is MATLAB
High level language for technical
computing
Stands for MATrix LABoratory
MATLAB Desktop
Menu and toolbar
Workspace
History
Command
MATLAB Windows
Matlab
Basics
Variables:
-Variables are assigned numerical values
by typing the expression directly
>> a = 1+2
>> a =
3
>> a = 1+2;
press enter
Interactive Commands
Interactive Commands
Interactive Commands
Format of output
Variables
X is not equal to x
Variables
Variables
>> y= 2*(1+4*j)
>> y=
2.0000 + 8.0000i
MATLAB Basics:
Arithmetic operators:
+ addition,
- subtraction,
* multiplication,
/division,
^ power operator,
' transpose
Array, Matrix
a vector
x = [1 2 5 1]
x =
1
a matrix
x = [1 2 3; 5 1 4; 3 2 -1]
x =
1
5
3
2
1
2
transpose
3
4
-1
y = x
y =
1
2
5
1
t =1:10
t =
2
3
4
k =2:-0.5:-1
k =
1.5
0.5
-0.5
= [1:4; 5:8]
x =
1
5
2
6
3
7
4
8
-1
10
x = ones(1,3)
x =
1
1
1
x = rand(1,3)
x =
0.9501 0.2311 0.6068
Matrix Index
Given:
A(-2), A(0)
Error: ??? Subscript indices must either be real positive integers or
logicals.
A(4,2)
Error: ??? Index exceeds matrix dimensions.
Concatenation of Matrices
B = [x ; y]
1 2
4 5
C = [x y ;z]
Error:
??? Error using ==> vertcat CAT arguments dimensions are not consistent
Operators (arithmetic)
+ addition
- subtraction
* multiplication
/ division
^ power
complex conjugate transpose
Matrices Operations
Given A and
B:
Additio
n
Subtractio
n
Product
Transpose
Creating Matrices
Matrix operations
^: exponentiation
*: multiplication
/: division
Array Operations
:
:
:
:
array
array
array
array
But:
>> A.*B
5
21
12
32
sum(A): summation.
Indexing Matrices
Given the matrix:
Then:
A =
0.9501
0.6068
0.4231
0.2311
0.4860
0.2774
A(1,2) = 0.6068
A(3) = 0.6068
index (i 1)m j
A(:,1) = [0.9501
1:m
]
0.2311
A(1,2:3)=[0.6068
0.4231]
>> C=[1 2; 3 4]
C=
1 2
3 4
>> C(3,:)=[5 6];
C=
1 2
3 4
5 6
>> D=linspace(4,12,3);
>> E=[C D]
E=
1 2 4
3 4 8
5 6 12
Basics Matrices
Special Matrices
null matrix: M = [];
nxm matrix of zeros:
nxm matrix of ones:
nxn identity matrix:
M = zeros(n,m);
M = ones(n,m);
M = eye(n);
Matrix Operations
Matrix operations:
A+B is valid if A and B are of same size
A*B is valid if As number of columns equals
Bs number of rows.
A/B is valid and equals A.B-l for same size
square matrices A & B.
Order of Operations
Parentheses
Exponentiation
Multiplication and division have equal
precedence
Addition and subtraction have equal
precedence
Evaluation occurs from left to right
When in doubt, use parentheses
Logical Operators
Operators (Element by
Element)
.*
element-by-element multiplication
./ element-by-element division
.^ element-by-element power
1
5
3
2
1
2
x = A(1,:)
x=
3
4
-1
y = A(3 ,:)
3
y=
3 4 -1
b = x .* y
c=x./y
d = x .^2
b=
c=
0.33
-3
d=
-3
3 8
0.5
K= x^2
Erorr:
??? Error using ==> mpower Matrix must be square.
B=x*y
Erorr:
??? Error using ==> mtimes Inner matrix dimensions must agree.
Waveform
representation
2D Plotting
creates linear continuous plots of
vectors and matrices;
plot(t,y): plots the vector t on the x-axis
versus vector y on the y-axis.
To label your axes and give the plot a title,
type
xlabel('time (sec)')
ylabel('step response')
title('My Plot')
Change scaling of the axes by using the
axis command after the plotting
command:axis([xmin xmax ymin ymax]);
plot:
2D Plotting
t=-2*pi:0.1:2*pi;
y=1.5*sin(t);
plot(t,y);
xlabel('------> time')
ylabel('------> sin(t)')
Matlab Plot
t=-2*pi:0.5:2*pi;
y=1.5*sin(t);
stem(t,y);
xlabel('------> time')
ylabel('------> sin(t)')
Matlab Plot
>>y=sin(x);
0.4
0.2
0
-0.2
-0.4
>>plot(y)
-0.6
-0.8
-1
10
20
30
40
50
60
70
80
90
100
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
-0.1
-0.2
-0.3
10
20
30
40
50
60
70
80
90
100
Display Facilities
0.7
0.6
plot(.)
0.5
0.4
0.3
Example:
>>x=linspace(0,4*pi,100);
>>y=sin(x);
>>plot(y)
>>plot(x,y)
stem(.)
0.2
0.1
0
-0.1
-0.2
-0.3
10
20
30
40
50
60
70
80
90
100
10
20
30
40
50
60
70
80
90
100
0.7
0.6
0.5
0.4
0.3
Example:
>>stem(y)
>>stem(x,y)
0.2
0.1
0
-0.1
-0.2
-0.3
Display Facilities
title(.)
>>title(This is the sinus function)
xlabel(.)
1
0.8
0.6
0.4
ylabel(.)
0.2
sin(x)
>>xlabel(x (secs))
0
-0.2
-0.4
-0.6
>>ylabel(sin(x))
-0.8
-1
10
20
30
40
50
60
x (secs)
70
80
90
100
Flow Control
if
for
while
break
.
Control Structures
Some Dummy Examples
If
Statement Syntax
if ((a>3) & (b==5))
if (Condition_1)
Matlab Commands
elseif (Condition_2)
Matlab Commands
elseif (Condition_3)
Matlab Commands
else
Matlab Commands
end
Control Structures
For
loop syntax
for i=Index_Array
Matlab Commands
end
Control Structures
while (condition)
Matlab Commands
end
Dummy Example
while ((a>3) & (b==5))
Some Matlab Commands;
end
Use of M-File
Click to
create a new
M-File
Extension .m
A text file containing script or function or
program to run
Use of M-File
Matlab Plot
Loops, flows:
for m=1:10:100
num = 1/(m + 1)
end
Practice
1>Draw a straight line satisfying the equation: y = 3*x + 10
2> Create matrices with zeros, eye and ones
3>Draw a circle with radius unity. (use cos for x axis, sin for y
axis)
4>Draw a cosine wave with frequency 10kHz. Use both plot and
stem functions to see the difference.
5>Multiply two matrices of sizes 3x3.
6>Plot the parametric curve x(t) = t, y(t) = exp(-t/2) for 0<t<pi/2
using ezplot
7>Plot a cardioid r() = 1 + cos() for 0< <pi/2
BEIT, 6th Semester
Graphics - Annotation
Use title, xlabel, ylabel and legend for
annotation
Graphics-Stem()
stem()is to plot discrete sequence data
The usage of stem() is very similar to plot()
cos(n/4)
>>
>>
>>
>>
n=-10:10;
f=stem(n,cos(n*pi/4))
title('cos(n\pi/4)')
xlabel('n')
0.5
-0.5
-1
-10
-5
0
n
10
subplots
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
x=0:0.1:10;
f=figure;
f1=subplot(1,2,1);
plot(x,cos(x),'r');
grid on;
title('Cosine')
f2=subplot(1,2,2);
plot(x,sin(x),'d');
grid on;
title('Sine');
0.8
0.8
0.6
0.6
0.4
0.4
0.2
0.2
-0.2
-0.2
-0.4
-0.4
-0.6
-0.6
-0.8
-0.8
-1
Sine
10
-1
10
Save plots
>>
>>
>>
>>
>>
>>
>>
f=figure;
x=-5:0.1:5;
h=plot(x,cos(2*x+pi/3));
title('Figure 1');
xlabel('x');
saveas(h,'figure1.fig')
saveas(h,'figure1.eps')
Practice Problems
x(t ) sin(3t )
5 t 5
y (t ) e 2t 3
0t 5
Plot the following signals, use log scale for y-axis
Plot thex(real
t ) part
e t 2 (2and
t 1)imaginary
0 t 10part of the following signal
.5 t j ( t / 3 )
(t ) e 0in
t 10 plot its phase and magnitude
For thexsignal
previous 0question,
prompt
continue statement on next line
separate statements and data
start comment which ends at end of
(1) suppress output
(2) used as a row separator in a matrix
specify range
MATLAB Help
Type helpwin on
command line
Look under the help menu
on the desktop