Student:Sofean Maeouf

advertisement
ECE 351
MATLAB INTRODUCTION
(BY TEACHING ASSISTANTS)
WHY MATLAB?
The name MATLAB (matrix laboratory), is
Developed by Mathworks.
http://www.mathworks.com/products/matlab/index.
html



While the software has progressed well beyond
its original goal as a tool dedicated to performing
matrix computations, it is still based on the
notation of arrays and matrices.
The latest version is R2013b



MATLAB is a high-level language and interactive
environment that enables you to perform
computationally intensive tasks.
High-level language for technical computing
Development environment for managing code,
files, and data



Interactive tools for iterative exploration, design,
and problem solving
Mathematical functions for linear algebra,
statistics, Fourier analysis, filtering, optimization,
and numerical integration
2-D and 3-D graphics functions for visualizing
data
MATRICES, ARRAYS AND VECTORS

A matrix is simply a rectangular list of numbers
The "size" of a matrix is given as two numbers, the
first is traditionally the number of rows in the matrix
while the second is the number of columns in the
matrix.
 Matrices are usually written in tabular form
contained between two large parentheses or square
brackets.



Arrays/vectors: matrices with only ONE row or
column
Example
A 2×3 (two by three)matrix
34 56 31
-45 6 43
A 1×3 row vector
34 56 31
A 2×1 column vector
34
-45
TO START MATLAB
Command window
Single line commands, results
 Editor
Edit scripts
 Workspace
Store variables
 Current Folder
Store files

TO START MATLAB
TO START MATLAB
Command window
Only one line a time
 Create a new .m file
File->New->Script
 Check the usage of ‘;’, ‘%’, ‘%%’
 Save and Run

PRESENT MATRICES IN MATLAB
>> a=[1 2 3; 4 5 6]
Or
>> a=[1, 2, 3; 4, 5, 6]
Try on your own computer if you haven’t done so.
ARITHMETIC OPERATIONS

Scalar operations:
There are four scalar operations
addition: +
 subtraction:  multiplication: *
 division: /

Example
>> a=[1 2 3; 4 5 6]
>> b=3*a
>> b=[3 6 9; 12 15 18]

 Matrix
Addition and Subtraction
For two matrices to be added or
subtracted they must be of the same
size.

The entries are computed by adding or
subtracting the corresponding entries in the
two original matrices.
 Example
>> a=[1 2 3; 4 5 6]
>> b=[2 4 6; 3 5 7]
>> c=a-b
c=[-1 -2 -3; 1 0 -1]


Multiplication and Division
2 different types: componentwise and
conventional
Recall: How to multiply matrices?
 2 1 1 0



 3 0  4 2

Normal multiplication:
A, B, C is n by n
 A   B    C 
n
c jk   a ji bik
i 1

In MATLAB
>> C=A*B

Componentwise multiplication:
Same problem as before,
c jk  a jk b jk

In MATLAB
>> C=A.*B
FUNCTIONS AND SHORTCUTS
Functions: operations that can be called in a
scripts
 Zeros()
 Ones()
 Eye()
 Diag()
 Linspace()
…

Zeros(n)
to create an n by n matrix with all entries zero.
Zeros(n, m) create an n by m one.
 Ones(n)
to create an n by n matrix with all entries 1. Used
as the same fashion as zeros(n)
 Eye(n)
to create an n by n identity matrix.
 Diag([])
to create a diagonal matrix. Vector given shows
as diagonal elements.

Two shortcuts for row vectors:
1. vector=a:n:b
the vector starts with a and end with b, n is the
step size
Example
>> t=1:0.5:3
t=1 1.5 2 2.5 3

If n is not given, default value is 1
>> t=1:3
t=1 2 3
2 linspace(a, b, n)
to create a row matrix with n elements, start
from a and end with b, with equal step size.
Example:
>> t=linspace(0,10,11)
t=0 1 2 3 4 5 6 7 8 9 10
size()
 inv()
 []’
 fft()

Functions can be self defined.
 Most functions work componentwise

SELF-DEFINED FUNCTION
File->New->Function
 Input arguments
 Output arguments
 Function name
 How to call function

HOW TO PLOT FIGURES?
Plot(x,y):
2 vector input, x will be horizontal axis and y be
the vertical one.
x, y must be the same size
s: applicable parameters:
color, plot symbol, line type
used as character strings.
t=0:0.1:5
 y=sin(2*pi*t)
 Plot(t,y)

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0
0.5
1
1.5
2
2.5
3
3.5
4
4.5
5
t=0:0.001:5
 y=sin(2*pi*t)
 Plot(t,y)

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0
0.5
1
1.5
2
2.5
3
3.5
4
4.5
5

plot(t,y,'--')
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0
0.5
1
1.5
2
2.5
3
3.5
4
4.5
5

plot(t,y,'g')
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0
0.5
1
1.5
2
2.5
3
3.5
4
4.5
5
Legend, title and label
legend(‘strings1’,’strings2’…..’location’,’orientation’)
e.g legend('sin function')
 Title
Title(‘text’)
e.g title('function')
 Label
Xlabel(‘text’)
e.g xlabel('time')
Similar: ylabel, zlabel

function
1
sin function
0.8
0.6
Amplitude
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0
0.5
1
1.5
2
2.5
time
3
3.5
4
4.5
5
Example 1
 t=0:0.001:5
 x=cos(2*pi*t)
 y=sin(2*pi*t)
 plot(t,x,'g')
 hold on
 plot(t,y,'r')
1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0
0.5
1
1.5
2
2.5
3
3.5
4
4.5
5
Example 2
 t=0:0.001:5
 x=cos(2*pi*t)
 y=sin(2*pi*t)
 subplot(2,1,1)
 plot(t,x,'g')
 subplot(2,1,2)
 plot(t,y,'r')
1
0.5
0
-0.5
-1
0
0.5
1
1.5
2
2.5
3
3.5
4
4.5
5
0
0.5
1
1.5
2
2.5
3
3.5
4
4.5
5
1
0.5
0
-0.5
-1
HW1




















t = (-2:0.01:2)/1000;
a1 = 500;
x1 = 20 * sin(2*pi*1000*t - pi/3) .* exp(-a1*t);
a2 = 750;
x2 = 20 * sin(2*pi*1000*t - pi/3) .* exp(-a2*t);
a3 = 1000;
x3 = 20 * sin(2*pi*1000*t - pi/3) .* exp(-a3*t);
%Plot Resutls
figure(1);
clf;
plot(t,x1,'b');
hold on
plot(t,x2,'k:');
plot(t,x3,'r--');
hold off
xlabel('time (sec)')
ylabel('Amplitude')
title('Exponentially Damped Sinusoid')
axis([-2/1000 2/1000 -120 120])
legend('a = 500','a = 750','a = 1000')
HW1
Exponentially Damped Sinusoid
a = 500
a = 750
a = 1000
100
Amplitude
50
0
-50
-100
-2
-1.5
-1
-0.5
0
time (sec)
0.5
1
1.5
2
-3
x 10
2. semilogy(x, y, s)
logarithmic (base 10) scale is used for the Y-axis.
Used as the same fashion as plot.

Similar: semilogx, loglog.
Example:
BASIC PROGRAMMING SYNTAX

FOR
‘for’ is used for repeating statements
Example:
t=0;
for i=1:5
t=t+i;
end
Also see WHILE

IF
If Conditionally execute statements.
Example
t=0;
for i=1:5
if i>3
t=t+i;
end
end
QUESTIONOS
Download