4. 01/12/2006 Department of Electrical & Electronics Engineering Getting Started Command-Window Workspace & Directory Command- History
5.
6. 01/12/2006 Department of Electrical & Electronics Engineering Basic MATLAB Commands clear all : clears workspace of all variables close all : closes all the figure windows plot (x,y) : plots vector “y” versus “x” % : used for Comments help : when used with command gives its syntax
7.
8.
9.
10.
11.
12.
13.
14.
15. 01/12/2006 Department of Electrical & Electronics Engineering First, let's create a simple vector with 9 elements called a a = [1 2 3 4 6 4 3 4 5] a = 1 2 3 4 6 4 3 4 5 b = a + 2 b = 3 4 5 6 8 6 5 6 7 Notice how MATLAB requires no special handling of vector or matrix math. Basic matrix operation
16. 01/12/2006 Department of Electrical & Electronics Engineering Creating a matrix Given A and B: Addition Subtraction Product Transpose
17. 01/12/2006 Department of Electrical & Electronics Engineering Operators (Element by Element) .* element-by-element multiplication ./ element-by-element division .^ element-by-element power
18. 01/12/2006 Department of Electrical & Electronics Engineering The Use of “ .” and “Element Operation” A = [1 2 3; 5 1 4; 3 2 1] A = 1 2 3 5 1 4 3 2 -1 x = A(1,:) x = 1 2 3 y = A(3 ,:) y= 3 4 -1 b = x .* y B=3 8 -3 c = x . / y c= 0.33 0.5 -3 d = x .^2 d= 1 4 9 K= x^2 Erorr: ??? Error using ==> mpower Matrix must be square. B=x*y Erorr: ??? Error using ==> mtimes Inner matrix dimensions must agree .
19. 01/12/2006 Department of Electrical & Electronics Engineering eye identity matrix zeros matrix of zeros ones matrix of ones diag create or extract diagonals triu upper triangular part of a matrix tril lower triangular part of a matrix Rand randomly generated matrix hilb Hilbert matrix magic magic square Creating Matrices using in-built functions
20. Basic Task: Plot the function sin(x) between 0≤x≤4π 01/12/2006 Department of Electrical & Electronics Engineering Scalar in-built functions >>x=linspace(0,4*pi,100); >>y=sin(x); >>plot(y)
21. Plot the function e -x/3 sin(x) between 0≤x≤4π 01/12/2006 Department of Electrical & Electronics Engineering Scalar in-built functions Calculate e -x/3 of the x-array >>y1=exp(-x/3); Multiply the arrays y and y1 >>y2=y*y1; Multiply the arrays y and y1 correctly >>y2=y.*y1; >>plot(y2)
22. 01/12/2006 Department of Electrical & Electronics Engineering Matrix in-built functions eig det size length rank Find Eigen values and eigenvectors determinant Size of an array Length of a vector rank fi nd indices of nonzero entries x = 2 * rand(1,5) y = x(find(x > 1)) Suppose we want vector that consists all values of x greater than 1
23. 01/12/2006 Department of Electrical & Electronics Engineering plot (x ,y) plots vector y versus vector x Plotting in Matlab plot(y) plots the columns of y versus their index. plot(x ,y,s ) Where s is a character string made from one element from any or all the following 3 columns: b blue . point - solid g green o circle : dotted r red x x-mark -. dashdot c cyan + plus -- dashed Example : plot(x, y, ‘r+:') plots a red dotted line with a plus at each data point
24. 01/12/2006 Department of Electrical & Electronics Engineering Plotting in Matlab - Examples x = -2.9:0.2:2.9; bar(x, exp(-x.*x)); x = -2.9:0.2:2.9; plot(x, exp(-x.*x));
25. 01/12/2006 Department of Electrical & Electronics Engineering Plotting in Matlab x = -pi:pi/10:pi; y = tan(sin(x)) - sin(tan(x)); plot(x,y,'--rs','LineWidth',2,... 'MarkerEdgeColor','k',... 'MarkerFaceColor','g',... 'MarkerSize',10)
26. 01/12/2006 Department of Electrical & Electronics Engineering x=0:0.25:10; stairs(x, sin(x)); x = 0:0.1:4; y = sin(x.^2).*exp(-x); stem (x,y) Plotting in Matlab - Examples
27. 01/12/2006 Department of Electrical & Electronics Engineering More details of plot Plot (X1,Y1,S1,X2,Y2,S2,X3,Y3,S3,...) combines the plots defined by the (X,Y,S) triples, x = -pi:pi/10:pi; y = tan(sin(x)) - sin(tan(x)); y1=- sin(tan(x)) plot(x,y,'--rs',x,y1,'ob:')
28.
29.
30.
31. 01/12/2006 Department of Electrical & Electronics Engineering While Loop Syntax while (condition) Matlab Commands end Dummy Example while ((a>3) & (b==5)) Some Matlab Commands; end Control structure