Organized
Organized
A
Lab Record
MATLAB Programming
(BCSP-606)
Session: - 2023-24
Department of Computer Science and Engineering
Submitted to Submitted by
Girish Singh Bisht Naisha Kashyap
Assistant professor (210120101067)
INDEX
Teacher’s Remark
S. no. Experiment Name Date
Signature
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
List of Experiments (beyon the syllabus)
Semester VI B.Tech
Open Sourse Lab (BCSP-606)
5 Text Mining: Create a MATLAB script to perform text mining tasks such as
sentiment analysis or topic modeling on a given dataset of textual
documents, utilizing techniques like natural language processing (NLP) and
machine learning..
6 Optimization: To optimize a given function using MATLAB's Optimization
Toolbox, exploring different optimization algorithms such as gradient
descent, genetic algorithms, or simulated annealing.
Program:
MATLAB stands for Matrix Laboratory. It is a high-performance language that is used for
technical computing. fourth-generation high-level programming language. As its name
contains the word Matrix, MATLAB does its' all computing based on mathematical matrices
and arrays. The necessary building components of MATLAB are the matrix. The fundamental
data type is the array. Vectors, scalars, real matrices, and complex matrices are all
automatically handled as special cases of the primary data type. MATLAB loves matrices and
matrix functions. The built-in functions are optimized for vector functions. Therefore,
Vectorized commands or codes run much faster in MATLAB.MATLAB's all types of variables
hold data in the form of the array only, let it be an integer type, character type or String type
variable.
History of MATLAB
It was developed by Cleve Molar of the company MathWorks.Inc in the year 1984. The
development of the MATLAB started in the late 1970s by Cleve Moler, the chairman of the
Computer Science department at the University of New Mexico. Cleve wanted to make his
students able to use LINPACK & EISPACK (software libraries for numerical computing, written
in FORTRAN), and without learning FORTRAN. In 1984, Cleve Moler with Jack Little & Steve
Bangert rewrote MATLAB in C and founded MathWorks. These libraries were known as
JACKPAC at that time, later these were revised in 2000 for matrix manipulation and named as
LAPACK.
MATLAB is used in every facet of computational mathematics. Following are some commonly
used mathematical calculations where it is used most commonly −
2
Various other special functions
3
OUTPUT:
The desktop includes these panels: Current Folder - This panel allows you to access the project
folders and files
Command Window - This is the main area where commands can be entered at the command
line. It is indicated by the command prompt (>>).
4
Workspace - The workspace shows all the variables created and/or imported from files.
Command History - This panel shows or rerun commands that are entered at the command
line.
5
Experiment No. -2
AIM: To Study about the local environment and basic Mathematical Operations In Matlab.
Program:
You are now faced with the MATLAB desktop on your computer, which contains the prompt
(>>) in the Command Window. Usually, there are 2 types of prompt: >>For full version EDU>
for educational version Note: 1. To simplify the notation, we will use this prompt, >>, as a
standard prompt sign, though our MATLAB version is for educational purpose. 2. MATLAB
adds variable to the workspace and displays the result in the Command Window.
Naming Variables
Variable names consist of a letter followed by any number of letters, digits or underscore.
MATLAB is case-sensitive. Variable names can be of any length; however, MATLAB uses only
first N characters, where N is given by the function namelengthmax.
The save command is used for saving all the variables in the workspace, as a file with .mat
extension, in the current directory. For example, save myfile You can reload the file anytime
later using the load command.
Example 1 :
6
Example 2 :
7
In the above example it creates a 1-by-1 matrix named ‘x’and stores the value 3 in its element
8
9
Experiment No. -3
AIM: To Study About the Matrix commands and process of writing mathematical
functions and equations in Matlab.
Program:
a) Create simple elements vector with 10 elements called A.
b) Add 3 to each element of vector A and store the result in a new vector B .
B=A+3
c) Creating a 5*5 C matrix using semicolon( :) to separate the the rows of matrix .
d) Find the transpose of the matrix and strore in the new matrix Y.
Y= C’
Y=transpose(C)
e) Create a square matrix by using magic() function .
M = magic(n)
f) Multiply two matrix( X and Y) .
Z= X*Y
g) Multiply the corresponding elements of matrix
P=X.*Y
h) Find the square of corresponding element of matrix .
V = X.^2
i) Divide Matrices element by element
G = X./Y
j) Extract an element of a Matrix.
k) Create a column vector X1 , from the element of the 4th row of the matrix ‘X”, To
refer all the element in the mth columns we type X(;,m)
X1= (x(4,:))’
l) Select the elements in the m’th through n’th columns of matrix ‘X’.
X1=(: ,3:4)
m) Create a sub matrix X2 by taking a inner sub part of matrix ‘X’.
X2= X(2:end, 2:end)
X2= X(3:end,2:end)
Delete a columns and a row from matrix ‘X’.
X(:,1) =[ ]
X(2,: ) = [ ]
n) Create two 1-by-3 string arrays, then append similarly located strings in the arrays.
s1 = ["Red" "Blue" "Green"]
s2 = ["Truck" "Sky" "Tree"]
s = s1 + s2
s = plus(S1,s2)
o) find the max and min element of matrix .
p) Performs square root operation on variable ‘t’.
S=sqrt(t)
q) Gives the remainder after the dividing ‘p’ by ‘q’.
rem(p,q)
r) Find the Length of the matrix :
L= Length(t)
10
s) Find the subtraction of two matrix
C = minus(A,B)
C = A-B
t) Right-matrix division
X = A/b
X = mrdivide(A, b)
u) Right-array division
X = A./B
X = rdivide(A,B) #X = A./B performs right-array division by dividing each
element of A by the corresponding element of B.
v) Find the Left array division of two matrix
x = B.\A
x = ldivide(B,A) # x = B.\A divides each element of A by the corresponding
element of B. The sizes of A and B must be the same or be compatible.There are two
operators allowing to divide in Matlab
w) Find the size of matrix and create a matric with all elements equal to zeros .
B= Size(A) and C = zeros(m,n)
w) Create a Matrix with All elements equal to one , Create an Identity matrix and Find
the inverse of the matrix.
D=ones(m,n) , E=eye(n) , X = inv(A)
x) Find the determinants of a Matrix and To create a Matrix with Random element in
Matlab.
Y= det(A) and z= Rand(A)
11
OUTPUT:
12
OUTPUT
13
OUTPUT:
14
OUTPUT
15
B) Performs square root operation on variable‘t’
17
F) Right-matrix division
v) Right-array division
18
x) Find the size of matrix and create a matric with all elements equal to zeros.
y) Create a Matrix with All elements equal to one , Create an Identity matrix and Find the
inverse of the matrix.
19
z) Find the determinants of a Matrix and To create a Matrix with Random element in Matlab.
Y= det(A) and z= Rand(A)
20
Experiment No. -4
21
OUTPUT:
a) find the factorial of the variable ./ matrix.
Factorial(a)
22
e) Create symbolic variables, expressions, functions, matrices using sym( ).
23
h) To know about the Log function
Nl= Log(n)
V=log10(100)
V=log2(100)
24
Experiment No. -5
AIM: To Study about the process to plot 2D graphs in the MATLAB for the given Trigonometric
equations.
Program:
customize the graph by editing the following attributes for any 2D plot function.
4. Axis equal : Enable user to create the ploy with the same scale.
6. Hold On :
b) Distribute the ‘y’ values along the x-axis in the form of bars and Create a Bar graph.
Y1 = sin(x)
25
OUTPUT:
a) How to use following Attributes in 2D Plots in Matlab.
26
b) Distribute the ‘y’ values along the x-axis in the form of bars and Create a Bar graph.
27
h) Create a Pie graph.
Y1 = sin(x)
28
PRACTICAL NO: 6
a) Plot3(x,y,z): If x,y,z are vectors of the same length, then this function will create a set of
coordinates connected by line segments. If we specify at least one of x, y or z as vectors, it will
plot multiple sets of coordinates for the same set of axes.
Code:
A=0:
pi/100;
50*pi;
sa=sin(a);
ca=cos(a);
plot3(sa, ca, a)
Output :-
b) plot3(X, Y, Z, LineSpec): This function will create the plot using the specified line style,
marker, and color.
Code :
A= 0:pi/20:2*pi;
sa= sin(a);
ca=cos(a) ;
plot3(sa, ca, a, ’o’)
29
c) plot3(X1, Y1, Z1,…, Xn, Yn, Zn): This function will plot multiple coordinates for the sam
e set of axes.
d) scatter3(X, Y, Z): This function will create circles at the vector locations of x, y, and z.(
SCATTER3 (3D scatter Plot))
Code :
[X,Y,Z] = SPHERE(10)
30
31
PRACTICAL NO: 7
AIM: To Create and Run Script File in MATLAB with In-BUILD functions.
Program:
Script (M-file):
Create a Script (M-file) where you create a vector with random data and find the average and the
standard deviation • Run the Script from the Command window.
MATLAB contains hundreds of built-in functions, but very often you need to create your own
functions.
32
Create a function calc_average() that finds the average of two numbers. • Test the function
afterwards in the Command window as follows:
Create a function circle that finds the area in a circle based on the input parameter r (radius). •
Run and test the function in the Command window.
33
PRACTICAL NO: 8
AIM: To Implement different programs with the help of for, while loops in Matlab.
Program:
d. How to use a Repeat Statement for every Matrix Column using for loop .
OUTPUT:
34
If Else Statement if Matlab :-
If the statement executes code or statement block only when the condition is true. It is a
conditional programming keyword used to give conditions to the program on Matlab.
It has three parts if statement, else statement and else if statement if-else statement in
Matlab.
If the first expression or condition is true then ‘ if ’ statement executes. If the expression is
false then else statement executes. And if there are multiple conditions in code then else if
the statement is used in Matlab.
Syntax:
If (condition)
Statement
Else
Statement
end
35
b. Write a program to compare two numbers for if-Else statement.
36
f. c. Write a Program to show a use of Nested if Statement.
for index = values, statements, end executes a group of statements in a loop for a
specified number of times. values has one of the following forms:
initVal:endVal — Increment the index variable from initVal to endVal by 1,
and repeat execution of statements until index is greater than endVal.
initVal:step:endVal — Increment index by the value step on each iteration, or
decrements index when step is negative.
valArray — Create a column vector, index, from subsequent columns of
array valArray on each iteration. For example, on the first
iteration, index = valArray(:,1). The loop executes a maximum of n times,
where n is the number of columns of valArray, given by numel(valArray(1,:)).
The input valArray can be of any MATLAB® data type, including a character
vector, cell array, or struct.
37
d.How to use a Repeat Statement for every Matrix Column using for loop .
38
Example 3 of using for loop
39
e. How to display decremental values using for loop
40
PRACTICAL NO: 9
AIM: To Implement different programs with the help of break, continue loops in Matlab.
.
Program:
c. Write a program to sum a sequence of random numbers until the next random number
is greater than an upper limit. Then, exit the loop using a break statement.
d. Write a Program to find even or odd elements for given data using while loop .
f. Write a Program to use break statement used for an inner loop as well as for the outer
loop.
41
OUTPUT
Syntax
while expression
statements
end
Explanation
2. An expression is a condition that needs to be true for the while loop to work.
3.Statements are the actions that would be executed if the condition or expression is true.
4.The end is the keyword which suggested the closure of the loop.
42
c. Write a program to sum a sequence of random numbers until the next random number
is greater than an upper limit. Then, exit the loop using a break statement.
d. Write a Program to find even or odd elements for given data using while loop .
43
e. Write a program to show the to show the working of switch statement .
f. Write a Program to use break statement used for an inner loop as well as for the outer
loop.
44
.
45
PRACTICAL NO: 10
AIM: To study about the process to Import Data and Analyze with Matlab.
Program:
Importing data in MATLAB means loading data from an external file. The importdata function
allows loading various data files of different formats. It has the following five forms.
Sr.No. Function & Description
1 A = importdata(filename)
Loads data into array A from the file denoted by filename.
2 A = importdata('-pastespecial')
Loads data from the system clipboard rather than from a file.
3 A = importdata( , delimiterIn)
Interprets delimiterIn as the column separator in ASCII file, filename, or the
clipboard data. You can use delimiterIn with any of the input arguments in the
above syntaxes.
4 A = importdata( , delimiterIn, headerlinesIn)
Loads data from ASCII file, filename, or the clipboard, reading numeric data
starting from line headerlinesIn+1.
5 [A, delimiterOut, headerlinesOut] = importdata( _)
Returns the detected delimiter character for the input ASCII file
in delimiterOut and the detected number of header lines in headerlinesOut, using
any of the input arguments in the previous syntaxes.
46
47