Вы находитесь на странице: 1из 21

Welcome to my Presentation

Presentation Topics:
How to Learn MATLAB

Presented by Dipankar Kumar Sarkar


Lecturer
Department of Mathematics
BSMRSTU,Gopalganj-8100,Bangladesh
MATLAB FUNDAMENTALS
Display Function
There are two straight forward ways of getting output from
MATLAB:
by entering a variable name, assignment or expression
on the command line, without a semicolon;
with the disp statement, e.g. disp( x ).
The general form of disp for a numeric variable is
disp([ ‘variable’] )
Example:
disp([‘Bangabandhu Sheikh Mujibur Rahman Science & Technology University’]);

>> Bangabandhu Sheikh Mujibur Rahman Science & Technology University


The num2str() and int2str() functions
x = 2;
disp([‘The answer is =‘ num2str(x)])
The output should be:
The answer is 2
Examples:
x = 23.11;
>> disp( [ 'answer = ' num2str(x) ] )
answer = 23.11
>> disp( [ 'answer = ' int2str(x) ] )
answer = 23
Scale factors (Changing the data format)

>> x= 12.345678901234567;
format short, x  12.3457
format long, x  12.34567890123457
format short e, x  1.2346e+001
format long e, x  1.234567890123457e+001
format short g, x  12.346
format long g, x  12.3456789012346
format rat, x  1000/81
Control Structures
1. Conditional Expressions
 Relational Operators  Logical Operators
 < less than  & and
 <= less than or equal to  | or
 > greater than  ~ not
 >= greater than or equal to
 == equal to
 ~= not equal
 True and False
 1 True
 0 False
Programming in MATLAB
Conditional Control
- if, else, elseif
- switch case

Loop Control
- for, while, continue, break

Error Control
- try, catch

Program Termination
- return
Conditional Control
The general form of if Statement in a single line is:
if (logical condition)
statements
end
A MATLAB Program (using if statement) After Execution Program:
clear all marks=45
clc Result =Pass
marks = input('marks=');
if (marks>=40)
Result=char([‘Pass'])
end
Conditional Control
The general form of if else Statement is as follows:
if (logical condition)
statements A
else
Statements B
end
Note:
Statements A and Statements B represent one or more
statements.
 If condition is true, Statements A are executed, but if
condition is false, Statements B are executed.
The else part is optional.
Conditional Control
A MATLAB Program (if else statement) After Execution Program:

clear all
clc
marks = input('marks=');
if (marks>=40)
Result=char([‘Pass'])
else
Result=char([‘Fail'])
end
Conditional Control
The general form of if elseif else Statement is as
follows:
if (logical condition1)
Statements A
elseif (logical condition2)
Statements B
elseif (logical condition3)
Statements C
.........................................
else
Statements E
end
Conditional Control
This is sometimes called an elseif ladder. It works as follows:

 Logical condition1 is tested. If it is true, statements A are executed; MATLAB


then moves to the next statement after end.
 If logical condition1 is false, MATLAB checks logical condition2. If it is true,
statements B are executed, followed by the statement after end.
 In this way, all the conditions are tested until a true condition is found. As soon
as a true condition is found, no further elseifs are examined, and MATLAB
jumps off the ladder.
 If none of the conditions is true, statements E after else are executed.
 You should arrange the logic so that not more than one of the conditions is
true.
 There can be any number of elseifs, but at most one else. elseif must be
written as one word.
 It is good programming style to indent each group of statements as shown.
Conditional Control
A MATLAB Program
(if elseif else statement)
clear all Letter_Grade=char('C')
clc elseif(x>=40 & x<=44)
x=input(['x=']); Letter_Grade=char('D')
if(x>=80 & x<=100) else
Letter_Grade=char('A+') Letter_Grade=char('F')
elseif(x>=75 & x<=79) end
Letter_Grade=char('A')
elseif(x>=70 & x<=74) After Execution MATLAB Program
Letter_Grade=char('A-')
elseif(x>=65 & x<=69)
x=99
Letter_Grade=char('B+') Latter_Grade =A+
elseif(x>=60 & x<=64)
Letter_Grade=char('B')
elseif(x>=55 & x<=59)
Letter_Grade=char('B-')
elseif(x>=50 & x<=54)
Letter_Grade=char('C+')
elseif(x>=45 & x<=49)
Conditional Control
The general form of switch case Statement is as follows:
switch expression
case test expression 1
commands
case {test expression 2, test expression 3}
commands
………………………………………………….
otherwise
commands
end
The expression result is compared in turn to the result of each case test
expression. If they are equal, then the commands following the case command
are executed and processing continues with the command following the end
statement. If expression is a character string, then a string comparison is made
with the case test expression. Multiple test expressions can be listed, comma
separated, enclosed in braces {}.
Conditional Control
A MATLAB Program
(switch case statement)
A MATLAB Program After Execution MATLAB Program
(switch case statement)
Clear all d=12
clc Even
d=input('d=')
%d = floor(10*rand);
switch d;
case {2, 4, 6, 8}
disp('Even');
case {1, 3, 5, 7, 9}
disp('Odd');
otherwise
disp('Zero');
end
Loop Control
The general form of for loop is as follows:
for index= start : increment :end
statements
end
Note:
 index, start, increment and end do not need to be integer
valued
 increment is optional, if increment is not specified
increment defaults to 1
 index can be incremented positive (increment > 0) or
negative (increment < 0)
 loop stops when index > end (or index < end)
Loop Control
A MATLAB Program (for loop) After Execution MATLAB Program
Loop Control
The general form of a WHILE statement is:

WHILE expression
statements
END

The statements are executed while the real part of the


expression
has all non-zero elements. The expression is usually the
result of
expression (relational operator) expression where
relational operator is ==, <, >, <=, >=, or ~=.
Find the first integer n for which factorial(n) is a 100-digit number:

n = 1;
nFactorial = 1;
while nFactorial < 1000
n = n + 1;
nFactorial = nFactorial * n;
end

Вам также может понравиться