0% found this document useful (0 votes)
7 views2 pages

Lab 2

Lab 2 focuses on learning advanced MATLAB commands, particularly dealing with loops and writing mathematical expressions. It covers the syntax for basic mathematical operations and demonstrates the use of for and while loops for iterating over arrays and performing calculations. The lab aims to familiarize students with MATLAB's capabilities in handling repetitive tasks and mathematical computations.

Uploaded by

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

Lab 2

Lab 2 focuses on learning advanced MATLAB commands, particularly dealing with loops and writing mathematical expressions. It covers the syntax for basic mathematical operations and demonstrates the use of for and while loops for iterating over arrays and performing calculations. The lab aims to familiarize students with MATLAB's capabilities in handling repetitive tasks and mathematical computations.

Uploaded by

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

Lab2 Signals and Systems Spring 2025

Lab 2:
“Learning, writing mathematical expressions, familiarizing with some more
advanced matlab commands and/or dealing with loops”

Objective of the Lab:


The objective of this lab is to learn some more matlab commands dealing with Loops in MATLAB.

Procedure:
Writing mathematical expressions:
MATLAB is an excellent tool for performing mathematical operations and calculations. To write
mathematical expressions in MATLAB, one must become familiar with its syntax and operators.
Some of the commonly used operators are +, -, *, /, ^ (exponent), and sqrt (square root).
For example, to calculate the square root of x, one could write:
sqrt(x)
To calculate the value of sin(x), one could write:
sin(x)
To calculate the sum of two variables, x and y, one could write:
x+y

Dealing with loops


Loops are a fundamental part of programming, and MATLAB provides several types of loops,
including for and while loops. Loops can be used to perform repetitive tasks, such as iterating
over an array or performing a calculation for a set of inputs.
For Loop
For example, to iterate over an array of values and calculate their squares, one could write:
array=[1,2,3]
for i = 1:length(array)
array_squared(i) = array(i)^2
end
OR
% Define an array
Lab2 Signals and Systems Spring 2025

A = [1, 2, 3, 4, 5];
% Square the array element-wise
B = A .^ 2;
% Display the result
disp('Squared array:');
disp(B);
Explanation:
 The .^ operator performs element-wise exponentiation.
 disp is used to display the squared array.
While Loop
Alternatively, to perform a calculation until a certain condition is met, one could write:
% Initialize a counter
n = 1;
% Define the stopping condition
while n <= 5
fprintf('Iteration %d\n', n);
% Increment the counter
n = n + 1;
end
Explanation:
 The loop runs as long as n <= 5.
 The counter n is incremented in each iteration to avoid an infinite loop
 fprintf: This function prints text to the command window.
 Iteration: A plain text string that gets printed as is.
 %d: A placeholder for an integer value. It gets replaced by n in each loop iteration.
 \n: A newline character, which moves the cursor to the next line after printing.
 n: This specifies that n should replace %d in the format string.

You might also like