Introduction of MATLAB
Introduction of MATLAB
Experiment No. 1
Experiment Name: Introduction of MATLAB
1.1 Objectives:
1.2 Theory:
A flexible computational tool for developing algorithms, data visualization, and numerical
computation is called MATLAB (Matrix Laboratory). It is perfect for domains like machine
learning, control systems, and signal processing because of its proficiency in matrix opera-
tions. MATLAB facilitates effective problem solving and graphical visualization in both 2D
and 3D with its robust functions and toolboxes.
The clc command clears the Command Window in MATLAB, providing a clean win-
dow. It is useful for improving code readability during interactive sessions.
• clear all
Syntax: clear all
The clear all command removes all variables from the workspace and makes the
memory free. This command is beneficial when you want to avoid conflicts between
variables of the same name.
• close all
Syntax: close all
1
The close all command closes all open figure windows in MATLAB. It’s helpful for
managing resources and reducing clutter on the screen.
• .ˆ (Element-wise Power)
Syntax: A .B̂
The .ˆ operator performs element-wise power, allowing you to raise each element of
a matrix or array to a specified power individually.
• + (Addition)
Syntax: A + B
The + operator is used for addition in MATLAB, whether it’s scalar addition or matrix
addition.
• - (Subtraction)
Syntax: A - B
The - operator is used for subtraction, performing element-wise subtraction for ma-
trices or subtracting scalar values.
• * (Multiplication)
Syntax: A * B
• / (Division)
Syntax: A / B
The semicolon is used at the end of a statement to suppress the output from being
displayed in the Command Window.
• input
Syntax: x = input(’Prompt: ’)
2
The input function reads and evaluates a specified file, allowing interaction with the
user for input during program execution.
• disp
Syntax: disp(x)
The disp function displays the value of a variable or expression. It is commonly used
for printing results in the Command Window.
• figure
Syntax: figure
The figure command creates a new figure for plotting. MATLAB figures provide a
blank space for visualizing data and results.
• plot
Syntax: plot(X, Y)
The plot function is used for creating 2D plots in MATLAB. It allows you to visualize
data points and relationships between variables.
– title(’Title Text’)
– xlabel(’X-axis Label’)
– ylabel(’Y-axis Label’)
These functions are used to set titles and labels for the plot, making it more informative
and understandable.
• subplot
Syntax: subplot(m, n, p)
The subplot function divides the figure window into subplots, allowing multiple plots
to be displayed within the same figure.
3
• grid
Syntax: grid on or grid off
The grid function displays grid lines on a plot, aiding in the visual interpretation of
data.
• filter
Syntax: y = filter(b, a, x)
The filter function in MATLAB is used to apply a filter operation to a signal, which is
crucial in signal processing and analysis.
• MATLAB R2024b
MATLAB Code:
1 clc ;
2 clear all ;
3 close all ;
4 p = 0;
5 a = 1;
6 w = 2* pi *5;
7 t = 0:0.001:1;
8 A = a * sin ( w * t + p ) ;
9 subplot (2 ,1 ,1) ;
10 plot (t ,A , ’ black ’) ;
11 xlabel ( ’ Time ’) ;
4
12 ylabel ( ’ Amplitude ’) ;
13 title (" Sine signal ") ;
14 grid on ;
15 B = a * cos ( w * t + p ) ;
16 subplot (2 ,1 ,2) ;
17 plot (t , B ) ;
18 xlabel ( ’ Time ’) ;
19 ylabel ( ’ Amplitude ’) ;
20 title (" Cosine signal ") ;
21 grid on ;
Output:
5
Task 02: Plot the addition and subtraction of sine and cosine wave
MATLAB Code:
1 clc ;
2 clear all ;
3 close all ;
4
5 % Defining value
6 p = 0;
7 a = 1;
8 w = 2* pi *5;
9 t = 0:0.001:1;
10 % Signal equation
11 A = a * sin ( w * t + p ) ;
12 B = a * cos ( w * t + p ) ;
13 % Addition
14 y1 = A + B ;
15 subplot (2 ,1 ,1) ;
16 plot (t , y1 ) ;
17 xlabel ( ’ Time ’) ;
18 ylabel ( ’ Amplitude ’) ;
19 title (" Addition signal ") ;
20 grid on ;
21 % Subtraction
22 y2 = A - B ;
23 subplot (2 ,1 ,2) ;
24 plot (t , y2 ) ;
25 xlabel ( ’ Time ’) ;
26 ylabel ( ’ Amplitude ’) ;
27 title (" Subtraction signal ") ;
28 grid on ;
6
Output:
Task 03: Plot a Signal, Noise, and add Noise to the Signal
MATLAB Code:
1 clc ;
2 clear all ;
3 close all ;
4
5 fs = 100;
6 T = 1/ fs ;
7 t = 0: T :1 - T ;
8 f = 2;
7
9 w = 2* pi * f ;
10 A = 10;
11 C =5;
12 % Generate Signal
13 signal = A * sin ( w * t ) ;
14
15 % Generate Noise
16 noise = C * randn ( size ( t ) ) ;
17
29 subplot (3 , 1 , 2) ;
30 plot (t , noise , ’r ’ , ’ linewidth ’ , 2) ;
31 title ( ’ 2. Noise ’) ;
32 xlabel ( ’ Time ’) ;
33 ylabel ( ’ Amplitude ’) ;
34 grid on ;
35 subplot (3 ,1 ,3) ;
36 plot (t , m , ’c ’ , ’ linewidth ’ , 2) ;
37 title ( ’ 3. Noise + Signal ’) ;
38 xlabel ( ’ Time ’) ;
39 ylabel ( ’ Amplitude ’) ;
40 grid on ;
8
Output:
1.5 Discussion:
9
1.6 Conclusion:
The experiment successfully introduced the core features of MATLAB, showing its role as a
powerful tool for numerical computation, data analysis, and data visualization. This basic
knowledge is crucial for applying MATLAB to more complex problems in various domains,
including engineering, physics & data science, etc.
10