Ramlal@ CC - Iitd.ac - In, MATLAB WORKSHOP, CSC, IITD
Ramlal@ CC - Iitd.ac - In, MATLAB WORKSHOP, CSC, IITD
Ramlal@ CC - Iitd.ac - In, MATLAB WORKSHOP, CSC, IITD
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
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
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
Testing the Final Program Use different combinations of inputs until you have observed
that every line of code is executed at least once.
(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
(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.
(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).
(i) MATLAB can easily join matrices together to make a larger matrix:
(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
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 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