03-04-2012, 11:04 AM
matlab
basic matlab.pdf (Size: 46 KB / Downloads: 113)
Some hints:
² after the start of Windows, run Matlab from Q:\matlab6p1 by double-clicking on its icon. Ignore the error messages.
² in Matlab, commands can be entered only in case the cursor is on the command line: >>
² run any text editor in another window | you may use also built-in editor of Matlab, that can make Matlab code
more beautiful (colors, etc.). In this editor, open a text ¯le, where you'll write everything (commands, results, your
comments). The commands can be copied to Matlab using standard copy-paste (Ctrl-c Ctrl-v) sequence. After the
end of the lab, you'll have an exact \log" of what you did and what (hopefully) worked.
² In case the Matlab line contains %, everything after this character is considered as a comment and is not interpreted.
² in Matlab, you can return to previous commands using the arrow key " . The return can be also \selective", for
example, you can return to already typed plot-commands by: pl " " " . . .
2 Basic functionalities of Matlab
1. Variables:
² scalar: a=1;
² column vector: vecsl=[1; 2; 3]
² row vector: vecrad=[2.5 16e-3 85]
² matrix: mat=[1 2 3; 4 5 6; 7 8 9]
2. Visualization of variable's contents:
² text: the name of the variable without semicolon+Enter.
² graphical with x-coordinates equal to indices of elements 1,2,3,. . . : plot(vec)
² graphical with both x- and y-coordinates: plot(vecx,vecy)
² size of a variable:
length(vec) { length of a vector
size(mat) { number of rows and columns of a matrix (2-point vector).
3. Help !!!
² help name of function
² lookfor keyword
4. Case sensitivity: in names of variables, Matlab is like C:
myvector 6= Myvector 6= MYVECTOR.
Exercises
1. Fill scalar, vector and matrix variable. Visualize their contents as text and graphically.
2. How do you enter a complex number ? What's the result of visualization of complex numbers ?
3 Basic operations with vectors and matrices
² addition/subtraction/multiplication/division vector or matrix vs. scalar: v+s, v-s, v*s, v/s
² scalar product (inner product) of two vectors: line v*col v
² multiplication of two vectors point-by-point (dot-product): line v.*line v, col v.*col v, mat.*mat
² generation of an arithmetic sequence with step 1: min value:max value
with arbitrary step: min valuetep:max value
1
² transpose of a vector/matrix: vector', matrix
² functions (abs, sin, cos, log,. . . ) work mostly also with vectors and matrices point-by-point.
² zero- and unity-matrices:
zeros(number rows,number columns), ones(number rows,number columns)
² accessing elements of vectors and matrices: vec(1), vec(2:5), mat(3,4), mat(3,, mat(1:5,4)
² ¯nding indices of vector/matrix satisfying a condition: find, for example: n = -10:10; ii = find(n<10);
Exercises
1. Create a 4£5 matrix with all elements equal to 1. Add 3.25 to all elements, then multiply all elements by -1.5. Visualize
the third and the fourth columns of the matrix.
2. Fill a vector with values cos(x) for 0 · x < 2¼. Visualize with correct values on both axis.
3. Find all places where the function cos(x) is negative and change their color in the plot to green.
4 Generation and visualization of signals
Discrete signals can be generated quite easily { a discrete signal is nothing but a vector of values. At ¯rst, we have to
de¯ne the range of the independent variable (most often n). The visualization can be done using function stem. For example,
a discrete unit-pulse can ±[n] can be \programmed" in the following way:
n = -10:10;
delta = zeros(size(n));
delta(find(n==0)) = 1;
stem(n,delta)
Note the use of the condition in the argument | this trick will be used quite often below!.
In theory, continuous-time signals can not be handled on computer. The time-axis contain 1 points. We must help
ourselves by a sampling with a small time-step (the theory of sampling will be covered later). The visualization will be done
using plot, to achieve the impression of continuous time. An example for the rectangular pulse:
x(t) = ½ 2 for ¡ 1 · t · 2
0 elsewhere
t = -10:0.01:10;
s = zeros(size(t));
s(find(t>=-1 & t<=2)) = 2;
plot(t,s)
Exercises
1. Generate and visualize a discrete unit-step ¾[n] for n 2 [¡10; 10].
2. Generate a continuous-time triangular pulse:
x(t) =8<
:
t + 1 for ¡ 1 · t · 0
¡t=3 + 1 for 0 < t · 3
0 elsewhere
5 Transformation of the independent variable { time
The lecture covered time-shifts (delay, advancing), °ipping of the time-axis and contraction and dilatation of time. In Matlab,
these operations can be done in the following way: First, we generate the standard time axis. After, we generate the modi¯ed
time-axis. The signal is generated according to this modi¯ed time-axis. Example for the delayed unit-pulse (discrete-time)
±[n ¡ 1]:
n = -10:10;
nminus1 = n-1;
deltaminus1 = zeros(size(nminus1));
deltaminus1(find(nminus1==0)) = 1;
stem(n,deltaminus1)
Note, that we have to visualize the signal against the original time-axis! A similar example for a delayed rectangular
pulse (continuous time { see above), advanced by 2 seconds: x(t + 2):
2
t = -10:0.01:10;
tplus2 = t+2;
splus2 = zeros(size(tplus2));
splus2(find(tplus2>=-1 & tplus2<=2)) = 2;
plot(t,splus2)
Exercises
Work with discrete-time unit-step ¾[n] and continuous-time triangular pulse from the previous exercises:
1. Generate and visualize the unit-step with the following modi¯cations of the time axis: ¾[n ¡ 2], ¾[n + 2], ¾[¡n ¡ 2],
¾[¡n + 2]. Explain, what you see.
2. Generate and visualize the triangular pulse for the following modi¯cations of the time-axis: s(t¡2), s(t+2), s(¡t¡2),
s(t + 2), s(t=2), s(2t). Explain, what you see.