Enrollment No: 230025 Signal System
Experiment-3
AIM: Relational Operators, Loops, Functions, and MATLAB Built-in Functions
Objective:
The objective of this lab is to explore the use of relational operators, loops, functions, and
various MATLAB built-in functions such as sqrt. These tools are critical for automating
tasks, manipulating data, and performing essential mathematical operations efficiently in
MATLAB.
1. Relational Operators in MATLAB
Relational operators allow comparisons between two values and return logical results (true or
false). Below is an example of using relational operators to compare two numbers:
% Define two values
a = 5;
b = 10;
% Using relational operators
result1 = (a == b) % Check if a is equal to b
result2 = (a ~= b) % Check if a is not equal to b
result3 = (a > b) % Check if a is greater than b
result4 = (a < b) % Check if a is less than b
result5 = (a >= b) % Check if a is greater than or equal to b
result6 = (a <= b) % Check if a is less than or equal to b
output:
Page No: 1
Enrollment No: 230025 Signal System
2. for Loop in MATLAB with Matrices
We can use a for loop to iterate over elements of a matrix, perform calculations, or
manipulate the data. Below is an example where a for loop is used to compute the sum of
each row in a matrix:
3. Factorial Function using while Loop:
Create a new file in MATLAB called factorial_function.m and add the following code:
% Factorial Function using while loop
function result = factorial_function(n)
result = 1; % Initialize the result
i = 1; % Counter
while i <= n
result = result * i; % Multiply the result by i
i = i + 1; % Increment the counter
end
end
How to Use the Function:
Once you save the factorial_function.m file, you can call this function in your main script like
this:
% Example of using the factorial function
n = 5; % Example: Factorial of 5
% Call the factorial function
factorial_result = factorial_function(n);
Page No: 2
Enrollment No: 230025 Signal System
% Display the result
disp(['Factorial of ', num2str(n), ' is: ', num2str(factorial_result)]);
Explanation:
• The function factorial_function(n) calculates the factorial of a given number n using a
while loop.
• It multiplies the result by each integer from 1 to n.
• The result is displayed in your main script.
output:
4. MATLAB Built-in Functions
a. randn for Generating Random Numbers (Normal Distribution)
MATLAB’s randn function generates random numbers from a normal distribution with a
mean of 0 and a standard deviation of 1. You can create matrices of random numbers using
this function.
b. sqrt for Square Root Calculation
The sqrt function in MATLAB calculates the square root of each element in an array or
matrix. Below is an example of using the sqrt function:
Page No: 3
Enrollment No: 230025 Signal System
Conclusion:
In Lab 3, we investigated a number of crucial ideas and MATLAB built-in features,
including:
1. Relational Operators: These are crucial for conditional operations in MATLAB because
they enable comparisons between variables and return logical values.
2. Loops: The for loop was used to iterate over matrices and carry out mathematical
operations, including factorial calculations and matrix row summation.
3. MATLAB Integrated Features:
• sqrt: The square root of an array's elements was calculated using the square root
function.
Page No: 4