A logical expression is a statement that can either be true or false. For example,
\( a \ge b \) is a logical expression. It can be true or false depending on what values
of a and b are given. In matlab, a logical expression
that is true will compute to the value “TRUE,” which is equivalent to 1. A false expression
will compute to the value “FALSE,” which is equivalent to 0.
Comparison operators compare the value of two numbers, and they are used to build logical expressions.
matlab reserves the symbols >, >=, <, <=, ~=, ==, to denote
“greater than,” “greater than or equal,” “less than,” “less than or equal,” “not equal,” and “equal,” respectively.
Logical operators are operations between two logical expressions. The fundamental logical
operators we will use herein are AND, OR, and NOT, which
in matlab are denoted by &&, ∥, and ~, respectively. There are other
logical operators, but they are equivalent to combinations of these three operators. It is important to note that
OR in matlab is “inclusive” OR, meaning P OR Q is true if both
P and Q are true. In contrast, “exclusive” OR or XOR is true if either P or
Q is true but false if both P and Q are true. Just as with arithmetic operators, logical
operators have an order of operations relative to each other and in relation to arithmetic operators. All arithmetic
operations will be executed before comparison operations, which will be xecuted before logical operations.
Parentheses can be used to change the order of operations.
When your write a program you might want to distinguish cases. The
easiest way to do this in matlab is to use an if statement. Their basic
structure is the following:
if condition holds
do something
end
For example, if you want to write a program that tells you if a number inputted number is less than 3, you will code
number=input('Please input a number');
if number<3
display('The number is smaller than 3.');
end
A further tweak on conditional statements is introducing else and elseif. (Another similar structure is switch, which we omit here.)
Not surprisingly, else can be used execute something when the condition in the if statement does not hold. So for example, just slightly tweaking the above example, one could code
number=input('Please input a number');
if number<3
display('The number is smaller than 3.');
else
display('The number is not smaller than 3.');
end
If there are more cases, you would use elseif, which combines else and if:
number=input('Please input a number');
if number<3
display('The number is smaller than 3.');
elseif number==3
display('The number is equal to 3.');
else
display('The number is less than 3.');
end
IV. Loops
Besides distinguishing cases with the use of if and related operators, you might want to execute things on multiple objects. Say you want to
take a matrix, and multiply it by 2, 3 and 4. and display all the
results. For such tasks, you use a for loop:
for i=[2 3 4]
i*A
end
Naturally, you can put various things for i, such as i=1:5, i=1:2:9 etc.
A related concept is a while loop, which executes commands while the
condition specified holds true. Thus
i=2;
while i<5
i*A
i=i+1;
end
does the same thing as the previous for loop. While loops are useful for example when one is not sure until when a command should be repeated.
A simple example of loop:
>> a = 0;
>> for n =1:10 a = a + 0.1; end
>> a
a =
1.0000
>> a – 1
ans =
-1.1102e-16
Here, the line that reads “for n=1:10 a= a + 0.1; end” is called a
“loop.” This is a very common operation in most computer programs. It
can be interpreted as the command: “for each of the discrete values of
the integer variable n between 1 and 10 (inclusive), calculate the
variable “a” by adding +0.1 to the previous value of “a”. The loop
starts with the value n=1 and ends with n=10. Loops can be used to
repeat calculations many times – we will see lots more examples in
later parts of the tutorial.
Thus, the for ... end loop therefore adds 0.1 to the variable a ten
times. It gives an answer that is approximately 1. But when you
compute a-1, you don’t end up with zero. Of course -1.1102e-016 is
not a big error compared to 1, and this kind of accuracy is good
enough for government work. But if someone subtracted 1.1102e-016 from
your bank account every time a financial transaction occurred around
the world, you would burn up your bank account pretty fast. Perhaps
even faster than you do by paying your tuition bills. You can
minimize errors caused by floating point arithmetic by careful
programming, but you can’t eliminate them altogether. As a user of
matlab they are mostly out of your control, but you need to know that
they exist, and try to check the accuracy of your computations as
carefully as possible.