Signals and System Lab3
Signals and System Lab3
LAB NO : 03
SUBMITTED BY : MAHNOOR
BATCH : AVIONICS 07
SECTION : B
Marks Obtained :
Remarks:
DEADLINE:
DATE OF SUBMISSION:
Working With Loops:
For Loop:
Generating numbers 1 to 10 in ascending order using for loop.
Type the following code in your editor, save and run and check the output.
It should generate numbers from 1 to 10.
OUTPUT:
Task 2. What is the use of semicolon at the end of each statement?
What happens if you put a semi colon and if you don’t?
In MATLAB, the SEMI COLON (;) at the end of an expression acts as a command separator
and presses output to the command window. Inserting a breakpoint executes the statement
without specifying an output, which is useful for suppressing large or intermediate results.
Unchecking the comma will display the output of the statement in the command window,
which can be useful for debugging when you need immediate feedback..
While loop:
Generating numbers 1 to 10 in ascending order using while loop.
Type the following code in your editor, save and run and check the output.
It should generate numbers from 1 to 10.
Task 4. What is the difference between for and while loop? Why
use either one if you can generate the same output using either for
or while loop.
Both for and while loops are used in programming to iterate over a sequence of instructions
multiple times, but they have differences in syntax and usage.
For Loops:
1. Designed for scenarios where the number of iterations is known or easily determined.
2. The syntax includes initialization, conditional checking, and repeated increment/decrement
in one line.
3. Usually used to iterate over sequences such as lists, repeats, or ranges.
4. Syntax example: `for (var i = 0; i < 10; i++) { // code }`.
5. Best suited for situations where loop control variables are simple and well defined.
While Loops:
1. Suitable for scenarios where the loop continues until a certain condition is false.
2. The syntax consists of a condition that is evaluated before each iteration, and the loop
continues until the condition is no longer met.
3. Provides more flexibility because the loop condition can depend on several factors such as
boolean values or existing variables.
4. Example syntax: `while (i < 10) { // code }`.
5. Enables dynamic control flow cycling under changing conditions.
Main differences:
1. Control Flow:For loops are more structured and follow a fixed pattern of initialization,
condition checking, and iteration, while for loops offer more flexibility in defining the
conditions of the loop.
2. Readability: It is recommended for loops if the number of iterations is known or if iterating
over sequences, because the syntax is short and expressive. Loops are more suitable for
scenarios where loop control depends on dynamic conditions or existing variables.
3. Variable Creation: For loops, a new loop control variable must be created in the loop
initialization statement. Loops, on the other hand, allow existing variables to be used directly
within the loop conditions.
In summary,while both for and loops are designed to iterate over blocks of code, they differ in
syntax, usage scenarios, and readability, which is more suitable for the specific requirements
of the program.
while both loops can accomplish similar tasks, the choice between them depends on the
specific requirements of the program and the readability of the code they are designed for
different scenarios and can lead to code that is more readable and maintainable when used
appropriately.
OUTPUT:
Task 6. Plot x[n]=cos[n] for -roll number<= t <= roll number.
(Hint: stem command is used for plotting in discrete time)
INPUT:
n=linspace(-2,2,26);
x=cos(n);
stem(n,x)
title("cos function for discrete time signal")
OUTPUT:
OUTPUT:
CONCLUSION:
• From this lab report, we have learnt to use for and while loops, how to apply them in
different scenarios and how to avoid infinite loop, where while loop is used and where for
loop is used.
• We have also learnt how to plot continuous time signal and how to plot discrete time
signal on MATLAB.
• We have also learnt to plot real and imaginary part of signals on MATLAB.