NAME : MUHAMMAD HAMZA
REG. NO : 23PWELE6203
SECTION : A
1. What is MATLAB?
MATLAB (short for MATrix LABoratory) is a high-level
programming language and computational environment developed
by Math Works. It is primarily used for numerical computing, data
analysis, algorithm development, and visualization. MATLAB is
widely used in engineering, scientific research, finance, and artificial
intelligence due to its efficiency in handling mathematical and
computational problems
Key Features of MATLAB
Matrix-Based Computing – MATLAB is designed for easy
manipulation of matrices and arrays.
Extensive Built-in Functions – Offers a wide range of
mathematical, statistical, and engineering functions.
Graphical Visualization – Provides tools for 2D and 3D
plotting to analyze data effectively.
Interactive Environment – Includes an easy-to-use IDE with
script execution and debugging features.
Toolboxes & Libraries – Specialized toolboxes for image
processing, machine learning, control systems, etc.
Integration & Compatibility – Supports interfacing with
languages like C, C++, Python, and Java.
2. Importance of MATLAB
A. Engineering and Scientific Research
MATLAB is extensively used in fields like mechanical, electrical, civil,
and aerospace engineering for simulations, modeling, and analysis.
Scientists use it for data visualization and numerical computations.
B. Data Analysis and Machine Learning
MATLAB provides advanced statistical and machine learning tools,
making it crucial for big data analytics, deep learning, and artificial
intelligence applications.
C. Image Processing and Computer Vision
It is widely used for medical imaging, object detection, and feature
extraction, playing a key role in robotics and autonomous systems.
D. Control Systems and Signal Processing
MATLAB helps in designing control systems, digital signal processing
(DSP), and communication systems, making it a valuable tool in
automation and electronics industries.
E. Financial and Economic Modeling
MATLAB is used in risk management, algorithmic trading, and
financial simulations, assisting in data-driven decision-making in
finance and economics
Common Types of Commands in MATLAB
MATLAB commands: In MATLAB, a command refers to a statement
or instruction that you enter in the Command Window (or in a script) to
perform a specific action. These commands are used to execute
operations, run calculations, or control the behavior of MATLAB.
Commands in MATLAB are typically written in a line-by-line format.
MATLAB commands are used to perform various operations such as
matrix creation, mathematical operations, plotting, and control flow.
1. Matrix Creation Commands
a) Row Vector
A 1D array (row vector) is created by separating elements with spaces
or commas.
A = [4 7 10];
Output:
A=
4 7 10
b) Column Vector
A column vector is created using semicolons.
B = [2; 5; 8];
Output:
B=
2
5
8
c) 2D Matrix
A multi-row matrix is created by separating rows with semicolons.
M = [1 3 5; 2 4 6; 7 8 9];
Output:
M
=
1 3 5
2 4 6
7 8 9
d) Special Matrices
Zeros Matrix
Creates a matrix filled with zeros of size (m, n).
Z = zeros(2,3);
Output:
Z
=
0 0 0
0 0 0
Ones Matrix
Creates a matrix filled with ones of size (m, n).
O = ones(3,2);
Output:
O
=
1 1
1 1
1 1
Identity Matrix
Creates an identity matrix (diagonal elements are 1, others are 0).
I = eye(4);
Output:
I
=
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
Common Built-in Functions
MATLAB provides many built-in functions for mathematical
calculations.
1. Mathematical Functions
sqrt(25) % Square
root of 25 abs(-8) %
Absolute value of -8
log10(1000) %
Base-10 logarithm
exp(2) % e^2
(exponential function)
sin(pi/2) % Sine of 90
degrees
cos(0) % Cosine of 0
degrees tan(pi/4)
% Tangent of
45 degrees
Outputs:
5
8
3
7.3891
1
1
1
2. Statistical Functions
mean([2 4 6 8]) % Mean (Average)
median([1 3 3 6]) % Median
std([1 2 3 4]) % Standard Deviation
Outputs:
5
3
1.29099
Built-in Constants in MATLAB
MATLAB provides predefined constants.
pi % Value of π (3.1416)
exp(1) % Euler's
Number (2.7183) i %
Imaginary unit (√-1)
inf % Infinity
NaN % Not a Number
Outputs:
3.1416
2.7183
0.0000 + 1.0000i
Basic Linear Algebra in MATLAB
MATLAB is designed for matrix operations and linear algebra
calculations.
1. Matrix Addition & Subtraction
A = [1 2; 3 4];
B = [5 6; 7 8];
C = A + B; % Addition
D = A - B; % Subtraction
Outputs:
C=
6 8
10 12
D=
-4 -4
-4 -4
2. Matrix Multiplication
A = [1 2; 3 4];
B = [5 6; 7 8];
C = A * B; % Matrix Multiplication
Output:
C=
19 22
43 50
Scalar Multiplication
A = [2 3; 4 5];
B = 3 * A; % Multiply each element by 3
Output:
B=
6 9
12 15
3. Element-wise Operations
A = [2 4; 6 8];
B = [1 2; 3 4];
C = A .* B; % Element-wise Multiplication
D = A ./ B; % Element-wise Division
Outputs:
C=
2 8
18 32
D=
2.0000 2.0000
2.0000 2.0000
4. Determinant of a Matrix
A = [4 7; 2 6];
det_A = det(A);
Output:
det_A = 10
Matrix Transpose
A = [1 3 5; 2 4 6];
B = A'; % Transpose of A
Output:
B
=
1 2
3 4
5 6
Inverse of a Matrix in MATLAB
To compute the inverse of a square matrix in MATLAB, use the inv()
function
A = [4 7; 2 6];
B = inv(A);
disp(B);
Output:
0.6000 -0.7000
-0.2000 0.4000
Matrix Indexing Commands
a) Accessing an Element
M = [1 2 3; 4 5 6; 7 8 9];
val = M(2,3); % Access element at row 2, column 3
Output:
val = 6
b) Extracting a Submatrix
subM = M(1:2, 2:3); % First 2 rows, last 2 columns
Output:
subM =
2 3
5 6
b) Extracting Rows &
Columns row2 = M(2, :);
% Entire second row col3
= M(:, 3); % Entire third
column
Output:
row2 = [4 5
6] col3 =
3
6
9
5. Eigenvalues and Eigenvectors
[V, D] = eig(A);
Output:
V=
-0.8954 -0.4472
0.4472 -0.8954
D=
3.0000 0
0 7.0000
Trigonometric Functions in MATLAB
MATLAB provides built-in functions for trigonometric calculations,
which work with angles in radians by default.
1. Basic Trigonometric
Functions sin(pi/6) % Sine
of 30 degrees cos(pi/3) %
Cosine of 60 degrees
tan(pi/4) % Tangent of 45 degrees
Outputs:
0.5000
0.5000
1.0000
2. Converting Degrees to Radians and Vice Versa:
rad =
deg2rad(90);
disp(rad);
Output:
1.5708
Linespace
It is used to find stepsize
Y =Linespace (0 , 20,
15) Output:
Y = 0 1.4286 2.85 4.28 5.71 7.1 --------- 20.00
Different Line Styles in MATLAB Plot
You can specify different line styles in the plot() function using symbols:
x = 0:0.1:10;
y1 =
sin(x);
y2 =
cos(x);
plot (x, y1, '--r', x, y2, ':b');
legend ('Sine - Dashed Red', 'Cosine -
Dotted Blue'); grid on;
Audioread:
It read the audio files.