Essential Mathematics for Data Scientists
Assessment 2D: MATLAB logic exercise
This exercise forms a part of your code workbook. Include answers to the tasks below in your workbook
as appropriately commented MATLAB code.
Like many programming languages, MATLAB has support for logical (Boolean) variables. In MATLAB, the
truth values ?? and ?? are specified using true and false. However, it should be noted that when
MATLAB displays logical variables, it shows true values as “1” and false values as “0”.
Of the logical connectives we have covered, MATLAB supports the small selection summarised in the
following table:
Connective MATLAB function MATLAB operator
AND and &
OR or |
NOT not ~
This means, given logical variables p and q, the proposition ?? ∧ ?? can be evaluated in MATLAB as either
and(p,q) or p&q.
In this exercise, we are interested in using MATLAB to determine if the proposition
(?? → ??) ∧ ¬?? ↔ ?? ∨ ??
is a tautology or a contradiction (if either). This can of course be achieved after enough simplification
with the laws of logic, but it should be much quicker to instead use MATLAB to evaluate the above
proposition for every combination of truth values for ?? and ?? (in effect, computing the truth table for
the proposition).
For this exercise, we encourage extensive use of MATLAB’s anonymous function notation which allows
functions to be defined succinctly in the same way you would define a variable. e.g.
add = @(x,y) x+y
defines a function that adds its inputs. e.g. add(1,2) would return 3.
Tasks
1. To begin with, we need to implement some of the logical connectives that MATLAB is missing.
Consider the IF-THEN connective. Using the implication law:
?? → ?? ≡ ¬?? ∨ ??
Write an ifthen function that defines IF-THEN in terms of NOT and OR, both of which MATLAB knows
y default. (2 marks)
2. Next, we need an IF-AND-ONLY-IF connective function iff. Use the relevant law of logic to
write this function in terms of ifthen. (3 marks)
3. To make it easier to evaluate the proposition
(?? → ??) ∧ ¬?? ↔ ?? ∨ ??,
use the ifthen and iff functions to define a proposition function that does this for a given p
and q. (3 marks)
4. Use this proposition function to determine whether the above proposition is a tautology,
contradiction or neither. (2 marks)
Essential Mathematics for Data Scientists
Assessment 2D: MATLAB logic exercise
Tasks