SNS Lab#02
SNS Lab#02
190319
BEEE-4B
EXPERIMENT 02
Objectives:
Familiarizing students with MATLAB programming
Using MATLAB for looping and decision making
Equipment required:
MATLAB installed on PCs
Background Knowledge:
MATLAB is a very powerful and well-known software package that is used in science and engineering
disciplines, for numerical computation, data analysis, and graphical visualization. It is available in almost
all platforms such as personal computers, and workstations running under several operating systems.
MATLAB contains a large collection of built-in functions and commands that are used in an interactive
mode, when you are in the command window. As soon as the name of a function or a command is typed
at the prompt in the command window, with the proper syntax, the answer is displayed immediately. But
there are two other windows, the edit window, and graphics window, which will be discussed later.
The software package is designed to use additional sets of functions that are more applicable disciplines
such as control systems, digital signal processing, communications engineering, and image processing.
There are more than 20 sets known as “toolboxes” (e.g., control toolbox, digital signal processing
toolbox, communication toolbox, and image processing toolbox). All of them run under MATLAB and
implement the functions based on matrix manipulation of numerical data, and that is why the software is
called MATLAB (matrix laboratory).
MATLAB allows us to construct new functions using the enormous number of built-in functions,
commands, and operations in MATLAB and in the many toolboxes, without having to know how to
compile, link, load, and create executable code, because MATLAB uses its own language to carry out all
these steps, which are invisible to the user. It carries out the steps and gives the answer very fast.
Loops:
There are two prominent types of loop in MATLAB. These are:
i. for loop
ii. while loop
With loop control statements, you can repeatedly execute a block of code. Each loop starts with the loop
keyword such as “for” or “while” and end keyword is required to terminate the respective loop.
for Loop:
The "for" loop allows us to repeat certain commands. Basically, for statements loop a specific number of
times, and keep track of each iteration with an incrementing index variable.
Once MATLAB reads the "end" statement, it will loop through and print out ‘j’ each time.
For another example, if we define a vector and later want to change the entries, we can change each
individual entry using a for loop as used in the given example.
while Loop:
while statements loop as long as a condition remains true. For example:
Length of a Vector:
The command length(A) is used to find the length of a vector A. For example:
To save this length in a variable, just assign this expression to a variable. For example:
Size of a Matrix:
The command size() returns the size of a matrix in terms of number of rows and columns.
Nested Loops:
It is a good practice to indent the statements especially while using a nested loop. Nested loops are often
used in different programming problems. An example of a nested for loop is given below:
if Statements:
if statement evaluates an expression and executes a group of statements when the expression is true. An
expression is true when its result is non-empty and contains only non-zero elements. Otherwise, the
expression is false. The general form of the IF statement is:
if expression
statements
elseif expression
statements
else
statements
end
The else and elseif conditions are optional and depend on the type of functionality required out of the
code.
If size(A) and size(B) are the same, concatenate the arrays; otherwise, display a warning and return an
empty array.
The result in the command window is:
Lab Task:
1. Let “x” and “y” be two randomly generated row vectors. Create a new data array “z”, based on
the following set of rules:
a. If x = 0 and the value y lies within 52-336, then value of z will be 0. The value of z will be 1
if y is less than 52 or more than 336.
b. If x = 1 and the value y lies within 38-176, then value of z will be 0. The value of z will be 1
if y is less than 38 or more than 176.
2. Generate the following vector using for loop:
3. Declare a vector A consisting of 9 elements. Use for loop to find the product of all the elements
of the vector and store the result in a variable ‘z’. Subtract 12 from z and store the final result in
“c”.
4. Use a while loop to print out the square of integers from 1 up to 20.
5. Write a short MATLAB program using loops to compute the first 100 Fibonacci numbers.
Command Window:
Total numbers could not be displayed so the start and end is displayed here
Conclusion:
Thus in this lab we understood the comprehensive details about the commands of
loops and we also got to know how to perform different actions using loops, basically
loops i.e. if else, for, while etc., all are used for the ease of coding in MATLAB and this
makes it easy for us to implement things and get the desired output or result.