0
Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# matab no2

39

Published on

Published in: Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
39
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
1
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. Lab No.02 M-files and Matrix operations Designed by : Dawar Awan dawar@cecos.edu.pk CECOS College of Engineering and IT March &#x2013; July 2012
• 2. Matrices &#xF0A7; MATLAB treats every thing as a matrix (check the Workspace) &#xF0A7; 1-by-1 matrices are interpreted as scalars &#xF0A7; Matrices with only one row or one column are known as vectors &#xF0A7; A matrix is a rectangular array of numbers. 1 9 2 6 7 8 3 5 1 8 2 4 A matrix with 3 rows, 4 columns, and 12 elements CECOS College of Engineering and IT March &#x2013; July 2012
• 3. Defining matrices &gt;&gt; A = [1 2 3; 4 5 6; 7 8 9]; &gt;&gt; A = [1, 2, 3; 4, 5, 6; 7, 8, 9]; and &gt;&gt; A = [ 1 2 3 456 7 8 9 ]; Defines A= 1 3 4 5 6 7 CECOS College of Engineering and IT 2 8 9 March &#x2013; July 2012
• 4. Accessing matrix elements &#xF0A7; The matrix element located in the i-th row and j-th column of &#x201C;A&#x201D; is referred to as, A(i,j) &#xF0A7; Try the following instruction and observe the change in &#x201C;A&#x201D; &gt;&gt; A(2,3) = 10 CECOS College of Engineering and IT March &#x2013; July 2012
• 5. Some Built-in matrix functions Function Description diag eye magic ones rand triu tril zeros size length find : : : : : : : : : : : CECOS College of Engineering and IT returns diagonal elements as vector identity matrix magic square matrix of ones randomly generated matrix upper triangular part of a matrix lower triangular part of a matrix matrix of zeros Number of rows and columns in a matrix Length of an array find indices of some elements in array March &#x2013; July 2012
• 6. Matrix functions : Examples &gt;&gt; a=[1 2 3;4 5 6;7 8 9] a= 1 4 7 2 5 8 3 6 9 &gt;&gt; diag(a) &gt;&gt; triu(a) &gt;&gt; tril(a) &gt;&gt; size(a) &gt;&gt; length(a) ans = ans = ans = ans = ans = 1 5 9 CECOS College of Engineering and IT 1 0 0 2 5 0 3 6 9 1 4 7 0 5 8 0 0 9 3 3 3 March &#x2013; July 2012
• 7. Matrix functions : Examples &gt;&gt; eye(3) &gt;&gt; magic(3) &gt;&gt; rand(3) ans = ans = ans = 1 0 0 0 1 0 0 0 1 8 3 4 1 5 9 6 7 2 0.8147 0.9134 0.2785 0.9058 0.6324 0.5469 0.1270 0.0975 0.9575 &gt;&gt; ones(3) &gt;&gt; ones(3,2) &gt;&gt; zeros(3) &gt;&gt; zeros(3,2) ans = ans = ans = ans = 1 1 1 1 1 1 1 1 1 CECOS College of Engineering and IT 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 March &#x2013; July 2012
• 8. Matrix functions : Examples &gt;&gt; rand(3,2) ans = 0.9649 0.9572 0.1576 0.4854 0.9706 0.8003 CECOS College of Engineering and IT March &#x2013; July 2012
• 9. Matrix operations + * ^ ' / =&gt; =&gt; =&gt; =&gt; =&gt; =&gt; Addition Subtraction Multiplication Power Conjugate transpose Division &#xF0A7; To perform index by index operation, use dot (.) notation ./ .* .^ .' =&gt; =&gt; =&gt; =&gt; CECOS College of Engineering and IT Index by index division Index by index multiplication Index by index power Non conjugate transpose March &#x2013; July 2012
• 10. Dot notation &#xF0A7; Observe the change in result due to the &#x201C;dot&#x201D; CECOS College of Engineering and IT March &#x2013; July 2012
• 11. Examples/Exercise 1 4 7 C= B= A= 2 5 8 3 6 9 1 2 3 1 2 3 1 2 3 1 D= 2 1 2 2 1 2 1 &#xF0A7; Practice the following matrix operations A+B, A&#x2019;, A*B, 2*A, A/2, A/B, A./B, C/D, C./D, C*D, C.*D, C^2, C.^2 CECOS College of Engineering and IT March &#x2013; July 2012
• 12. Colon notation CECOS College of Engineering and IT March &#x2013; July 2012
• 13. M - files &#xF0A7; M-files are script/text files containing MATLAB commands Script M-file Function M-file &#xF0A7; To make a script M-file, you need to open a file using the built-in MATLAB editor. &#xF0A7; There are two ways to accomplish it: 1. From file menu, click NEW 2. Type edit on command line CECOS College of Engineering and IT March &#x2013; July 2012
• 14. M - files &#xF0A7; To save the M-file go to the &#x201C;file&#x201D; menu and click on &#x201C;save&#x201D;. &#xF0A7; The name should not contain spaces, should not start with a number and should not be same as any built-in function. &#xF0A7; The extension of this file should be &#x201C;.m&#x201D; CECOS College of Engineering and IT March &#x2013; July 2012
• 15. M - files &#xF0A7; This code can now be executed in two ways 1. Write the name of file on command window (excluding &#x201C;.m&#x201D;) 2. Under the &#x201C;debug&#x201D; menu, click &#x201C;Run&#x201D; &#xF0A7; The variables used in the M-file will be maintained in the workspace and hence accessible from the command window. CECOS College of Engineering and IT March &#x2013; July 2012
• 16. M - files &#xF0A7; Code written in M-file to calculate various parameters of a circle &#xF0A7; Check the values of the variables after running the M-file CECOS College of Engineering and IT March &#x2013; July 2012
• 17. Comments &#xF0A7; While writing any code, it&#x2019;s a good practice to write proper comments against each instruction. &#xF0A7; In MATLAB, any thing written after &#x201C;%&#x201D; is treated as a comment and is ignored by the compiler. CECOS College of Engineering and IT March &#x2013; July 2012
• 18. Function M-files &#xF0A7; User defined functions &#xF0A7; These M-files start with a keyword, &#x201C;function&#x201D; &#xF0A7; The first line of these files should look like function[output variables]=functionName(input variables) &#xF0A7; These files are saved with the funtionName.m as file name. &#xF0A7; An example: CECOS College of Engineering and IT March &#x2013; July 2012
• 19. Function M-files &#xF0A7; Calling this function from command window CECOS College of Engineering and IT March &#x2013; July 2012
• 20. Function M-files &#xF0A7; Any comments after the first line becomes the help for that function &#xF0A7; Command window CECOS College of Engineering and IT March &#x2013; July 2012
• 21. Tasks 1- Generate a vector of 50 elements having random values between 0 and 50. 2- Generate a vector, containing all the odd numbers between 0 and 100. 3- Create a function M-file that accepts two numbers and generates there sum, product and difference as output. 4- Use Cramer's rule to solve the following set of equations. 3x1 - 1x2 + 0x3 = 1 1x1 + 4x2 - 2x3 = 5 0x1 - 2x2 + 8x3 = 6 CECOS College of Engineering and IT March &#x2013; July 2012