Ramlal@ CC - Iitd.ac - In, MATLAB WORKSHOP, CSC, IITD

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

MATLAB Workshop

Date: 17th Feb, 2010

NOTES:

Special Symbols
[ ] Array constructors
( ) Forms subscript
; 1. Suppress echoing in command window
2. Separates matrix rows
3. Separates assignment statements on a line
‘ Transpose operator
.^ Array exponential
% Marks the beginning of a comment
: Colon operator, used to create shorthand lists
+ Matrix addition
- Mmatrix subtraction
* Matrix Multiplication
.* Array Multiplication

Useful tips for a matrix operations

•A(:,n) refers to the elements in all rows of column n of the matrix A.


•A(n,:) refers to the elements in all the columns of row n of the matrix A.
•A(:,m:n) refers to the elements in all the rows between columns m and n of the
matrix A.
•A(m:n,:) refers to the elements in all the columns between rows m and n of the
matrix A.
•A(m:n, p:q) refers to the elements in rows m through n and columns p through q of
the matrix A.

General commands

Workspace information
who lists variable currently in the workspace
whos lists variables currently in the workspace with their size
clear x y z clears only variable x, y, and z
clear all clears all variable and functions from workspace
clc clears command window, command history is lost
home same as clc
what lists m, mes files on the disk
clf Figure windows can be cleared

Directory information
pwd shows the current working directory
cd changes the current working directory
dir list the contents of the current directory
ls lists contents of the current directory

General Information
computer tells you the computer-type you are using

ver gives the license and the version information about MATLAB installed on
your computer
date tells you the date as a string

ramlal@ cc.iitd.ac.in, MATLAB WORKSHOP, CSC, IITD 1


Termination
^c(control c) local abort, kills the current execution
quit quit MATLAB
exit same as quit

Help List topic on which help is available


Lookfor string Lists help topics containing string
help topic Provides help on topic
demo Run the demo program

fix Round toward zero


floor Round toward negative infinity
ceil Round toward positive infinity
round Round towards nearest integer
rem Remainder after division

HOW TO WRITE PROGRAM IN MATLAB

STEPS DESCRPTION
Take the problem you are trying to solve and break it down
Planning the Program into a series of smaller, independent tasks. Implement each task
as a separate function. Try to keep functions fairly short, each
having a single purpose.
Using Pseudo-Code Write the initial draft of your program in a structured format
using your own natural language. This pseudo-code is often
easier to think through, review, and modify than using a formal
programming language, yet it is easily translated into a
programming language in the next stage of development.
General Coding Practices Use descriptive function and variable names to make your code
easier to understand. Order sub-functions alphabetically in an
M-file to make them easier to find.
Naming a Function Uniquely To avoid choosing a name for a new function that might conflict
with a name already in use, check for any occurrences of the
name using this command:
which -all <function-name>
Add comments generously, explaining each major section and
The Importance of Comments any smaller segments of code that are not obvious.
%---------------------------------------------------------------
% This function computes the ... <and so on>
%-------------------------------------------------
Don't try to write the entire program all at once. Write a
Coding in Steps portion of it, and then test that piece out

When making modifications to a working program, don't make


Making Modifications in Steps widespread changes all at one time

Testing the Final Program Use different combinations of inputs until you have observed
that every line of code is executed at least once.

ramlal@ cc.iitd.ac.in, MATLAB WORKSHOP, CSC, IITD 2


Computer Services Centre, IIT, Delhi.
MATLAB Workshop

Ex 1: Use MATLAB to evaluate the following expressions:


Create a A 3x3 matrix and Do the following operations
(i) Operate the following commands for handling on the above Matrix A.
length(A) ; size(A); reshape(A); diag(A);
(ii) To extract a submatrix B consisting of rows 1 and 3 and columns 1 and 2 of the
matrix A.
(iii) To interchange rows 1 and 3 of A, use the vector of row indices together with the
colon operator

(iv) Using the zeros, ones and eye commands create the following matrix.
a) 2x2 with all elements are 0’s.
b) 4x4 with diagonal elements are 1’s.
c) 3x2 with elements are 1’s.
d) To delete a 3rd row from matrix A

(A)
(i) Compare the results of the following entries: a=[1,2,3], b=[1 2 3], and c=[1;2;3]

(ii) Type cos(pi) and cos(180). Which is the answer that you’d expect?

x2 y3
iii) +5 x=2, y=3 ii) Area = πr 2 , where r = 5 cm
x− y

e2 −1
iv) Solve for x, 3 x = 17 iv)
e2 + 1
(v) Suppose that u=1 and v=3. Evaluate the following expressions using MATLAB
(a)
4u
3v2v -2
(b)
(u + v) 2
4 2
pi v
( c) 3
(d) A trigonometric identity is given by:
x tan x + sin x
cos2 2 =
2 tan x

ramlal@ cc.iitd.ac.in, MATLAB WORKSHOP, CSC, IITD 3


Verify that the identity is correct by calculating each side of the equation, substituting
x= π /5.
(e)
(i) Create a row vector that has the elements: 32,4,81,63, cos( π /10), and 14.24.
(ii) Create a row vector with 15 equally spaced elements in which the first
element is 7 and the last element is 40.
(iii) Create a column vector with 12 equally spaced elements in which the
first element is -1 and the last element is -15.
(iv) Create a ten-dimensional row vector whose all components are equal 2. You
cannot enter number 2 more than once.

