Experiment 02
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.
For example, a simple loop will go around four times:
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: