0% found this document useful (0 votes)
44 views48 pages

Organized

format for matlab

Uploaded by

qurekaabhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views48 pages

Organized

format for matlab

Uploaded by

qurekaabhi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

● Approved by AICTE, Ministry of HRD, Govt.

of India ●ISO 9001:2015, ISO 14001:2015, ISO 50001:2018


Affiliated: ●VMSB Uttarakhand Technical University ● Sri Dev Suman Uttarakhand University ● Uttarakhand Board of Technical Education

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)

S.No List of Experiments


1 Image Processing: To develop a MATLAB program for
image segmentation using techniques like thresholding, edge detection,
and region-based segmentation.

2 Signal Processing: To design and implement a MATLAB algorithm for


filtering noise from a given signal using techniques such as median filtering
or wavelet denoising.

3 Machine Learning: To create a MATLAB script for implementing a simple


machine learning algorithm such as linear regression or k-nearest neighbors
for a given dataset.

4 Machine Learning: Develop a MATLAB program to train and evaluate


machine learning models (e.g., support vector machines, neural networks)
for tasks such as classification or regression using benchmark datasets.

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.

7 Algorithm Analysis: Implement and analyze the time complexity of various


sorting algorithms (e.g., bubble sort, merge sort, quicksort) using MATLAB
to compare their efficiency.

8 Image Compression: Develop a MATLAB program to compress images using


techniques like discrete cosine transform (DCT) or discrete wavelet
transform (DWT)
Experiment No. -1

AIM: To Know About the history and features of Matlab.

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.

Main Features and Capabilities of MATLAB

MATLAB is used in every facet of computational mathematics. Following are some commonly
used mathematical calculations where it is used most commonly −

 Dealing with Matrices and Arrays


 2-D and 3-D Plotting and graphics
 Linear Algebra
 Algebraic Equations
 Non-linear Functions
 Statistics
 Data Analysis
 Calculus and Differential Equations
 Numerical Calculations
 Integration
 Transforms
 Curve Fitting

2
 Various other special functions

3
OUTPUT:

Matlab Work Environment :-

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.

Saving Your Work

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 :

In MATLAB environment, every variable is an array or matrix.

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

A) Find the max and min element of matrix

15
B) Performs square root operation on variable‘t’

C) gives the remainder after the dividing ‘p’ by ‘q’.

D) Length of the matrix...

E) Find the subtraction of two matrixes

17
F) Right-matrix division

v) Right-array division

W) Left 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.

D=ones(m,n) , E=eye(n) , X = inv(A)

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

AIM: To study about The Scaler Operations in Matlab.


Program:

a) find the factorial of the variable ./ matrix.


Factorial(a)
b) Print a pi value in matlab .
C = 3 *pi;
c) Find the square root of a variable .
C= Sqrt(a) or C = a * (1/2)
d) To show the working of nthroot. With variable and matrix..
Y = nthroot(X,N)
Y = nthroot(X,N) returns the real nth root of the elements of X. Both X and N must be
real scalars or arrays of the same size.
e) Create symbolic variables, expressions, functions, matrices using sym( ).
S= Sqrt(a)
SS= sym(s)
f) How to use pretty function in equations.
g) To Know about the Trigonometric function of MATLAB
i. Sin(a) - Sine of the input
ii. Cos(a)- Cosine of the input
iii. Tan(a)- Cosine of the input
iv. Asin(a) - Cosine of the input

h) To know about the Log function


Nl= Log(n)
V=log10(100)
V=log2(100)

21
OUTPUT:
a) find the factorial of the variable ./ matrix.
Factorial(a)

b) Print a pi value in matlab .


C = 3 *pi;

c) Find the square root of a variable .


C= Sqrt(a) or C = a * (1/2)

d) To show the working of nthroot. With variable and matrix..


Y = nthroot(X,N)

22
e) Create symbolic variables, expressions, functions, matrices using sym( ).

f) How to use pretty function in equations.

g) To Know about the Trigonometric function of MATLAB.

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:

a) How to use following Attributes in 2D Plots in Matlab.


MATLAB supports customizing the appearance and detailing of any graph. Users can

customize the graph by editing the following attributes for any 2D plot function.

1. xlabel : Generates labels along the x-axis .

2. ylabel: Generates labels along the y-axis .

3. Grid on : Enables the grid lines foe the graphs

4. Axis equal : Enable user to create the ploy with the same scale.

5. Axis square : Used to generate a square plot.

6. Hold On :

b) Distribute the ‘y’ values along the x-axis in the form of bars and Create a Bar graph.

c) Create a Pie graph.

d) How to plot a sin function in matlab ?

Y(x)= sin(x) , for 0<x<10

e) How to plot the given trigonometric function matlab?

Y(x) = (10 cos(x) sin(x) ) , for 1<x< 100

f) How to plot multiple lines in matlab .

Y1 = sin(x)

Y2= cos (x) , for 0<x<10

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.

g) How to plot a sin function in matlab ?

Y(x)= sin(x) , for 0<x<10

27
h) Create a Pie graph.

i) How to plot multiple lines in matlab .

Y1 = sin(x)

Y2= cos (x) , for 0<x<10

28
PRACTICAL NO: 6

Aim: To Study about the process to plot 3D graphs in the Matlab.


Program:

1. How to use following functions in 3D Plots in Matlab.

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)

x = [0.5*X(:); 0.25*X(:); X(:)];

y = [0.5*Y(:); 0.25*Y(:); Y(:)];

z = [0.5*Z(:); 0.25*Z(:); Z(:)];

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:

a. Write a simple program for If-Else statement.

b. Write a program to compare two numbers for if-Else statement.

c. Write a Program to show a use of Nested if Statement.

d. How to use a Repeat Statement for every Matrix Column using for loop .

e. How to display decremental values 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

a. Write a simple program for If-Else statement.

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.

Syntax of For Loop:


for index = value/values
statement
end

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

Example 4 – Using disp()

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:

a. Write a simple program to show the functionality of while loop.

b. Write a program to use a while loop to calculate factorial(10).

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 .

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.

41
OUTPUT

Syntax
while expression
statements
end

Explanation

1. While is the keyword for while loop.

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.

a.Write a simple program to show the functionality of while loop.

b. Write a program to use a while loop to calculate factorial(10).

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

You might also like