Sunteți pe pagina 1din 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)

S-ar putea să vă placă și