Matlab I
Matlab I
Dr.K.Srinivasa Rao
Professor & Head
Department of Mathematics
Sri Chandrasekharendra Saraswathi Viswa Mahavidyalaya
Kanchipuram, Tamilnadu
Material Description
The material provides a gentle introduction to the MATLAB com-
puting environment, and designed to give a basic understanding of
MATLAB. No prior programming experience or knowledge of MAT-
LAB is assumed.
Prerequisites
* No specific prerequisites are needed.
* It is advisable to have a have a good familiarity with PC oper-
ations.
* Basic knowledge of computer programming and an understand-
ing of matrix and linear algebra are highly beneficial.
What Is MATLAB?
MATLAB (MATrix LABoratory) is an efficient user-friendly interac-
tive software package, which is very effective for solving engineering,
mathematical, and system problems.
MATLAB Windows
The assumption here is that the reader is sitting in front of an
active computer and MATLAB is installed. To begin MATLAB,
double click the MATLAB icon on the computer’s desktop or select
MATLAB from the Start or Program menu. Immediately a special
window called the MATLAB desktop appears as below.
MATLAB Operations
Mathematical Functions
>> home
home moves the cursor to the upper-left corner of the Command
Window. You can use the scroll bar to see the history of previous
functions.
>> clc
clc clears all input and output from the Command Window display,
giving you a ”clean screen”. After using clc, you cannot use the
scroll bar to see the history of functions, but you still can use the
up arrow to recall statements from the command history.
>> clear
clear removes all variables from the workspace. This frees up system
memory.
This is what is called the default format, i.e. what normally hap-
pens. However, you can change from the default with variations on
the format command, as follows. If you want values displayed in
scientific notation (floating point form) whatever their size, enter
the command
>> format short e
All output from subsequent display statements will be in scientific
notation with five significant digits, until the next format command
is issued. Enter this command and check it with the following values:
0.0123456, 1.23456, 123.456 (all on separate lines). If you want
more accurate output, you can use
>> format long e
This also gives scientific notation, but with 15 significant digits. Try
it out on 1/7.
The commonly used formats are format short, format long, format
bank, format rat
We can change the format to format long by typing
>> format long √
View the result of value of 3 by typing
>> sqrt(3)
ans =
1.732050807568877
When the format is set to format short
>>format short
>> sqrt(3)
ans =
1.7321
>> round(6.628)
ans =
7
round command rounds the element of x to towards nearest integer
>> fix(6.628)
ans =
6
fix command rounds the element x to nearest integer towards zero
>> ceil(6.628)
ans =
7
ceil command rounds the element x to nearest integer towards
infinity
>> floor(6.628)
ans =
6
floor command rounds the element x to nearest integer towards
minus infinity
Operator Description
< less than
> greater than
<= less than or equal to
>= greater than or equal to
== equal to
∼= not equal to
>> find(a> 4)
ans =
4 5 6 7 8
>> find(a==b)
ans =
4
OR
100
P
Find the value of n
n=1
OR
100
P
Find the value of n
n=1
OR
100
P
Find the value of n
n=1
OR
100
P
Find the value of n
n=1
OR
100
P
Find the value of n
n=1
OR
100
n2
P
Find the value of
n=1
OR
100
n2
P
Find the value of
n=1
OR
100
n2
P
Find the value of
n=1
OR
100
n2
P
Find the value of
n=1
OR
100
n2
P
Find the value of
n=1
OR
50
P
Find the value of (2n − 1)
n=1
OR
50
P
Find the value of (2n − 1)
n=1
OR
50
P
Find the value of (2n − 1)
n=1
OR
50
P
Find the value of (2n − 1)
n=1
OR
50
P
Find the value of (2n − 1)
n=1
N
1
aN , for a =
P
Find the value of 2 and N = 50
n=1
N
1
aN , for a =
P
Find the value of 2 and N = 50
n=1
N
1
aN , for a =
P
Find the value of 2 and N = 50
n=1
N
1
aN , for a =
P
Find the value of 2 and N = 50
n=1
N
1
aN , for a =
P
Find the value of 2 and N = 50
n=1
N
1
aN , for a =
P
Find the value of 2 and N = 50
n=1
9
nn
P
Find the value of
n=1
9
nn
P
Find the value of
n=1
9
nn
P
Find the value of
n=1
9
nn
P
Find the value of
n=1
9
nn
P
Find the value of
n=1
9
nn
P
Find the value of
n=1
(−1)n + 1
3. Create a vector x with the elements, xn = . Add up
2n − 1
the elements of the version of this vector that has 100
elements.
4. Write a MATLAB program to find a factorial of n for given n
value
5. Given the arrays A = [ 2 3 7 8 4 1 9] and B=[2 4 6] provide
the commands needed to
(a). merge A and B horizontally
(b). insert the value 10 between A(4) and A(5)
(c). insert an array [2 5 10] between A(3) and A(4)
(d). merge A and B vertically
Command Output
a’ trancepose of a
a+b Performs addition of two matrices
a−b Performs subtraction of two matrices
a*b Performs matrix multiplication
a.*b Performs point-wise multiplication
diag(a) We get the diagonal elements of a
rank(a) We get the rank of the matrix a
det(a) We get the determinant of a
inv(a) inverse of a provided a is non-singular
[a b] a and b will be merged horizontally
[a;b] a and b will be merged vertically
a(i,j) Displays i th -row and j th - column element of a
Command Output
eye(m,n) Returns m × n matrix with ones on the main diagonal
zeros(m,n) Returns m × n matrix of zeros
ones(m,n) Returns m × n matrix of ones
rand(m,n) Returns m × n matrix of random numbers
randn(m,n) Returns m × n matrix of normally distributed numbers
diag(v) Returns a diagonal matrix with vector v on the diagonal
diag(a,1) Extracts the first upper-diagonal vector of matrix a
diag(a,-1) Extracts the first lower-diagonal vector of matrix a
The first five commands with single argument, i.e., eye(m), returns
unit matrix of order m. For example, eye(3) returns unit matrix of
order 3. ones(4) returns matrix of ones of order 4.
Consider a matrix
>> s=round(100*rand(6))
s=
81 28 96 79 68 71
91 55 49 96 76 3
13 96 80 66 74 28
91 96 14 4 39 5
63 16 42 85 66 10
10 97 92 93 17 82
The following command returns the 3rd and 4th column of s
>> s(:,[3 4])
The following command returns the 1st and 2nd row of s
>> s([1 2],:)
Also we can replace the two rows by new rows with the following
command
>> s([1 2],:) =[1 3 2 1 3 2;0 9 8 9 8 8]