Академический Документы
Профессиональный Документы
Культура Документы
Presentation Topics:
How to Learn MATLAB
>> 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:
WHILE expression
statements
END
n = 1;
nFactorial = 1;
while nFactorial < 1000
n = n + 1;
nFactorial = nFactorial * n;
end