🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Transcript

Experiment No. (1) Introduction to MATLAB Experiment No. ( 1 ) Introduction to MATLAB 1-1 Introduction MATLAB is a high-performance language for technical computing. It inte...

Experiment No. (1) Introduction to MATLAB Experiment No. ( 1 ) Introduction to MATLAB 1-1 Introduction MATLAB is a high-performance language for technical computing. It integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation. The name MATLAB stands for matrix laboratory. MATLAB was originally written to provide easy access to matrix. 1-1-1 Starting and Quitting MATLAB  To start MATLAB, double-click the MATLAB shortcut icon on your Windows desktop. You will know MALTAB is running when you see the special " >> " prompt in the MATLAB Command Window.  To end your MATLAB session, select Exit MATLAB from the File menu in the desktop, or type quit (or exit) in the Command Window, or with easy way by click on close button in control box. 1-1-2 Desktop Tools 1- Command Window: Use the Command Window to enter variables and run functions and M-files. 2- Command History: Statements you enter in the Command Window are logged in the Command History. In the Command History, you can view previously run statements, and copy and execute selected statements. 3- Current Directory Browser: MATLAB file operations use the current directory reference point. Any file you want to run must be in the current directory or on the search path. 4- Workspace: The MATLAB workspace consists of the set of variables (namedarrays) built up during a MATLAB session and stored in memory. 1 Experiment No. (1) Introduction to MATLAB Current Directory Browser Command Window Workspace Command History 5- Editor/Debugger Window: Use the Editor/Debugger to create and debug M-files. 2 Experiment No. (1) Introduction to MATLAB 1-2 Basic Commands  clear Command: Removes all variables from workspace.  clc Command: Clears the Command window and homes the cursor.  help Command: help displays help about that Topic if it exist.  lookfor Command: Provides help by searching through all the first lines of MATLAB help topics and returning those that contains a key word you specify.  edit Command: enable you to edit (open) any M-file in Editor Window. This command doesn’t open built-in function like, sqrt. See also type Command.  more command: more on enables paging of the output in the MATLAB command window, and more off disables paging of the output in the MATLAB command window. Notes:  A semicolon " ; " at the end of a MATLAB statement suppresses printing of results.  If a statement does not fit on one line, use "... ", followed by Enter to indicate that the statement continues on the next line. For example: >> S= sqrt (225)*30 /... (20*sqrt (100)  If we don’t specify an output variable, MATLAB uses the variable ans (short for answer), to store the last results of a calculation.  Use Up arrow and Down arrow to edit previous commands you entered in Command Window.  Insert " % " before the statement that you want to use it as comment; the statement will appear in green color. 3 Experiment No. (1) Introduction to MATLAB Now Try to do the following: >> a=3 >> a=3; can you see the effect of semicolon " ; " >> a+5 assign the sum of a and 5 to ans >> b=a+5 assign the sum of a and 5 to b >> clear a >> a can you see the effect of clear command >> clc clean the screen >> b Exercises 1- Use edit command to edit the dct function, then try to edit sin function. State the difference. 2- Use help command to get help about rand function. 3- Enter a=3; b=5; c=7, then clear the variable b only 4 Experiment No. ( 2 ) Working with Matrices 2-1 Entering Matrix The best way for you to get started with MATLAB is to learn how to handle matrices. You only have to follow a few basic conventions:  Separate the elements of a row with blanks or commas.  Use a semicolon ( ; ) to indicate the end of each row.  Surround the entire list of elements with square brackets, [ ]. For Example >> A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1] MATLAB displays the matrix you just entered. A = 16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1 Once you have entered the matrix, it is automatically remembered in the MATLAB workspace. You can refer to it simply as A. Also you can enter and change the values of matrix elements by using workspace window. 2-2 Subscripts The element in row i and column j of A is denoted by A(i,j). For example, A(4,2) is the number in the fourth row and second column. For the above matrix, A(4,2) is 15. So to compute the sum of the elements in the fourth column of A, type >> A(1,4) + A(2,4) + A(3,4) + A(4,4) ans = 34 5 Experiment No. (2) Working with Matrices You can do the above summation, in simple way by using sum command. If you try to use the value of an element outside of the matrix, it is an error. >> t = A(4,5) ??? Index exceeds matrix dimensions. On the other hand, if you store a value in an element outside of the matrix, the size increases to accommodate the newcomer. The initial values of other new elements are zeros. >> X = A; >> X(4,5) = 17 X = 16 3 2 13 0 5 10 11 8 0 9 6 7 12 0 4 15 14 1 17 2-3 Colon Operator The colon " : " is one of the most important MATLAB operators. It occurs in several different forms. The expression >> 1:10 is a row vector containing the integers from 1 to 10 1 2 3 4 5 6 7 8 9 10 To obtain nonunit spacing, specify an increment. For example, >> 100:-7:50 100 93 86 79 72 65 58 51 Subscript expressions involving colons refer to portions of a matrix. >>A(1:k,j) is the first k elements of the jth column of A. 6 Experiment No. (2) Working with Matrices The colon by itself refers to all the elements in a row or column of a matrix and the keyword end refers to the last row or column. So >> A(4,:) or >> A(4,1:end) give the same action ans = 4 15 14 1 >> A(2,end) ans = 8 2-4 Basic Matrix Functions Command Description sum(x) The sum of the elements of x. For matrices, >> x=[1 2 3 sum(x) is a row vector with the sum over each 4 5 6]; column. >> sum(x) ans = 5 7 9 >> sum(x,2) sum (x,dim) sums along the dimension dim. ans= 6 15 >>sum(sum(x)) In order to find the sum of elements that are ans = stored in matrix with n dimensions, you must 21 use sum command n times in cascade form, this is also applicable for max, min, prod, mean, median commands. 7 Experiment No. (2) Working with Matrices Command Description mean(x) The average of the elements of x. For x=[1 2 3; 4 5 6]; matrices, mean(x) is a row vector with the >> mean(x) average over each column. ans = mean (x,dim) averages along the dimension 2.5 3.5 4.5 dim. >> mean(x,2) ans = 2 5 >>mean(mean(x)) ans = 3.5000 zeros(N) Produce N by N matrix of zeros. zeros(N,M) Produce N by M matrix of zeros. >> zeros(2,3) ans = 0 0 0 0 0 0 ones(N) Produce N by N matrix of ones. ones(N,M) Produce N by M matrix of ones. >> ones(2,3) ans = 1 1 1 1 1 1 8 Experiment No. (2) Working with Matrices Command Description size(x) return the size (dimensions) of matrix x. >> x=[1 2 3 4 5 6]; >> size(x) ans = 2 3 length(v) return the length (number of elements) >> v=[1 2 3]; of vector v. >> length(v) ans = 3 numel(x) returns the number of elements in array x. >> v =[55 63 34]; >> numel(v) ans = 3 >> x=[1 2 4 5 7 8 ]; >> numel(x) ans = 6 9 Experiment No. (2) Working with Matrices Command Description single quote ( ' ) Matrix transpose. It flips a matrix about its main diagonal and it turns a row vector into a column vector. >> x=[1 2 3 4 5 6 7 8 9]; >> x' ans = 1 4 7 2 5 8 3 6 9 >> v=[1 2 3]; >> v' ans = 1 2 3 max (x) Find the largest element in a matrix or a vector. >> x=[1 2 3 4 5 6]; >> max (x) ans = 4 5 6 >> max(max(x)) ans = 6 10 Experiment No. (2) Working with Matrices Command Description min (x) Find the smallest element in a matrix or a vector. >> x=[1 2 3 4 5 6]; >> min (x) ans = 1 2 3 >> min(min(x)) ans = 1 magic(N) produce N Magic square. This command produces valid magic squares for all N>0 >> magic(3) except N=2. ans = 8 1 6 3 5 7 4 9 2 inv(x) produce the inverse of matrix x. >> x=[1 4; 5 8]; >> inv(x) ans = -0.6667 0.3333 0.4167 -0.0833 11 Experiment No. (2) Working with Matrices Command Description diag(x) Return the diagonal of matrix x. if x is a vector then this command produce a diagonal matrix >> x=[1 2 3 with diagonal x. 4 5 6 7 8 9]; >> diag(x) ans = 1 5 9 >> v=[1 2 3]; >> diag(v) ans = 1 0 0 0 2 0 0 0 3 prod(x) Product of the elements of x. For matrices, Prod(x) is a row vector with the product over >> x=[1 2 3 each column. 4 5 6]; >> prod(x) ans = 4 10 18 >> prod(prod(x)) ans = 720 12 Experiment No. (2) Working with Matrices Command Description median(x) The median value of the elements of x. For x=[4 6 8 matrices, median (x) is a row vector with the 10 9 1 median value for each column. 8 2 5]; >> median(x) ans = 8 6 5 >> median(x,2) median(x,dim) takes the median along the ans = dimension dim of x. 6 9 5 >> median(median(x)) ans = 6 sort(x,DIM,MODE) Sort in ascending or descending order. >> x = [3 7 5 - For vectors, sort(x) sorts the elements of x 0 4 2]; in ascending order. >> sort(x,1) ans = For matrices, sort(x) sorts each column of 0 4 2 x in ascending order. 3 7 5 >> sort(x,2) ans = 3 5 7 DIM= 1 by default 0 2 4 MODE= 'ascend' by default >> sort(x,2,'descend') ans = 7 5 3 4 2 0 13 Experiment No. (2) Working with Matrices Command Description det(x) Det is the determinant of the square matrix x. >> x=[5 1 8 4 7 3 2 5 6]; >> det(x) ans = 165 tril(x) Extract lower triangular part of matrix x. >> x=[5 1 8 4 7 3 2 5 6]; >> tril(x) ans = 5 0 0 4 7 0 2 5 6 triu(x) Extract upper triangular part of matrix x. >> x=[5 1 8 4 7 3 2 5 6]; >> triu(x) ans = 5 1 8 0 7 3 0 0 6 14 Experiment No. (2) Working with Matrices Note When we are taken away from the world of linear algebra, matrices become two-dimensional numeric arrays. Arithmetic operations on arrays are done element-by- element. This means that addition and subtraction are the same for arrays and matrices, but that multiplicative operations are different. MATLAB uses a dot (. ),or decimal point, as part of the notation for multiplicative array operations. Example: Find the factorial of 5 >> x=2:5; >> prod(x) Example: if x = [1,5,7,9,13,20,6,7,8], then a) replace the first five elements of vector x with its maximum value. b) reshape this vector into a 3 x 3 matrix. solution a) >> x(1:5)=max(x) b) >> y(1,:)=x(1:3); >> y(2,:)=x(4:6); >> y(3,:)=x(7:9); >> y Example: Generate the following row vector b=[1, 2, 3, 4, 5,............................ 9,10], then transpose it to column vector. solution >> b=1:10 b = 1 2 3 4 5 6 7 8 9 10 >> b=b'; 15 Experiment No. (2) Working with Matrices Exercises 1- If x=[1 4; 8 3], find : a) the inverse matrix of x. b) the diagonal of x. c) the sum of each column and the sum of whole matrix x. d) the transpose of x. 2- If x= [2 8 5; 9 7 1], b=[2 4 5] find: a) find the maximum and minimum of x. b) find median value over each row of x. c) add the vector b as a third row to x. 3- If x=[ 2 6 12; 15 6 3; 10 11 1], then a) replace the first row elements of matrix x with its average value. b) reshape this matrix into row vector. 4- Generate a 4 x 4 Identity matrix. 5- Generate the following row vector b=[5, 10, 15, 20......... 95, 100], then find the number of elements in this vector. 16

Tags

matlab programming computer science
Use Quizgecko on...
Browser
Browser