Experiment-1:
Module-1: Introduction to MATLAB Basic Operations on Matrices
Aim:
(a) To define & use variables, vectors, Matrices & its operation in MATLAB.
(b) To study various arithmetic operators and mathematical functions in MATLAB.
(c) To create & use m-files.
SOFTWARE REQURIED:-
1.MATLAB R2015a.
2.Windows XP SP2.
THEORY:-
MATLAB, which stands for MATrixLABoratory, is a state-of-the-art mathematical software
package, which is used extensively in both academia and industry. It is an interactive program
for numerical computation and data visualization, which along with its programming capabilities
provides a very useful tool for almost all areas of science and engineering. Unlike other
mathematical packages, such as MAPLE or MATHEMATICA, MATLAB cannot perform
symbolic manipulations without the use of additional Toolboxes. It remains however, one of the
leading software packages for numerical computation. As you might guess from its name,
MATLAB deals mainly with matrices. A scalar is a 1-by-1 matrix and a row vector of length say
5, is a 1-by-5 matrix.. One of the many advantages of MATLAB is the natural notation used. It
looks a lot like the notation that you encounter in a linear algebra. This makes the use of the
program especially easy and it is what makes MATLAB a natural choice for numerical
computations. The purpose of this experiment is to familiarize MATLAB, by introducing the
basic features and commands of the program.
Built in Functions:
1. Scalar Functions:
Certain MATLAB functions are essentially used on scalars, but operate element-wise when
applied to a matrix (or vector). They are summarized below.
1. sin - trigonometric sine
2. cos - trigonometric cosine
3. tan - trigonometric tangent
4. asin - trigonometric inverse sine (arcsine)
5. acos - trigonometric inverse cosine (arccosine)
6. atan - trigonometric inverse tangent (arctangent)
7. exp - exponential
8. log - natural logarithm
9. abs - absolute value
10. sqrt - square root
11. rem - remainder
12. round - round towards nearest integer
13. floor - round towards negative infinity
14. ceil - round towards positive infinity
2. Vector Functions:
Other MATLAB functions operate essentially on vectors returning a scalar value. Some of
these functions are given below.
1. max largest component : get the row in which the maximum element lies
2. min smallest component
3. lengthlength of a vector
4. sortsort in ascending order
5. sumsum of elements
6. prod product of elements
7. medianmedian value
8. meanmean value std standard deviation
3. Matrix Functions:
Much of MATLAB‟s power comes from its matrix functions. These can be further separated
into two sub-categories.
The first one consists of convenient matrix building functions, some of which are given
below.
1. diag(v) % change a vector v to a diagonal matrix.
2. diag(A) % get diagonal of A.
3. eye(n) % identity matrix of size n.
4. zeros(m,n) % m-by-n zero matrix.
5. ones(m,n) % m*n matrix with all ones.
commands in the second sub-category of matrix functions are
1. size-size of a matrix
2. det -determinant of a square matrix
3. inv -inverse of a matrix
4. rank-rank of a matrix
6. eig -eigenvalues and eigenvectors
7. poly -characteristic polynomial
Matlab’s Workspace:
• The workspace is Matlab’s memory
• Can manipulate variables stored in the workspace
who, whos – current workspace variables.
clear all – clear workspace variables.
close all – close all figures
clc – clear screen
Variables & vectors:
>> a=12; % variable a is assigned 12
View variable contents by simply typing the variable name at the command prompt
>> a
a=
12
>>
>> a*2
a=
24
>>
>> b=[1:5]
b=
1 2 3 4 5
>> b=[1:5] % Row vector
b=
1 2 3 4 5
>> c=[1;2;3;4] % Column vector
c=
1
2
3
4
>> A=[1,2;3,5;7,8]
A=
1 2
3 5
7 8
>> A=[1,2;3,5;7,8]; %semicolon at the end of an input line suppresses printing of the output of
the MATLAB command
Matrices & Mathematical operations on Matrices:
>>A = [3 2 1; 5 1 0; 2 1 7]
A=
3 2 1
5 1 0
2 1 7
• Access elements of a matrix
>>A(1,2)
ans=
2
Manipulating Matrices:
>> A ' % transpose
>> B*A % matrix multiplication
>> B.*A % element by element multiplication
>> B/A % matrix division
>> B./A% element by element division
>> [B A] % Join matrices (horizontally)
>> [B; A] % Join matrices (vertically)
The : operator:
• VERY important operator in Matlab
• Means ‘to’
>> 1:10
ans =
1 2 3 4 5 6 7 8 9 10
>> 1:2:10
ans =
1 3 5 7 9
>>A(3,2:3)
ans =
1 7
>>A(:,2)
ans =
2
1
1
Creation & use of M-file:\
PROCEDURE:-
Open MATLAB
Open new M-file
Type the program
Save in current directory
Compile and Run the program
For the output see command window\ Figure window
PROGRAM:-
clc;
close all;
clear all;
a=[1 2 -9 ; 2 -1 2; 3 -4 3];
b=[1 2 3; 4 5 6; 7 8 9];
disp('The matrix a= ');
a
disp('The matrix b= ');
b
% to find sum of a and b
c=a+b;
disp('The sum of a and b is ');
c
% to find difference of a and b
d=a-b;
disp('The difference of a and b is ');
d
%to find multiplication of a and b
e=a*b;
disp('The product of a and b is ');
e
OUTPUT:-
The matrix a=
a=
1 2 -9
2 -1 2
3 -4 3
The matrix b=
b=
123
456
789
The sum of a and b is
c=
2 4 -6
648
10 4 12
The difference of a and b is
d=
0 0 -12
-2 -6 -4
-4 -12 -6
The product of a and b is
e=
-54 -60 -66
12 15 18
8 10 12
RESULT:-