0% found this document useful (0 votes)
4 views

Experiment 2

The document is a lecture on control flow and operators in MATLAB, covering decision-making structures such as 'if...end', 'for...end' loops, and relational operators. It provides examples of using 'if' statements with the quadratic formula and illustrates the 'for' loop syntax. Additionally, it includes exercises for plotting signals based on given conditions.

Uploaded by

rasha waleed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Experiment 2

The document is a lecture on control flow and operators in MATLAB, covering decision-making structures such as 'if...end', 'for...end' loops, and relational operators. It provides examples of using 'if' statements with the quadratic formula and illustrates the 'for' loop syntax. Additionally, it includes exercises for plotting signals based on given conditions.

Uploaded by

rasha waleed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

University of Ninevah

College of Electronics Engineering


Electronic Engineering Department
2 nd Year

LECTURE (2): CONTROL FLOW & OPERATOR


Introduction

MATLAB is also a programming language. Like other computer programming languages,


MATLAB has some decision making structures for control of command execution. These
decision making or control flow structures include for loops, while loops, and if-else-end
constructions. Control flow structures are often used in script M-files and function M-files.

1. The ``if...end'' structure

MATLAB supports the variants of \if" construct.

1) if ... end

2) if ... else ... end

3) if ... elseif ... else ... end

The simplest form of the if statement is

if expression (condition)
statements
end
-----------------------
Here are some examples based on the familiar quadratic formula.
a=input('enter value of a=');
b=input('enter value of b=');
c=input('enter value of c=');

1.
discr = b*b - 4*a*c;
if discr < 0
disp('Warning: discriminant is negative, roots are imaginary');
end

2.
discr = b*b - 4*a*c;
if discr < 0
disp('Warning: discriminant is negative, roots are imaginary');
else
University of Ninevah
College of Electronics Engineering
Electronic Engineering Department
2 nd Year

disp('Roots are real, but may be repeated')


end
3.
discr = b*b - 4*a*c;
if discr < 0
disp('Warning: discriminant is negative, roots are
imaginary');
elseif discr == 0
disp('Discriminant is zero, roots are repeated')
else
disp('Roots are real')
end

It should be noted that:


1- elseif has no space between else and if (one word)
2- no semicolon (;) is needed at the end of lines containing if, else, end
3- indentation of if block is not required, but facilitate the reading.
4- the end statement is required

A relational operator compares two numbers by determining whether a comparison is true or


false. Relational operators are shown in Table

2- The ``for...end'' loop

In the for ... end loop, the execution of a command is repeated at a fixed and predeter-
mined number of times. The syntax is
University of Ninevah
College of Electronics Engineering
Electronic Engineering Department
2 nd Year

for variable = initial : step : final


statement(s)
end

Usually, expression is a vector of the form i:s:j. A simple example of for loop is
for ii=1:5
x=ii*ii
end

for example
n=5
a=eye(n)
for j=2: n
for i =1: j-1
a(i,j)=i/j
a(j,i)=i/j
end
end

Experiments

Q1) find and plot the signal x(t) for the system shown

x(t)= 4 t + 1 if t > 0
=t-5 if t< -1

Q2) find and plot the signal x(n) for the system shown:

x(t)= 2 n+ 1 n= 0
= n- 3 n< -1
= n+ 3 -3<n<2
= 0.5 n + n 5< n

You might also like