MATLAB Selection Statements, Loops, & 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:




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



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
In MATLAB:



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
TTTT
TFFT
FTFT
FFFF

Example of Composite Statement

P Q R Q | R P & (Q | R)
TTTTT
TTFTT
TFTTT
TFFFF
FTTTF
FTFTF
FFTTF
FFFFF


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

if-elseif-else Statement

if y == 20
    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

Loops

For-loop

for n = 1:7
    % 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
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
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);
myfunc.m
function l = myfunc(m,n)
    % 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.


Practice Questions: Test your Knowledge

View the Questions

Answers to the Practice Questions

View the Answer



Additional Exercises



Practice Exercise 1

View the Exercise

Solution to Exercise 1

View the Answer

Practice Exercise 2

View the Exercise

Solution to Exercise 2

View the Answer

Practice Exercise 3

View the Exercise

Solution to Exercise 3

View the Answer

Practice Exercise 4

View the Exercise

Solution to Exercise 4

View the Answer

Practice Exercise 5

View the Exercise

Solution to Exercise 5

View the Answer

Practice Exercise 6

View the Questions

Solution to Exercise 6

View the Answer

Practice Exercise 7

View the Questions

Solution to Exercise 7

View the Answer

Practice Exercise 8

View the Questions

Solution to Exercise 8

View the Answer

Practice Exercise 9

View the Questions

Solution to Exercise 9

View the Answer

Practice Exercise 10

View the Questions

Solution to Exercise 10

View the Answer

Practice Exercise 11

View the Questions

Solution to Exercise 11

View the Answer

Practice Exercise 12

View the Questions

Solution to Exercise 12

View the Answer

Practice Exercise 13

View the Questions

Solution to Exercise 13

View the Answer

Practice Exercise 14

View the Questions

Solution to Exercise 14

View the Answer

Practice Exercise 15

View the Questions

Solution to Exercise 15

View the Answer

Practice Exercise 16

View the Questions

Solution to Exercise 16

View the Answer

Practice Exercise 17

View the Questions

Solution to Exercise 17

View the Answer

Practice Exercise 18

View the Questions

Solution to Exercise 18

View the Answer

Practice Exercise 19

View the Questions

Solution to Exercise 19

View the Answer

Practice Exercise 20

View the Questions

Solution to Exercise 20

View the Answer

Practice Exercise 21

View the Questions

Solution to Exercise 21

View the Answer

For more details, please contact me here.