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

MATLAB-5

Uploaded by

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

MATLAB-5

Uploaded by

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

contd…

Programming In MATLAB

BSCpE 4
Flow Control

2
Flow Control
• MATLAB has several flow control constructs:
– if

– switch and case

– For

– while

– Continue

– Break

– try – catch

– return

3
IF-Statement

• The if statement evaluates a logical expression and executes a group of


statements when the expression is true.

• The optional elseif and else keywords provide for the execution of alternate
groups of statements.

• An end keyword, which matches the if, terminates the last group of
statements.

4
IF-Statement

• The if statement evaluates a logical expression and executes a group of


statements when the expression is true.

• The optional elseif and else keywords provide for the execution of alternate
groups of statements.

• An end keyword, which matches the if, terminates the last group of
statements.

5
IF-Statement
• For Example write a program that accepts a number and displays Good if
number is greater than 4 and displays Bad when number is less than 4 and
displays Exactly when number is equal to 4.

A=input('Enter any number = ');


if A>4
disp('Good')
elseif A<4
disp('Bad')
else
disp(‘Exactly')
end
6
Activity#1
1. Write Down MATLAB Program to indicate that the entered
number is even or odd.

2. Write Down a MATLAB Program that accepts value of


Damping ratio (ζ) and computes maximum overshoot of the
system. Also it should display following messages.
– The system is over damped if ζ>1.

– The system is under damped if ζ<1.

– The system is critically damped if ζ=1.


−
1− 2
M =e 7
For-Statement
• The for loop repeats a group of statements a fixed,
predetermined number of times.

for I = 1:N,

for J = 1:N,

end

8
Nested For-Statement
• We can also insert another for statement inside existing for
statement.

for I = 1:N,

for J = 1:N,

A(I,J) = 1/(I+J-1);

end

end

9
For-Statement
• Write a MATLAB code to print table of entered number.

n=input(’Enter your desired number = ’)

for I = 1:n,

sprintf( ’%g X %g = %g’,I,n,I*n)

end

10
Activity #2
1. Write Down the MATLAB program to print first 20 odd
numbers.

2. Compute the Fourier series of square wave up to first five terms


and display graph, consider ω=1.

4 1 1 1 1
f (t ) = (cos t − cos 3t + cos 5t − cos 7t + cos 9t )
 3 5 7 9

11
Switch and Case
• The switch statement executes groups of statements based on the
value of a variable or expression.

R=input('Enter the component name = ','s');


switch R
case ('resistor')
disp('12 resistors left')
case ('capacitor')
disp('20 capacitors left')
end 12
Activity #3
1. Write Down Matlab program using switch case statement to
achieve following objectives.
• It accepts three inputs from the keyboard
▪ ovsht
▪ stime
▪ ptime
• After accepting one of these inputs jump to corresponding case
and calculate and display the given parameter consider ζ=0.1.
−
 3
tp = M =e 1− 2 ts =
4 5
13
Activity #4
1. Write a MATLAB code to print table of entered number.

2. Write Down MATLAB Program that accepts a numeric value


from the keyboard and Compares this number to a randomly
generated number and indicates
– The number is greater if entered number is greater

– The number is smaller if entered number is smaller

– Great Shot if entered number is equal

14
While-Statement
• The while loop repeats a group of statements an indefinite
number of times under control of a logical condition.
• A matching end delineates the statements.

a=input('Enetr any number = ');

while a<30

disp('you are still away')

a=input('Enter number again = ');

end
15
Exercise#4 (contd… )
3. Now modify this program in such a way that computer also
indicates number of attempts to guess the number .

16
Functions

17
Functions
• Functions are M-files that can accept input arguments and return
output arguments.

• The names of the M-file and of the function should be the same.

• Functions operate on variables within their own workspace,


separate from the workspace you access at the MATLAB
command prompt.

18
Functions
• The first line of a function M-file starts with the keyword function. It
gives the function name and order of arguments.

• The next several lines, up to the first blank or executable line, are
comment lines that provide the help text. These lines are printed when
you type

>> help <function name>

• The first line of the help text is the H1 line, which MATLAB displays
when you use the lookfor command or request help on a directory.

• The rest of the file is the executable MATLAB code defining the
function.
19
Functions
• Lets create a function that accepts one numeric value and notify that the
number is even or odd.

function evenodd(n)

if mod(n,2)==0

disp('The number is Even')

elseif mod(n,2)==1

disp('The number is Odd')

end
20
Functions
function evenodd(n)

% function evenodd(n) displays whether n is even or odd

% Example evenodd(7)

% Matlab Returns

% The number is Odd

if mod(n,2)==0

disp('The number is Even')

elseif mod(n,2)==1

disp('The number is Odd')


21
end
Activity #5

1. Develop a MATLAB function with one input argument


table(n) that display table of n up to 10 numbers.

2. Develop a MATLAB function with two input arguments


stable(n,e) that display table of n up to e.

22
Activity #6
1. Develop a MATLAB function with one input argument
trig(n) that produce sine and cos vlaues as two output
arguments .

2. Develop a MATLAB function that calculates transient


response parameters overshoot (M), settling time (ts) and
rise time (tr).

>> [M ts tr]= transient(b,wn)


 −
3
tp = 1− 2 ts =
4 M =e n 23
Functions with output arguments
1. Let’s Develop a MATLAB function that accepts two numbers
and produce sum and difference of these numbers.

function [sum diff]=sumdiff(a,b)


% function sumfidd(a,b) produces sum and difference of a and b.
sum=a+b;
diff=a-b;
To execute it
>> [s d]=sumdiff(6,7)

24
Activity #6 (contd….)
3. Develop a MATLAB function that accepts a symbolic
expression of one variable and returns its integral and
derivative.

4. Develop a MATLAB function that accepts a symbolic


expression of one variable and plot graphs of its integral
and derivative.

25

You might also like