Chapter#5
Chapter#5
MATLAB
A computer program is a sequence of computer commands.
In a simple program the commands are executed one after the other in the order they are typed
but in many situations, commands are not necessarily executed in the order they are typed.
In other situations there might be a need to repeat a sequence of commands several times
within a program.
RELATIONAL AND LOGICAL OPERATORS
where the ‘s’ inside the command defines the characters that will be entered as a string. In this
case when the prompt message appears, the text is typed in without the single quotes, but it is
assigned to the variable as a string.
Using the fprintf command to display a mix of text and numerical data:
Examples:
Refer help window and text book for formatting fprintf
Conversion specifications
Escape characters
CONDITIONAL
STATEMENTS
A conditional statement is a command that allows MATLAB to make a decision of whether
to execute a group of commands that follow the conditional statement, or to skip these
commands.
In a conditional statement expression .
If the expression is true, a group of commands that follow the statement are
executed.
If the expression is false, the computer skips the group.
if logical expression
Statements
end
Every if statement must have an accompanying end statement. The end statement marks the
end of the statements that are to be executed if the logical expression is true.
The structure of the if-end conditional statement.
The if-else-end Structure
The basic structure for the use of the else statement is
if logical expression
statement group 1
else
statement group 2
end
Example
clc;
clear;
x=input('enter the number:' );
if x >=0
y=sqrt(x);
fprintf('Square root of %d is %f\n',x,y);
else
disp('number is negative');
end
The if-elseif-else-end Structure
if logical expression 1
if logical expression 2
statements
end
end
can be replaced with the more concise program
Clear; Clear;
p=input('enter a value'); p=input('enter a value');
if p>5 if p>5
y=1; y=1;
elseif p>10 if p>10
y=2; y=2;
end end
disp(y); end
disp(y);
THE switch-case STATEMENT
The switch-case statement is another method that can be used to direct the
flow of a program. It provides a means for choosing one group of commands
for execution out of several possible groups.
The switch structure provides an alternative to using the if, else if and else
commands. Anything programmed using switch can also be programmed
using if structures.
However, for some applications the switch structure is more readable than
code using the if structure.
The structure of a switch-case statement.
Example
clc;
clear;
m = input('Enter a Month(1-12): ');
switch m
case {1 2 3 4 5}
disp('Spring')
case {6 7}
disp('Summer')
case {8 9 10 11 12}
disp('Fall')
otherwise
disp('Bad Month Index')
end
LOOPS
A loop is another method to alter the flow of a computer program. In a loop, the execution of a
command, or a group of commands, is repeated several times consecutively.
for-end Loops
In for-end loops the execution of a command, or a group of commands, is
repeated a predetermined number of times.
The structure of a for-end loop.
for k = f : s : t
........ A group of MATLAB
commands.
end
In the first pass k = f and the computer executes the commands between the
for and end commands.
Then, the program goes back to the for command for the second pass.
k obtains a new value equal to k = f + s, and the commands between the for
and end commands are executed with the new value of k.
The process repeats itself until the last pass, where k = t.
Then the program does not go back to the for, but continues with the
commands that follow the end command.
A simple example of a for-end loop (in a script file) is:
For k=1 : 3 : 10
x = k^ 2
end
When this program is executed, the loop is executed four times.
The value of k in the four passes is k = 1, 4, 7, and 10,
which means that the values that are assigned to x in the passes are
x = 1, 16, 49, and 100, respectively.
A simple example of a for-end loop, Execute and discuss the working
of for loop
clc;
clear;
x=input('Enter a Number:');
n=5;
for(i=1:n)
y=x+i;
disp(y);
end
clc; clear;
n=input('Enter the number of terms: ' );
S=0; %Setting the sum to zero.
for k=1:n
S=S+(-1)^k*k/2^k; %for-end loop.
end
fprintf('The sum of the series is: %f\n',S)
In each pass one element of the series is calculated and is added to the sum of the
elements from the previous passes.
while-end Loops
while-end loops are used in situations when looping is needed but the number of passes is not known in
advance.
In while-end loops the number of passes is not specified when the looping process starts.
Instead, the looping process continues until a stated condition is satisfied.
clc; clear;
n=5;
i=1;
x=input('Enter Your name:','s' );
while(i<=n)
disp(x);
i=i+1;
end
Print numbers, its square and its square roots n times Using While loop.
clc;
clear;
n=input('limit:');
i=1;
fprintf('Number\tSquare\tSquareRoot\n');
while(i<=n)
a=sqrt(i);
b=i^2;
fprintf('%d\t\t%d\t\t%g\n',i,b,a);
i=i+1;
end
clc;clear;
n=input('limit:');
i=1;
fprintf('Number\tSquare&SquareRoot\n');
while(i<=n)
if (rem(i,2)==0)
a=sqrt(i);
fprintf('%d\t\t%g\n',i,a);
i=i+1;
else
a=i^2;
fprintf('%d\t\t%g\n',i,a);
i=i+1;
end
end
NESTED LOOPS AND NESTED CONDITIONAL STATEMENTS
Loops and conditional statements can be nested within other loops or conditional statements.
This means that a loop and/or a conditional statement can start (and end) within another loop or conditional
statement.
There is no limit to the number of loops and conditional statements that can be nested.
It must be remembered, however, that each if, case, for, and while statement must have a corresponding
end statement.
Structure of nested loops
Example: Creating a matrix with a loop
clc;
clear;
m=input('row:');
n=input('column:');
for i=1:m
for j=1:n
x(i,j)=input('enter elements:');
end
end
disp(x);
THE break AND continue COMMANDS
The break command:
When inside a loop (for or while), the break command terminates the execution of the loop (the
whole loop, not just the last pass).
When the break command appears in a loop, MATLAB jumps to the end command of the loop
and continues with the next command (it does not go back to the for command of that loop).
clc;clear;
for i=1:5
a=input('enter your no: ');
if(a<0) %exit your loop when
any negative no. input
break;
end
fprintf('Number is %d\n',a);
end
The continue command:
The continue command can be used inside a loop (for or while) to stop the present pass and start the next pass
in the looping process.
The continue command is usually a part of a conditional statement.
When MATLAB reaches the continue command, it does not execute the remaining commands in the loop, but
skips to the end command of the loop and then starts a new pass.
clc;clear;
for i=1:5
a=input('enter your no: ');
if(a<0) %exit your loop when any
negative no. input
continue;
end
n=sqrt(a);
fprintf('Square root is %f\n',n);
end