(v) Given a row vector a = [1 2 3 4 5]. Create a column vector b that has the same
components as the vector a but they must be stored in the reversed order.

(vi ) With matrix A= 1 2 3 4


5 6 6 7
8 9 10 11
11 12 13 14
Perform the following operations:
• Extract the 3rd column from matrix A and store it in vector B.
• Extract the 1st and 3rd columns from matrix A and store them in
matrix C .
• Add the 1st and 3rd rows from matrix A together and store the
result in vector D.
• Change the value in 2nd row and 3rd column of matrix A to -7 and
Call the result AA.
• Create a matrix that contains rows 1 and 3rd from A, second row of
AA and the result of D.

(f ) Do practice the following functions


round(17/5 ) , fix(13/5), ceil(11/5), floor(-9/4), rem(13, 5) and sign(-5)

(g) Create a matrix of zeros with 5 rows and 7 columns and assign the matrix as
bob. Note the usefulness of the size command by typing size(bob).

ramlal@ cc.iitd.ac.in, MATLAB WORKSHOP, CSC, IITD 4


(h) Type a=bob+5 and then b=bob+3. Try the following operations: a-b, a.^2,
a.*b, and a*b. Why does a*b return an error?

(i) MATLAB can easily join matrices together to make a larger matrix:

A=[3 7 4;5 9 2;4 6 1];

B= [A A+12; A*3 A/2]


(B)
(i) Create a vector t with 10 elements: 1, 2, 3, 4, 5,…10. Now compute the following
quantities:
x = t sin(t); (b ) y = (t-1)/(t+1) ; ( c) z = sin(t2 ) / t2

(ii) Create the 4x4 matrix c and answer the following questions for the array c.
c(2, : ) (b) c(: , end ) (c ) c(1:2, 2:end) ( d) c(6)
(e) c(1:2,2:4) (f ) c([1 3],2) (g) c([2 2],[3 3])
(iii) Create the following matrix A 3 by 5 matrix
Use a the matrix A to :
1. Create a five - element row vector named va that contains the elements of
the second row of A.
2. Create a three - element row vector named vb that contains the elements of
the fourth column of A.
3. Create a ten - element row vector named vc that contains the elements of the
first and second rows of A.
4. Create a six - element row vector named vd that contains the elements of the
second column and fifth columns of A.
(iv) Create the following matrix D 4 by 7 matrix
1. Create a 3 by 4 matrix B from the 1st, 3rd , and 4th rows and 1st , 3rd
through 5th and 7th columns of the matrix D.
2. Create a 15 elements-long row vector u from the elements of the third
row and the 5th and 7th columns of the matrix D.
3. Create a 3x3 matrix A in which all the elements are 1, and create a 2x2
matrix B in which all the elements are 5. Then , add elements to the
matrix A by appending the matrix B such that A will be:

A= 1 1 1 0 0
1 1 1 0 0
1 1 1 0 0
0 0 0 5 5
0 0 0 5 5

ramlal@ cc.iitd.ac.in, MATLAB WORKSHOP, CSC, IITD 5


(V) Create the following matrix A:

1 2 3 4 5 6 7
2 4 6 8 10 12 14
A=
21 18 15 12 9 6 3
5 10 15 20 25 30 35

a) Create a 3 x 4 matrix B from the 1st, 3rd and 4th rows and the 1st, 3rd through 5th and
7th columns pf the matrix A.
b) B) create a 15 elements-log row vector u from the elements of the third row, and 5th
and 7th columns of the matrix A.

(v) Using the Zeros and ones commands, create a 3x5 matrix in which the first, second,
and fifth columns are 0’s and the third and fourth columns are 1’s.

Ex 2: Create a 3x3 matrix and perform the following:


• Rank
• Inverse
Ex 3: [A] Use MATLAB to create the following matrix, row vector and column vector and
determine the dimensions of the matrices using MATLAB command:
⎡1 2 3⎤ ⎡7 ⎤
A = ⎢⎢4 5 6⎥⎥ , B = [4 3 2] , C = ⎢⎢8 ⎥⎥
⎢⎣7 8 9⎥⎦ ⎢⎣6⎥⎦

Ex 4: Type each of the following commands to generate special matrices:


a) eye(4) ; b) zeros(3) ; c) zeros(3,5) ; d) ones(2,3) ; e) diag([4 5 6 7]) ;
f) diag([4 5 6 7], -1) ; g) diag ([4 5 6 7], 2) ; h) diag([4 5 6 7], 3).

Notice the differences between (g) and (h).

Ex 5: Form a diagonal matrix with specified diagonal (or sub, super diagonal) i.e
d=1:5;
main=diag(d) % on the main diagonal
super=diag(d,1) % on the super diagonal
sub=diag(d,-1) % on the sub diagonal

Ex 6: Generate the following matrices using the colon operators:


a) E = [1:5;2:6] ; b) F = [5:-1:1;2:0.5:4]
Ex 7: Form with MATLAB the matrices:
⎡1 2⎤ ⎡ 2 4⎤ ⎡7 2 8⎤
A=⎢ ⎥ , B=⎢ ⎥ , C=⎢ ⎥
⎣3 4⎦ ⎣6 8 ⎦ ⎣1 5 3⎦
Compute the following matrix computations:
AB , BA , A+B , A+C ,BC, CC , CCT.
Which of the computations could not be computed ?

ramlal@ cc.iitd.ac.in, MATLAB WORKSHOP, CSC, IITD 6

You might also like