\[ \begin{align}\begin{aligned}\newcommand\blank{~\underline{\hspace{1.2cm}}~}\\% Bold symbols (vectors) \newcommand\bs[1]{\mathbf{#1}}\\% Differential \newcommand\dd[2][]{\mathrm{d}^{#1}{#2}} % use as \dd, \dd{x}, or \dd[2]{x}\\% Poor man's siunitx \newcommand\unit[1]{\mathrm{#1}} \newcommand\num[1]{#1} \newcommand\qty[2]{#1~\unit{#2}}\\\newcommand\per{/} \newcommand\squared{{}^2} \newcommand\cubed{{}^3} % % Scale \newcommand\milli{\unit{m}} \newcommand\centi{\unit{c}} \newcommand\kilo{\unit{k}} \newcommand\mega{\unit{M}} % % Percent \newcommand\percent{\unit{{\kern-4mu}\%}} % % Angle \newcommand\radian{\unit{rad}} \newcommand\degree{\unit{{\kern-4mu}^\circ}} % % Time \newcommand\second{\unit{s}} \newcommand\s{\second} \newcommand\minute{\unit{min}} \newcommand\hour{\unit{h}} % % Distance \newcommand\meter{\unit{m}} \newcommand\m{\meter} \newcommand\inch{\unit{in}} \newcommand\foot{\unit{ft}} % % Force \newcommand\newton{\unit{N}} \newcommand\kip{\unit{kip}} % kilopound in "freedom" units - edit made by Sri % % Mass \newcommand\gram{\unit{g}} \newcommand\g{\gram} \newcommand\kilogram{\unit{kg}} \newcommand\kg{\kilogram} \newcommand\grain{\unit{grain}} \newcommand\ounce{\unit{oz}} % % Temperature \newcommand\kelvin{\unit{K}} \newcommand\K{\kelvin} \newcommand\celsius{\unit{{}^\circ C}} \newcommand\C{\celsius} \newcommand\fahrenheit{\unit{{}^\circ F}} \newcommand\F{\fahrenheit} % % Area \newcommand\sqft{\unit{sq\,\foot}} % square foot % % Volume \newcommand\liter{\unit{L}} \newcommand\gallon{\unit{gal}} % % Frequency \newcommand\hertz{\unit{Hz}} \newcommand\rpm{\unit{rpm}} % % Voltage \newcommand\volt{\unit{V}} \newcommand\V{\volt} \newcommand\millivolt{\milli\volt} \newcommand\mV{\milli\volt} \newcommand\kilovolt{\kilo\volt} \newcommand\kV{\kilo\volt} % % Current \newcommand\ampere{\unit{A}} \newcommand\A{\ampere} \newcommand\milliampereA{\milli\ampere} \newcommand\mA{\milli\ampere} \newcommand\kiloampereA{\kilo\ampere} \newcommand\kA{\kilo\ampere} % % Resistance \newcommand\ohm{\Omega} \newcommand\milliohm{\milli\ohm} \newcommand\kiloohm{\kilo\ohm} % correct SI spelling \newcommand\kilohm{\kilo\ohm} % "American" spelling used in siunitx \newcommand\megaohm{\mega\ohm} % correct SI spelling \newcommand\megohm{\mega\ohm} % "American" spelling used in siunitx % % Capacitance \newcommand\farad{\unit{F}} \newcommand\F{\farad} \newcommand\microfarad{\micro\farad} \newcommand\muF{\micro\farad} % % Inductance \newcommand\henry{\unit{H}} \newcommand\H{\henry} \newcommand\millihenry{\milli\henry} \newcommand\mH{\milli\henry} % % Power \newcommand\watt{\unit{W}} \newcommand\W{\watt} \newcommand\milliwatt{\milli\watt} \newcommand\mW{\milli\watt} \newcommand\kilowatt{\kilo\watt} \newcommand\kW{\kilo\watt} % % Energy \newcommand\joule{\unit{J}} \newcommand\J{\joule} % % Composite units % % Torque \newcommand\ozin{\unit{\ounce}\,\unit{in}} \newcommand\newtonmeter{\unit{\newton\,\meter}} % % Pressure \newcommand\psf{\unit{psf}} % pounds per square foot \newcommand\pcf{\unit{pcf}} % pounds per cubic foot \newcommand\pascal{\unit{Pa}} \newcommand\Pa{\pascal} \newcommand\ksi{\unit{ksi}} % kilopound per square inch \newcommand\bar{\unit{bar}} \end{aligned}\end{align} \]

Dec 04, 2025 | 702 words | 7 min read

15.2.2. Task 2#

Learning Objectives#

Practice using simple logical operators in MATLAB. Practice manipulating simple vectors and matrices in MATLAB. Understand the purpose of nested conditional statements and loop structures in MATLAB.

Introduction#

This task is designed to introduce you to the MATLAB IDE and syntax. You will be asked to explore various operations in MATLAB and determine the differences from Python. Although the syntax differs, the coding applications are the same as what you have previously seen in Python. You will also be asked to explore common built-in functions in MATLAB. These will be useful for future assignments, so make sure that you understand how each function works. Lastly, you will be getting practice with vector and matrix operations. MATLAB is designed to handle vectors and matrices via indexing and mathematical operations. This is a fundamental part of MATLAB, and it will help you with tasks in the remaining part of the semester.

Task Instructions#

Part A: Logical Operators#

  1. Open up MATLAB and type edit in the Command Window. Then save your file as ma1_team_2_a_teamnumber.m. Make sure to use the MATLAB Template (ENGR133_MATLAB_Template.m).

  2. In the INTIALIZATION section, create the following vectors and arrays:

    • Cvector = [1, 2];

    • Aarray = [1, 2; 3, 4];

    • Barray = [1, 0; -1, 4];

    • Carray = [1, 2; 3, 4; 5, 6];

    • Darray = [0, 0, 1; 3, 5, -5; 1, 0, 1];

  3. Open up the Answer Sheet (MA1_Team_2_teamnumber.docx). Then compute each of the operations under the %% CALCULATIONS section of your script.

    Note

    If any calculations cause errors, comment them out and re-run the code.

    1. Fill in the table with the calculated output and an explanation of what MATLAB did to get the result or why MATLAB cannot perform the operation.

      • Ans_A = Aarray >= Barray

      • Ans_B = Aarray .* Barray ~= 1

      • Ans_C = Barray < (Aarray Ans_B) <= (Ans_B < 1) * 3

      • Ans_D = Barray > Carray

      • Ans_E = [Barray; Cvector] == Carray

    2. Fill in the table with the calculated output and an explanation of what MATLAB did to get the result or why MATLAB cannot perform the operation. Use MATLAB documentation to learn about any, all, and find functions.

      • Ans_F = any(Aarray) + any(Barray)

      • Ans_G = all(Aarray) + all(Barray)

      • Ans_H = all(Carray > 1)

      • Ans_I = all(any(Barray < -1))+any(all(Darray))

      • Ans_J = find(Barray).^(find(Carray > 5))

      • Ans_K = find(any(Darray == 1))

Part B: Vector Manipulation#

  1. Open up MATLAB and type edit in the Command Window. Then save your file as ma1_team_2_b_teamnumber.m. Make sure to use the MATLAB Template (ENGR133_MATLAB_Template.m).

  2. In %% INTIALIZATION, create the following scalars and vectors:

    • Ascalar = 3

    • Arowvector = [0 1 2 3]

    • Browvector = 4:-2:-2

      Note

      This notation start:step:end allows you to create vectors of any size with any starting value, increment step size, and ending value. This example creates a four element vector starting at \(4\) and ending at \(-2\) with increments of \(-2\).

    • Crowvector with three elements of your choice between \(-5\) and \(5\) inclusive.

    • Acolvector = [0; 1; 2; 3]

      Note

      This is a \(4\times1\) column vector, which is different from Arowvector. You could also create Acolvector by computing the transpose of Arowvector by typing Acolvector = Arowvector’

    • Bcolvector = [-4; -3; -2; -1]

  3. Open the Answer Sheet from part A. Then compute each of the operations under the CALCULATIONS section of your script.

    Note

    If any calculations cause errors, comment them out and re-run the code.

    1. Fill in the table with the calculated output and an explanation of what MATLAB did to get the result or why MATLAB cannot perform the operation.

      • Calc1 = Arowvector + Arowvector

      • Calc2 = Arowvector + Browvector

      • Calc3 = Arowvector + Ascalar

      • Calc4 = Arowvector - Arowvector

      • Calc5 = Arowvector - Crowvector

      • Calc6 = Acolvector + Bcolvector

      • Calc7 = Arowvector + Bcolvector

    2. Fill in the table with the calculated output and an explanation of what MATLAB did to get the result or why MATLAB cannot perform the operation.

      • Calc8 = Arowvector * Browvector

      • Calc9 = Arowvector .* Browvector

      • Calc10 = Arowvector * Ascalar

      • Calc11 = Arowvector .* Ascalar

      • Calc12 = Arowvector ./ Browvector

      • Calc13 = Arowvector ^ Ascalar

      • Calc14 = Arowvector .^ Ascalar

  4. Save the Answer Sheet as ma1_team_2_teamnumber.pdf.

Part C: Nested Conditionals#

Open up MATLAB and type edit in the Command Window. Then save your file as ma1_team_2_c_teamnumber.m. Make sure to use the MATLAB Template (ENGR133_MATLAB_Template.m).

Below is a set of nested conditional statements written in Python syntax. The goal of this part of the task is to rewrite the set of nested conditional statements in MATLAB. Publish your script as ma1_team_2_c_pub_teamnumber.pdf with x and y hardcoded as x = 40 and y = 40.

Listing 15.1 Previous Python Code#
x = int(input("Choose x value: "))
y = int(input("Choose y value: "))

if x <= 50:
    z = 4
    if y < 30:
        z = x * y
    elif y >= 100:
        z = x + y

elif y >= 60:
    if x > 80:
        z = x
    elif y > 50:
        z = y
else:
    z = z * 2

print(f"z = {z}")

Hint

Remember to add end at the end of each if statement and loop!

Sample Output#

Use the values in Table 15.3 below to test your program.

Table 15.3 Test Cases#

Case

x

y

1

40

40

2

20

20

3

55

22

Ensure your program’s output matches the provided samples exactly. This includes all characters, white space, and punctuation. In the samples, user input is highlighted like this for clarity, but your program should not highlight user input in this way.

Case 1 Sample Output

>> ma1_team_2_c_teamnumber Choose x value: 40 Choose y value: 40 z = 4

Case 2 Sample Output

>> ma1_team_2_c_teamnumber Choose x value: 20 Choose y value: 20 z = 400

Case 3 Sample Output

>> ma1_team_2_c_teamnumber Choose x value: 55 Choose y value: 22 {Unrecognized function or variable 'z'.

Error in solution (line 56) z = z * 2; ^ }

Part D: Loop Structures#

Open up MATLAB and type edit in the Command Window. Then save your file as ma1_team_2_d_teamnumber.m. Make sure to use the MATLAB Template (ENGR133_MATLAB_Template.m).

Below is a set of loop structures written in Python syntax. The goal of this part of this task is to rewrite the set of loop structures in MATLAB. Publish your script as ma1_team_2_d_pub_teamnumber.pdf. Use the same variable names used in the script.

Listing 15.2 Previous Python Code#
i = 1
x = 3
y = 6
print("While Loop Ouput:")
while i < 10:
    for z in [x, y]:
        i += z
    if x < 3:
        x += 1
print(i)

w = [3, 6, -2, 1]
u = 0

print("For Loop Output:")
for index in w:
    if index <= 3:
        u = u + index
    print(f"u = {u}")

Sample Output#

Sample Output

>> ma1_team_2_d_teamnumber While Loop Output: 10 For Loop Output: u = 3 u = 3 u = 1 u = 2

Table 15.4 Deliverables#

Deliverables

Description

ma1_team_2_a_teamnumber.m

Your completed MATLAB code for part A.

ma1_team_2_b_teamnumber.m

Your completed MATLAB code for part B.

ma1_team_2_c_teamnumber.m

Your completed MATLAB code for part C.

ma1_team_2_d_teamnumber.m

Your completed MATLAB code for part D.

ma1_team_2_c_pub_teamnumber.pdf

PDF with published MATLAB code for part C.

ma1_team_2_d_pub_teamnumber.pdf

PDF with published MATLAB code for part D.

ma1_team_2_teamnumber.pdf

Your answers for each part of this task.