- Computation
- How real numbers are digitally represented (“floating point”)
- Variable types
- Selection statements (if and switch)
- Loops (for and while)
- User defined functions
Computation
Arithmetic Logic Unit (ALU) in a computer’s CPU executes fundamental arithmetic and logical operations. These are combined at higher levels of hardware and software to perform more sophisticated computations.
Knowing this can improve engineers’ understanding and performance in several areas:
- Error awareness: Understanding potential sources of computational errors. For instance, floating-point arithmetic can lead to precision issues and rounding errors, which can accumulate in large-scale computations.
- Algorithm design: Understanding the limitations of computer arithmetic helps design accurate numerical algorithms.
- Debugging and troubleshooting: When unexpected results occur in computations, understanding the underlying arithmetic can be crucial in identifying and resolving issues.
MATLAB Recap
% assign a value to variable t
t = 5;
% calculate the value of another variable x
x = 3*sin(5*pi*t/2);
% if x satisfies some condition, output message to console
if x < 9
display('Hello World');
end
t = 5;
% calculate the value of another variable x
x = 3*sin(5*pi*t/2);
% if x satisfies some condition, output message to console
if x < 9
display('Hello World');
end
MATLAB Statements/Commands
MATLAB statements are instructions; they are not equations.
Example:
a = 5 % Assign the value on the right side to the variable on the left
a == 5 % Check if the right and left sides are equal; returns true if equal, false otherwise
a == 5 % Check if the right and left sides are equal; returns true if equal, false otherwise
In MATLAB:
=is an assignment operator.==is a comparison operator used in logical expressions.
Some Variable Types in MATLAB
| Variable Type | Memory Size | Examples |
|---|---|---|
| Double | 8 Bytes | 5, 1.2345, pi, -7.3e5, … |
| Character | 2 Bytes | 'x', 'y', '0', … |
| Logical | 1 Byte | false, true |
Propositional Logic
Truth Tables for Conjunction (“and”), Disjunction (“or”)
| P | Q | P & Q | P | Q |
|---|---|---|---|
| T | T | T | T |
| T | F | F | T |
| F | T | F | T |
| F | F | F | F |
Example of Composite Statement
| P | Q | R | Q | R | P & (Q | R) |
|---|---|---|---|---|
| T | T | T | T | T |
| T | T | F | T | T |
| T | F | T | T | T |
| T | F | F | F | F |
| F | T | T | T | F |
| F | T | F | T | F |
| F | F | T | T | F |
| F | F | F | F | F |
MATLAB Examples
P = (1 < (2/pi))
Q = (49 == 7^2)
P & Q
P | Q
For more details and examples on propositional logic, please visit: Propositional Logic
Selection Statements
Basic if-else Statement
if y == 7
x = 5
else
x = -9
end
x = 5
else
x = -9
end
if-elseif-else Statement
if y == 20
x = 5
elseif y == 35
x = 20
else
x = -4
end
x = 5
elseif y == 35
x = 20
else
x = -4
end
switch Statement
switch y
case 5
x = 15
case 10
x = 35
otherwise
x = -4
end
case 5
x = 15
case 10
x = 35
otherwise
x = -4
end
Loops
For-loop
for n = 1:7
% computations
%
end
% computations
%
end
Note: The for-loop executes a block of code a specified number of times. In this example, the loop runs 5 times with n taking values from 1 to 7.
While-loop
n = 1
while n ≤ 7
% computations
%
n = n + 1
end
while n ≤ 7
% computations
%
n = n + 1
end
Note: The while-loop executes a block of code as long as a specified condition is true. Remember to update the loop variable (n) inside the loop to avoid infinite loops.
Functions
User-defined function examples:
Techique 1: All in one file
script_1.m
x = 15;
y = 19;
z = myfunc(x,y);
function r = myfunc(a,b)
l = m^2 - n;
end
y = 19;
z = myfunc(x,y);
function r = myfunc(a,b)
l = m^2 - n;
end
Note: In MATLAB, when using local functions in a script file (Techique 1), the functions must be placed at the end of the file. The main script code comes first, followed by all function definitions.
Method 2: Separate function file
mainscript.m
x = 15;
y = 19;
z = myfunc(x,y);
y = 19;
z = myfunc(x,y);
myfunc.m
function l = myfunc(m,n)
% MYFUNC Example custom function
% Computes m squared minus n
l = m^2 - n;
end
% MYFUNC Example custom function
% Computes m squared minus n
l = m^2 - n;
end
Note: When using separate files (Techique 2), the function file must have the same name as the function (myfunc.m) and be located in the MATLAB path.
This is the traditional approach for organizing larger projects.