0% found this document useful (0 votes)
18 views4 pages

LAB 2 - Manual

This document introduces functions, loops, and conditional statements in MATLAB. Functions allow grouping code to perform tasks and operate in their own workspace. There are two types of loops: for loops that iterate a specific number of times using an index variable, and while loops that iterate as long as a condition is true. Conditional statements like if-else and switch enable selecting code to execute based on conditions. Examples are provided for each.
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)
18 views4 pages

LAB 2 - Manual

This document introduces functions, loops, and conditional statements in MATLAB. Functions allow grouping code to perform tasks and operate in their own workspace. There are two types of loops: for loops that iterate a specific number of times using an index variable, and while loops that iterate as long as a condition is true. Conditional statements like if-else and switch enable selecting code to execute based on conditions. Examples are provided for each.
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/ 4

LAB 2

Introduction to MATLAB (Part 2)


Aim:
The main aim for this lab is to learn about the functionalities of functions, Loops and Conditional
Statements.
Functions:
A function is a group of statements that together perform a task. In MATLAB, functions are
defined in separate files. The name of the file and of the function should be the same.
Functions operate on variables within their own workspace, which is also called the Local
Workspace.
Functions can accept more than one input arguments and may return more than one output
arguments.
 Syntax of a function statement is –
function [out1,out2, ..., outN] = myfun(in1,in2,in3, ..., inN)
Functions can be called anywhere with the input arguments to perform any kind of task.
Loops:
With loop statements, you can repeatedly execute a block of code. There are two types of loops:

 ‘for’ statement:
for statements loop a specific number of times, and keep track of each iteration with an
incrementing index variable
 Syntax of a for statement is
for i = 1:N
commands
end
Example:
s = 10;
H = zeros(s);
for c = 1:s
for r = 1:s
H(r,c) = 1/(r+c-1);
end
end
Try to run the code and write down your observations:
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
‘while’ statement:
while statements loop as long as a condition remains true.
 Syntax of a while statement is
while (condition)
commands...
end
Example:
n = 10;
f = n;
while n > 1
n = n-1;
f = f*n;
end
Try to run the code and write down your observations:
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Each loop requires the end keyword.
Conditional Statements:
Conditional statements enable you to select at run time which block of code to execute. There are
two types of conditional statements:
if-else statement:
 Syntax of if-else statement is
if (expression)
commands...
end
Example:
a = randi(100, 1);

if a < 30

disp('small')

elseif a < 80

disp('medium')

else

disp('large')

end

Try to run the code and write down your observations:


______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
switch statement:
 Syntax of switch statement is
switch switch_expr
case case_expr1
commands ...
case {case_expr2,case_expr3}
commands ...
otherwise
commands ...
end
Example:
[dayNum, dayString] = weekday(date, 'long', 'en_US');

switch dayString
case 'Monday'

disp('Start of the work week')

case 'Tuesday'

disp('Day 2')

case 'Wednesday'

disp('Day 3')

case 'Thursday'

disp('Day 4')

case 'Friday'

disp('Last day of the work week')

otherwise

disp('Weekend!')

end

Try to run the code and write down your observations:


_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Practice tasks will now be displayed.

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