0% found this document useful (0 votes)
75 views6 pages

LAB2

This document introduces various control flow statements in MATLAB including if/else, for loops, while loops, and switch statements. It also covers some fundamental signal sequences like unit step functions, unit impulse sequences, and sinusoidal sequences. It provides syntax examples and activities to generate and plot these sequences in MATLAB.

Uploaded by

Ali Shan
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)
75 views6 pages

LAB2

This document introduces various control flow statements in MATLAB including if/else, for loops, while loops, and switch statements. It also covers some fundamental signal sequences like unit step functions, unit impulse sequences, and sinusoidal sequences. It provides syntax examples and activities to generate and plot these sequences in MATLAB.

Uploaded by

Ali Shan
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/ 6

LAB#2

INTRODUCTION TO CONTROL FLOW


STATEMENTS & SIGNAL SEQUENCE

1. INTRODUCTION TO CONTROL FLOW STATEMENTS:


MATLAB has the following flow control constructs:
a. if statements
b. switch statements
c. for loops
d. while loops
e. break statement

The if, for, switch and while statements need to terminate with an end statement.

1.1 WHILE LOOP:

while loop to repeat when condition is true

Syntax:

while expression
statements
end

1.1.1 NESTED WHILE LOOP

Syntax:

While <expression1>

While <expression2>

<statements>

end

end
1.2 IF LOOP

Execute statements if condition is true

Syntax:

if expression
statements
elseif expression
statements
else
statements
end
1.3 FOR Loop:

for loop to repeat specified number of times

Syntax:

for index = values


statements
end

1.3.1 NESTED FOR LOOP


MATLAB allows to use one loop inside another loop.
Syntax:
The syntax for a nested for loop statement in MATLAB is as follows

For m=1:j

For n=1:k

<statements>;

end

end
1.4 SWITCH STATEMENTS:
Execute one of several groups of statements

Syntax:

switch switch_expression
case case_expression
statements
case case_expression
statements
...
otherwise
statements
end

1.5 Break:

The break statement lets you exit early from a for or a while loop.

Syntax:

while expression
statements
break expression
break
end

Example:

x = -10;
while x < 0
x = x + 2;
if x = = -2
break;
end
2. SOME FUNDAMENTAL SIGNAL SEQUENCE
2.1 UNIT STEP:
The Heaviside step function, or the unit step function, usually denoted by u, is a discontinuous
function whose value is zero for negative argument and one for positive argument

[n] = {10,,nn<0 0

Unit Step Sequence

2.2 UNIT IMPULSE SEQUENCE:

Unit Impulse sequence is very important to characterize the impulse response of the system.
Mathematically an impulse sequence is represented as:

[n] = {10,,n=0
n 0
3. SINUSOIDAL SEQUENCE:
A matlab function cos (or sin) is used to generate sinusoidal sequences.
To generate x(n) = 3cos(0.1n + /3) + 2sin(0.5 n), 0<= n<=10, we will need the following
script:

n = [0:10];

x = 3*cos(0.1*pi*n+pi/3) + 2*sin(0.5*pi*n);
ACTIVITY:
Q1. Determine a value falls within a specified range using IF Else statements.

Q2. Initialize a vector A having 20 rows. Replace the values of this vector from row 5 to row 15
by this equation 5*(n-1) using For loop.

Q3. Use a while loop to calculate factorial.

Q4. Run the MATLAB program as described in section 1.5.

Q5. Use switch-case statements to categorize the 12 months into 3 seasons i.e
summer,spring,winter.

Q6. Implement a unit step function in MATLAB and plot it, Provide the snapshot

Q7. Implement a unit impulse sequence in MATLAB and plot it, Provide the snapshot

Q8. Provide the snapshot of the following pulses using built-in MATLAB commands.

a. Rectangular pulse
b. Saw tooth
c. Trianglular
d. Square

Q9. Provide the snapshot of the program described in step 3.

Q10. Implement the following equation in MATLAB and plot.

x = sin(2*pi*f*n) + 2*sin(2*pi*f*n)
x1 = cos(2*pi*f*n) + 2*cos(2*pi*f*n)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy