Matlab
Matlab
BACHELOR OF TECHNOLOGY
Index
S.No EXPERIMENTS Date Sign Remark
INTRODUCTION TO MATLAB
1.
Creating a One and Two-Dimensional Array
(Row / Column Vector) (Matrix of given size)
then, (A). Performing Arithmetic Operations -
Addition, Subtraction, Multiplication and
Exponentiation. (B). Performing Matrix
operations - Inverse, Transpose, Rank with
plots.
MATLAB is an interactive system whose basic data element is an array that does not
require dimensioning. This allows you to solve many technical computing problems,
especially those with matrix and vector formulations, in a fraction of the time it would take
to write a SUGGESTED PROGRAM: in a scalar non interactive language such as C or
Fortran. The name MATLAB stands for matrix laboratory.
EXPERIMENT NO. 01
OBJECTIVE: Creating a 1-D array and 2-D array(row/column vector) and then
performing
a) Arithmetic Operations; Addition, Subtraction, Multiplication, Exponentiation
b) Matrix Operations; Inverse, Transpose, Rank with plots.
Matrix arithmetic operations are same as defined in linear algebra. Array operations are
executed element by element, both on one dimensional and multi-dimensional array.
ARITHMETIC OPERATIONS:
Addition (+): Addition or unary plus. A+B adds the values stored in variables A and B. A and
B must have the same size.
Subtraction (-): Subtraction or unary minus. A-B subtracts the value of B from A. A and B must
have the same size.
Matrix Multiplication (*): Matrix multiplication. C = A*B is the linear algebraic product of the
matrices A and B. For non-scalar A and B, the number of columns of A must be equal to the
columns of B.
Array Multiplication (.*): Array multiplication. A.*B is the element-by-element product of the
The matrix is called the inverse of X.
Inverse: A matrix X is invertible if there exists a matrix of the same size such that
XY=YX.
The matrix is called the inverse of X.
Transpose: B =A’ returns the non-conjugate transpose of A, that is, interchanges the
row and
column index for each element.
Rank: The number of linearly independent columns in a matrix is the rank of the
matrix. The
rank gives a measure of the dimension of the range or column space of the matrix,
which is the
collection of all linear combinations of the columns.
Commands:
Clc - Clears command windows
Program :
Clc
Clear all
Creating 1D arrays
a= [1 2 3]
b= [4 5 6]
c= [7;8;9]
Addition of 1D arrays
Sum=a+b
Subtraction of 1D arrays
Sub=a-b
Multiplication of 1D arrays
Mul=a*c
Exponentiation of a matrix
expo_a=exp(a)
Creating 2D arrays
a2=[2 1 2; 7 8 9; 6 5 4]
b2=[2 2 2; 3 3 3; 4 4 4]
Addition of 2D arrays
c2=a2+b2
Subtraction of 2D arrays
c3=a2-b2
Multiplication of 2D arrays
c4=a2*b2
Inverse of Matrix
inversea2=inv(a2)
Transpose of Matrix
Transpose_a2=transpose(a2)
Rank of Matrix
Rank_a2=rank(a2)
RESULT:
1) ARITHMETIC OPERATIONS:
2) Matrix Operations
EXPERIMENT NO.02
THEORY:
CONCATENATION: Just like in actual world, where concatenation is used to join smaller
things to make something bigger, concatenation has the same application in MATLAB. Using
concatenation we can join smaller matrices to produce bigger matrices. As in a matrix, its most
fundamental unit is an element of that matrix, an entire matrix can be seen as an amalgamation
of all the elements in your matrix. In other words, a matrix can be viewed as a product of
concatenation of all the different elements of that matrix.
INDEXING: Indexing on a matrix means selecting a subset of elements from itself. Indexing
as a function is used as a first or primary operation before many advanced operations. There
are many types of indexing possible as well, depending upon whether you want to select
individual elements out of a given matrix or a set or range of elements.
SORTING: Just like we operated while indexing matrices, in a column major order, sorting
of matrices works in the same manner.
SHIFTING: Shifting of matrices takes place in term of rows and/or columns. MATLAB
allows us to shift rows and/or columns simultaneously in individual commands.
RESHAPING: Reshaping a matrix means changing the order of the matrix. It is done when
for example we create a matrix of the order (m x n) while we actually wanted it to be of the
order (p x q) or just of any other order but (m x n).
RESIZING:
FLIPPING: To understand flipping about axes, first we should be able to understand axes. In
terms of MATLAB, up-down form the vertical axis while left-right form the horizontal axis.
RELATIONAL OPERATORS: Relational operators can also work on both scalar and non-
scalar data. Relational operators for arrays perform element-by-element comparisons between
two arrays and return a logical array of the same size, with elements set to logical 1 (true) where
the relation is true and elements set to logical 0 (false) where it is not.
LOGICAL OPERATORS:
MATLAB offers two types of logical operators and functions −
• Element-wise − These operators operate on corresponding elements of logical arrays.
• Short-circuit − These operators operate on scalar and, logical expressions.
Element-wise logical operators operate element-by-element on logical arrays. The symbols
&, |, and ~ are the logical array operators AND, OR, and NOT.
Short-circuit logical operators allow short-circuiting on logical operations. The symbols &&
and || are the logical short-circuit operators AND and OR.
RESULT:
1) Concatenating:
2) Indexing:
3) Sorting:
4) Shifting:
5) Reshaping:
6) Flipping:
Creating Arrays X & Y of given size (1 x N):
(A): Relational Operations ( >, <, = =, <=, >=, ~= )
(B): Logical Operations ( ~, &, |, XOR )
EXPERIMENT NO. 03
THEORY:
Matrices with one dimension equal to one and the other greater than one are called
vectors. Here is an example of a numeric vector:
A = [5.73 2-4i 9/7 25e3 .046 sqrt(32) 8j];
size(A) % Check value of row and column
dimensions ans =
1 7
We can construct a vector out of other vectors, as long as the critical dimensions agree. All
components of a row vector must be scalars or other row vectors. Similarly, all components
of a column vector must be scalars or other column vectors: A = [29 43 77 9
21]; B = [0 46 11];
C = [A 5 ones(1,3) B]
C=
29 43 77 9 21 5 1 1 1 0 46 11
RANDOM:
rand
:
Matrices with one dimension equal to one and the other greater than one are called vectors.
Here is an example of a numeric vector:
A = [5.73 2-4i 9/7 25e3 .046 sqrt(32) 8j];
size(A) % Check value of row and column
dimensions ans = 1 7
We can construct a vector out of other vectors, as long as the critical dimensions agree. All
components of a row vector must be scalars or other row vectors. Similarly, all components
of a column vector must be scalars or other column vectors: A = [29 43 77 9
21]; B = [0 46 11];
C = [A 5 ones(1,3) B]
C = 29 43 77 9 21 5 1 1 1 0 46 11
RESULT:
THEORY:
Round - Round to nearest integer
Syntax: Y = round(X)
Description: Y = round(X) rounds the elements of X to the nearest integers. For complex X,
the imaginary and real parts are rounded independently.
Trigonometric Functions
The trigonometric functions are used along with their names and have the angle value as the
parameters in them. A range for the value of the parameter is defined to attain their graphical
representations.
Syntax: A = ‘trigonometric function name’(Value)
Ex. sin(30), cos(115), tan(-45)
RESULT:
OPEN ENDED QUESTIONS
QUESTION 1
OBJECTIVE: Write a function called flip-it that has one input argument, a row vector v,
and one output argument, a row vector w that is of the same length as v. The vector w
contains all the elements of v, but in the exact opposite order. For example, if v is equal to [1
2 3] then w must be equal to [3 2 1]. You are not allowed to use the built-in function flip. Use
function rot90.
Flipping a vector means flipping the elements of the vector, i.e., reversing the order in which
the elements are stored in the vector.
rot90: Matlab built_in function rot90 (A,k) can be used to rotate images in 90 degrees. Here
is an example using rot90: Assign K=1 for 90 degree, 2 for 180, 3 for 270 and 4 for 360. The
output image will be rotated 90 degrees. Another matlab built_in function flipud (A) can be
used to rotate the image 90 degrees.
FUNCTION:
RESULT:
>> A = [1 2 3]
A =
1 2 3
>> B = flip_it(A)
B =
3 2 1
QUESTION 2
OBJECTIVE: Write a function called top_right that takes two inputs: a matrix N and a
scalar non-negative integer n, in that order, where each dimension of N is greater than or
equal to n. The function returns the n-by-n square subarray of N located at the top right
corner of N.
THEORY: This function takes in a matrix N and a scalar n, and returns the n-by-n square
subarray located at the top right corner of N. The function first computes the dimensions of N
using the size function, and stores the number of rows in ~ and the number of columns in k.
Then, it uses indexing to extract the subarray at the top right corner, which starts at row 1 and
goes to row n, and starts at column k-n+1 and goes to column k. The subarray is stored in the
output variable T.
RESULT:
>> A = [4 5 6; 9 2 7; 5 4 3; 8 3 2]
A =
4 5 6
9 2 7
5 4 3
8 3 2
>> n = 2
n =
>> top_right(A,n)
ans =
5 6
2 7
QUESTION 3
OBJECTIVE: Input a one-dimensional array x of 10 elements with linspace command and
find y = x2.
RESULT:
>> x = linspace(0,9,10)
x =
0 1 2 3 4 5 6 7 8 9
>> y = x.^2
y =
0 1 4 9 16 25 36 49 64 81
>> plot(x,y)
QUESTION 4
25 1 −1
OBJECTIVE: Compute the following and compare results 25 −1
and (1 − 25 ) .
To compute the given expressions in MATLAB, we can simply type the expressions into the
command window and press enter. MATLAB will automatically calculate the results.
As we can see, the results of the two expressions are the same, with the value of
approximately 1.0323.
Q.1- Draw a circle of unit Radius.
Q.2- Plot y=sin x, 0 ≤ x ≤ 2π, taking 100 linearly spaced points in
the given interval. Label the axes and put ‘Sine Plot’ in the title.
Q.3- Make the same plot as above , but rather than displaying the
graph as a curve , show the unconnected data points . To display the
data points with small circles , use plot (x, y,’o’). Two plots can also be
combined with the commands plot(x,y,x,y,’o’).
Q.4- Plot y=e-0.4x sinx, 0 ≤ x ≤ 4π, taking 10, 50, and 100 points interval.
For 10
For 50
For 100
Q.5- Use the command plot3(x,y,z)to plot the circular helix x(t)=sin t,
y(t)= cos t, z(t)=t, 0≤ t≤ 20.