matab no2
TRANSCRIPT
![Page 1: matab no2](https://reader033.vdocument.in/reader033/viewer/2022060202/559c44d91a28abec0b8b46ca/html5/thumbnails/1.jpg)
M-files and Matrix operations
Designed by : Dawar [email protected]
March – July 2012CECOS College of Engineering and IT
Lab No.02
![Page 2: matab no2](https://reader033.vdocument.in/reader033/viewer/2022060202/559c44d91a28abec0b8b46ca/html5/thumbnails/2.jpg)
March – July 2012CECOS College of Engineering and IT
Matrices
MATLAB treats every thing as a matrix (check the Workspace)
1-by-1 matrices are interpreted as scalars
Matrices with only one row or one column are known as vectors
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
![Page 3: matab no2](https://reader033.vdocument.in/reader033/viewer/2022060202/559c44d91a28abec0b8b46ca/html5/thumbnails/3.jpg)
March – July 2012CECOS College of Engineering and IT
Defining matrices
>> A = [1 2 3; 4 5 6; 7 8 9];
>> A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
and
>> A = [ 1 2 3
4 5 6
7 8 9 ];
Defines A =
1 2 3
4 5 6
7 8 9
![Page 4: matab no2](https://reader033.vdocument.in/reader033/viewer/2022060202/559c44d91a28abec0b8b46ca/html5/thumbnails/4.jpg)
March – July 2012CECOS College of Engineering and IT
Accessing matrix elements
The matrix element located in the i-th row and j-th column of
“A” is referred to as, A(i,j)
Try the following instruction and observe the change in “A”
>> A(2,3) = 10
![Page 5: matab no2](https://reader033.vdocument.in/reader033/viewer/2022060202/559c44d91a28abec0b8b46ca/html5/thumbnails/5.jpg)
March – July 2012CECOS College of Engineering and IT
Some Built-in matrix functions
Function Description
diag : returns diagonal elements as vectoreye : identity matrixmagic : magic squareones : matrix of onesrand : randomly generated matrixtriu : upper triangular part of a matrixtril : lower triangular part of a matrixzeros : matrix of zerossize : Number of rows and columns in a matrixlength : Length of an array find : find indices of some elements in array
![Page 6: matab no2](https://reader033.vdocument.in/reader033/viewer/2022060202/559c44d91a28abec0b8b46ca/html5/thumbnails/6.jpg)
March – July 2012CECOS College of Engineering and IT
Matrix functions : Examples>> a=[1 2 3;4 5 6;7 8 9]
a =
1 2 34 5 67 8 9
>> triu(a)
ans =
1 2 30 5 60 0 9
>> tril(a)
ans =
1 0 04 5 07 8 9
>> size(a)
ans =
3 3
>> length(a)
ans =
3
>> diag(a)
ans =
159
![Page 7: matab no2](https://reader033.vdocument.in/reader033/viewer/2022060202/559c44d91a28abec0b8b46ca/html5/thumbnails/7.jpg)
March – July 2012CECOS College of Engineering and IT
Matrix functions : Examples
>> eye(3)
ans =
1 0 00 1 00 0 1
>> magic(3)
ans =
8 1 63 5 74 9 2
>> ones(3)
ans =
1 1 11 1 11 1 1
>> ones(3,2)
ans =
1 11 11 1
>> rand(3)
ans =
0.8147 0.9134 0.27850.9058 0.6324 0.54690.1270 0.0975 0.9575
>> zeros(3)
ans =
0 0 00 0 00 0 0
>> zeros(3,2)
ans =
0 00 00 0
![Page 8: matab no2](https://reader033.vdocument.in/reader033/viewer/2022060202/559c44d91a28abec0b8b46ca/html5/thumbnails/8.jpg)
March – July 2012CECOS College of Engineering and IT
Matrix functions : Examples
>> rand(3,2)
ans =
0.9649 0.95720.1576 0.48540.9706 0.8003
![Page 9: matab no2](https://reader033.vdocument.in/reader033/viewer/2022060202/559c44d91a28abec0b8b46ca/html5/thumbnails/9.jpg)
March – July 2012CECOS College of Engineering and IT
Matrix operations
+ => Addition - => Subtraction * => Multiplication ^ => Power' => Conjugate transpose/ => Division
To perform index by index operation, use dot (.) notation
./ => Index by index division
.* => Index by index multiplication
.^ => Index by index power
. ' => Non conjugate transpose
![Page 10: matab no2](https://reader033.vdocument.in/reader033/viewer/2022060202/559c44d91a28abec0b8b46ca/html5/thumbnails/10.jpg)
March – July 2012CECOS College of Engineering and IT
Dot notation
Observe the change in result due to the “dot”
![Page 11: matab no2](https://reader033.vdocument.in/reader033/viewer/2022060202/559c44d91a28abec0b8b46ca/html5/thumbnails/11.jpg)
March – July 2012CECOS College of Engineering and IT
Examples/Exercise
A+B, A’, A*B, 2*A, A/2, A/B, A./B, C/D, C./D, C*D, C.*D, C^2, C.^2
A =
1 2 34 5 67 8 9
B =
1 1 12 2 23 3 3
C =
1 2 1 2
D =
2 1 2 1
Practice the following matrix operations
![Page 12: matab no2](https://reader033.vdocument.in/reader033/viewer/2022060202/559c44d91a28abec0b8b46ca/html5/thumbnails/12.jpg)
March – July 2012CECOS College of Engineering and IT
Colon notation
![Page 13: matab no2](https://reader033.vdocument.in/reader033/viewer/2022060202/559c44d91a28abec0b8b46ca/html5/thumbnails/13.jpg)
March – July 2012CECOS College of Engineering and IT
M - files
M-files are script/text files containing MATLAB commands
Script M-file
Function M-file
To make a script M-file, you need to open a file using the built-in
MATLAB editor.
There are two ways to accomplish it:
1. From file menu, click NEW
2. Type edit on command line
![Page 14: matab no2](https://reader033.vdocument.in/reader033/viewer/2022060202/559c44d91a28abec0b8b46ca/html5/thumbnails/14.jpg)
March – July 2012CECOS College of Engineering and IT
M - files
To save the M-file go to the “file” menu and click on “save”.
The name should not contain spaces, should not start with a number and should not
be same as any built-in function.
The extension of this file should be “.m”
![Page 15: matab no2](https://reader033.vdocument.in/reader033/viewer/2022060202/559c44d91a28abec0b8b46ca/html5/thumbnails/15.jpg)
March – July 2012CECOS College of Engineering and IT
M - files
This code can now be executed in two ways
1. Write the name of file on command window
(excluding “.m”)
2. Under the “debug” menu, click “Run”
The variables used in the M-file will be maintained in the workspace
and hence accessible from the command window.
![Page 16: matab no2](https://reader033.vdocument.in/reader033/viewer/2022060202/559c44d91a28abec0b8b46ca/html5/thumbnails/16.jpg)
March – July 2012CECOS College of Engineering and IT
M - files
Code written in M-file to calculate various parameters of a circle
Check the values of the variables after running the M-file
![Page 17: matab no2](https://reader033.vdocument.in/reader033/viewer/2022060202/559c44d91a28abec0b8b46ca/html5/thumbnails/17.jpg)
March – July 2012CECOS College of Engineering and IT
Comments
While writing any code, it’s a good practice to write proper comments
against each instruction.
In MATLAB, any thing written after “%” is treated as a comment and is
ignored by the compiler.
![Page 18: matab no2](https://reader033.vdocument.in/reader033/viewer/2022060202/559c44d91a28abec0b8b46ca/html5/thumbnails/18.jpg)
March – July 2012CECOS College of Engineering and IT
Function M-files
User defined functions
These M-files start with a keyword, “function”
The first line of these files should look like
function[output variables]=functionName(input variables)
These files are saved with the funtionName.m as file name.
An example:
![Page 19: matab no2](https://reader033.vdocument.in/reader033/viewer/2022060202/559c44d91a28abec0b8b46ca/html5/thumbnails/19.jpg)
March – July 2012CECOS College of Engineering and IT
Function M-files
Calling this function from command window
![Page 20: matab no2](https://reader033.vdocument.in/reader033/viewer/2022060202/559c44d91a28abec0b8b46ca/html5/thumbnails/20.jpg)
March – July 2012CECOS College of Engineering and IT
Function M-files
Any comments after the first line becomes the help for that function
Command window
![Page 21: matab no2](https://reader033.vdocument.in/reader033/viewer/2022060202/559c44d91a28abec0b8b46ca/html5/thumbnails/21.jpg)
March – July 2012CECOS College of Engineering and IT
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 = 11x1 + 4x2 - 2x3 = 50x1 - 2x2 + 8x3 = 6