My S & S Manual
My S & S Manual
My S & S Manual
SUBMITTED BY
OSAMA TAHIR 09-EE-88
BASIC TERMS:
>> help It will display list of all toolboxes included in MATLAB. >> help functionname This command displays a description of the function and generally also includes a list of related functions >> lookfor keyword This command will display a list of functions that include the keyword in their descriptions. Other help commands that you may find useful are info, what, and which
MATLAB variables
MATLAB stores variables in the form of matrices, which are M x N, where M is the number of rows and N the number of columns.. A matrix is written with a square bracket [ ] with spaces separating adjacent columns and semicolons separating adjacent rows. For example, consider the following assignments of the variable x Real scalar >> x = 5 Complex scalar >> x = 5+10j (or >> x = 5+10i) Row vector >> x = [1 2 3] (or x = [1, 2, 3]) Column vector >> x = [1; 2; 3] 3 x 3matrix >> x = [1 2 3; 4 5 6; 7 8 9]
09-EE-88
Generating Vectors
Vectors can be generated using the : command. For example, to generate a vector x that takes on the values 0 to 10 in increments of 0.5, type the following, which generates a 1 x 21 matrix? >> x = [0:0.5:10];
Matrix Operations
Addition and subtraction involve element-by-element arithmetic operations; matrix multiplication and division do not. However, MATLAB provides for element-by-element operations as well by prep ending a . before the operator as follows:
.* ./ .\ .^ .
The difference between matrix multiplication and element-by-element multiplication is seen in the following example >>A = [1 2; 3 4] A= 12 34 >>B=A*A B= 7 10 15 22 >>C=A.*A C= 14 9 16
09-EE-88
Relational Operations
The following relational operations are defined: < <= > >= == ~= Less than Less than or equal to Greater than Greater than or equal to Equal to Not equal to
09-EE-88
SUBMITTED BY
OSAMA TAHIR 09-EE-88
SIZE
We can get the size (dimensions) of a matrix with the command size >> size (A), ans = 23
Transpose
Transposing a vector changes it from a row to a column vector and vice versa. >> D, D D= 12345 6 7 8 9 10 11 13 15 17 19 ans = 1 6 11 2 7 13 3 8 15 4 9 17 5 10 19 >> size (D), size(D) ans = 35 ans = 53
Special Matrices
MATLAB provides a number of useful builtin matrices of any desired size. Ones (m, n) give an mxn matrix of 1s, >> P =ones (2, 3)
09-EE-88
MatrixMatrix Products
To form the product of an m n matrix A and a np matrix B, written as AB, we visualize the first matrix (A) as being composed of m row vectors of length n stacked on top of each other while the second (B) is visualized as being made up of p column vectors of length n: (m n) times (n p) = (m p). Check that you understand what is meant by working out the following examples by hand and comparing with the MATLAB answers. >> A = [5 7 9; 1 -3 -7] A= 579 1 -3 -7 >> B = [0, 1; 3, -2; 4, 2] B= 01 3 -2 42 >> C = A * B C= 57 9 -37 -7
09-EE-88
>> D = B * A D= 1 -3 -7 13 27 41 22 22 22
Flip matrices left-right Flip matrices up-down Replicate and tile an array Reshape array
using "whos" displays the following: >> whos Name Size Bytes Class Attributes A 4-D 1920000 double B 5-D 3000000 single complex
09-EE-88
SUBMITTED BY
OSAMA TAHIR 09-EE-88
How to plot???
Suppose we wish to plot a graph of y = sin3x for 0 x 1. We do this by sampling the function at a sufficiently large number of points and then joining up the points (x, y) by straight lines. Suppose we take N+1 points equally spaced a distance h apart: >> N =10; h =1/N; t = 0: h: 1; defines the set of points x = 0, h, 2h, . . ., 9h, 1. The corresponding y values are computed by >> y =sin(3*pi*t); and finally, we can plot the points with >> plot (t, y) The result is shown in Figure 1, where it is clear that the value of N is too small.
1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
Grids:
A dotted grid may be added by >> grid
09-EE-88
Multiplots
Several graphs can be drawn on the same figure as: >> plot (t, y, w-,t, cos (2*pi*t), g--) legend A descriptive legend may be included with >> legend (Sin curve, Cos curve) EXAMPLE >> plot (t, y, w-, t, cos (2*pi*t), g--) >> legend (Sin curve, Cos curve) >> title (Multi-plot ) >> xlabel (x axis(time)), ylabel (y axis) >> grid
Multi-plot 1 0.8 0.6 0.4 0.2
y axis
0.1
0.2
0.3
0.4
0.5 x axis
0.6
0.7
0.8
0.9
Hold
A call to plot clears the graphics window before plotting the current graph.. To stop the window being cleared: >> plot (t, y, w-), hold >> plot (t, y, gx), hold off hold on holds the current picture; hold off releases it (but does not clear the window, which 09-EE-88
can be done with clf). hold on its own toggles the hold state.
EXAMPLE
The graphics window may be split into an m n array of smaller windows into which we may plot one or more graphs. The windows are counted 1 to mn rowwise, starting from the top left. Both hold and grid work on the current subplot. >> subplot (221), plot (t,y) >> xlabel (x), ylabel (sin 3 pi t) >> subplot (222), plot (t, cos (3*pi*t)) >> xlabel (x), ylabel (cos 3 pi t) >> subplot (223), plot (t, sin (6*pi*t)) >> xlabel (x), ylabel (sin 6 pi t) >> subplot (224), plot (t, cos (6*pi*t)) >> xlabel (x), ylabel (cos 6 pi t) subplot(221) (or subplot(2,2,1)) specifies that the window should be split into a 2 2 array and we select the first sub-window.
1 0.5 0 -0.5 -1
cos 3 pi x sin 3 pi x
1 0.5 0 -0.5 -1
0.5 x
0.5 x
1 0.5 0 -0.5 -1
cos 6 pi x sin 6 pi x
1 0.5 0 -0.5 -1
0.5 x
0.5 x
09-EE-88
09-EE-88
LoopS
FOR LOOP The for loop allows us to repeat certain commands. If you want to repeat some action in a predetermined way, you can use the for loop. All of the loop structures in matlab are started with a keyword such as "for", or "while" and they all end with the word "end". Another deep thought, eh.
Conditional Statements
Conditional statements enable you to select at run time which block of code to execute.There are times when you want certain parts of your program to be executed only in limited circumstances. The way to do that is to put the code within an "if" statement.
elseif and else are optional, and execute statements only when previous expressions in the if block are false. An if block can include multiple elseif statements.
if (condition statement) (matlab commands) elseif (condition statement) (matlab commands) elseif (condition statement) (matlab commands) . . . else (matlab commands) end
Odd Signal:
A signal is odd if x(t) = -x(-t).
Three possible time transformations: Time Flip (or reverse): x(-t), x[-n]
Flips the signal over the vertical axis. Time Shift: x(t+a), x[n+a]
On horizontal axis, shifts to the right when a<0, shifts to the left when a>0. Time Scale: x(at), x[an] for a>0.
On horizontal axis, scales the signal length down when a>1, scales it up when a<1.
Lab tasks
EXAMPLE :1
Run the following program
nrows = 4; ncols = 4; myData = ones(nrows, ncols); % Loop through the matrix for r = 1:nrows for c = 1:ncols if r == c myData(r,c) elseif abs(r myData(r,c) else myData(r,c) end = 2; c) == 1 = -1; = 0;
09-EE-88
end end
Output
EXAMPLES
function [even,odd]=evenoddfunc(signal,time) rev_sig=fliplr(signal); rev_time=fliplr(time); revtime= -1*rev_time; even=0.5*(signal+rev_sig); odd=0.5*(signal-rev_sig); subplot(5,1,1) stem(time,signal,'r'); title('original'); subplot(5,1,2); stem(revtime,rev_sig); title ('reversal'); subplot(5,1,3); stem(time,odd); title('odd'); subplot(5,1,4); stem(time,even); title('even'); subplot(5,1,5); stem(time,(even+odd)); title('restored signal');
09-EE-88
save it by the name of the function (evenoddfunc) & then open a new m-file and write the code below for to simulate the code and to show output .
inc=0.1; t=0:inc:10; f=0.3; omega=2*pi*f; x=cos(omega*t); [even,odd]=evenoddfunc(x,t);
Output
09-EE-88