0% found this document useful (0 votes)
15 views20 pages

Session 7 PPT Loop

The document provides an overview of MATLAB control statements, including if-else, switch, while, and for loops, along with examples for solving quadratic equations, determining even or odd numbers, and calculating factorials. It also discusses increment and comparison operators, boolean expressions, and naming conventions for MATLAB functions and scripts. Additionally, it presents a programming problem involving salary calculations for two friends with different raise percentages, demonstrating solutions using both while and for loops.

Uploaded by

keraibhavnik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views20 pages

Session 7 PPT Loop

The document provides an overview of MATLAB control statements, including if-else, switch, while, and for loops, along with examples for solving quadratic equations, determining even or odd numbers, and calculating factorials. It also discusses increment and comparison operators, boolean expressions, and naming conventions for MATLAB functions and scripts. Additionally, it presents a programming problem involving salary calculations for two friends with different raise percentages, demonstrating solutions using both while and for loops.

Uploaded by

keraibhavnik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

MATLAB Control Statements

Content:
• Introduction to MATLAB
• Solution using Loop Programming
Programming
Control Structures
 if Statement
if condition,
then-body;
elseif condition,
elseif-body;
else
else-body;
end

The else and elseif clauses are optional.


Any number of elseif clauses may exist.
Make a programme for solution of quadratic equation to find
its real roots

clear all
disp ('The Programme gives possible real roots for a Quadratic Equation')
disp ('Input Constants a, b and c for Quadratic Equation a x2 + b x + c =0')
a = input('input value of a');
b = input('input value of b');
c = input('input value of c');
d = b*b-4*a*c;
if d<0
disp ('The roots are not real')
else
Root1 = -b+sqrt(d)/(2*a)
Root2 = -b-sqrt(d)/(2*a)
end
If-else Statement
Programming
Control Structures
 switch Statement
switch expression
case label
command-list;
case label
command-list;
...
otherwise
command-list;
end

Any number of case labels are possible.


Make a programme to find
input number as even or odd

n = input('Enter a integer number: ');


o = 2*(round(n/2)-n/2);
switch (o)
case 1
disp (‘Odd Number')
case 0
disp ('Even Number')
otherwise
disp ('Invalid Input')
end
Programming
Control Structures
 while Statement
while condition,
body;
end

 for statement

for var = expression,


body;
end
Make a programme to
determine factorial of factorial command can be used
alternative to validate the Output
given number
clear all
disp ('This programme calculates
the factorial of number')
N = input('enter number for
determination of its factorial\n');
A=1;
for i = 1:N
A = A*i;
end
disp('Factorial of Number required
number N is');
A
Programming
Interrupting and Continuing Loops
 break
Jumps out of the innermost for or while loop that encloses it.

 continue
Used only inside for or while loops. It skips over the rest of the
loop body, causing the next cycle to begin. Use with care.
Programming
Increment Operators
Increment operators increase or decrease the value of a
variable by 1.
i++ Increment scalar i by 1
i-- Decrement scalar i by 1
A++ Increment all elements of matrix A by 1
v-- Decrement all elements of vector v by 1

There are the C/C++ equivalent operators ++i , --A .


Programming
Comparison Operators
 All of comparison operators return a value of 1 if the comparison is
true, or 0 if it is false.
Examples: i == 6, cond1 = (d > theta)

 For the matrix-to-matrix case, the comparison is made on an element-


by-element basis. Example:
[1 2; 3 4] == [1 3; 2 4] returns [1 0; 0 1]

 For the matrix-to-scalar case, the scalar is compared to each element


in turn. Example:
[1 2; 3 4] == 2 returns [0 1; 0 0]
Programming
Comparison Operators
 any(v) Returns 1 if any element of vector v is non-zero
(e.g. 1)
 all(v) Returns 1 if all elements in vector v are non-zero
(e.g. 1)

For matrices, any and all return a row vector with elements
corresponding to the columns of the matrix.
 any(any(C)) Returns 1 if any element of matrix C is non-zero
(e.g. 1)
 all(all(C)) Returns 1 if all elements in matrix C are non-
zero (e.g. 1)
Programming
Relational Operators
 x < y True if x is less than y
 x <= y True if x is less than or equal to y
 x == y True if x is equal to y
 x >= y True if x is greater than or equal to y
 x > y True if x is greater than y
 x ~= y True if x is not equal to y
Programming
Boolean Expressions
 B1 & B2 Element-wise logical and
 B1 | B2 Element-wise logical or
 ~B Element-wise logical not

Short-circuit operations: evaluate expression only as long as needed


(more efficient).
 B1 && B2 Short-circuit logical and
 B1 || B2 Short-circuit logical or
Programming
Recommended Naming Conventions
 Underscore-separated or lowercase notation for functions
Examples: intersect_line_circle.m, drawrobot.m,
calcprobability.m

 UpperCamelCase for scripts


Examples: LocalizeRobot.m, MatchScan.m

 Note: Matlab commands are all in lowercase notation (no underscores


or dashes)
Examples: continue, int2str, isnumeric
Programming- Problem
 Two friends A and B start with initial salaries of 1 unit and 1.25 unit,
respectively. At the end of each year, they get a raise of 6% and 2%
respectively. Write a MATLAB code that uses either a for or while loop
to calculate annual salaries of the two until the year when A’s salary
exceeds that of B’s. Report the value of the earliest year n when A’s
salary exceeds B’s. When the program ends, A and B should be a n
dimensional vectors, containing their salaries in the respective years.
Programming- Solution
 Code of salary.m (Method 1: Using “while” loop)

% Program using while loop

salA(1) = 1;

salB(1) = 1.25;

n = 1;

while(salA<salB)

salA(n+1) = (1.06) * salA(n);

salB(n+1) = (1.02) * salB(n);

n = n + 1;

end

 Final Result: Number of years = 7 (6 years later, i.e., in the 7th year)
Programming- Solution
Code of salary.m (Method 2: Using “for” loop)

% Program using for loop

salA(1) = 1;

salB(1) = 1.25;

maxYears = 20;

for n=2:maxYears

salA(n) = (1.06) * salA(n-1);

salB(n) = (1.02) * salB(n-1);

if (salA(n+1)>salB(n+1) )

break

end

end

Final Result: Number of years = 7 (6 years later, i.e., in the 7th year)
Exercise problem for IF-ELSE

Observed N
Value
NR

Cohessionless
Cohesive Soil
Soil
Nc = 0.77 log10(2000/S)×N No Corrections Required

Above Water
Below Water
Table
N = Nc

NR < 15 NR > 15
N = Nc N = 15 + 0.5 × (Nc - 15)

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