\[ \begin{align}\begin{aligned}\newcommand\blank{~\underline{\hspace{1.2cm}}~}\\% Bold symbols (vectors) \newcommand\bs[1]{\mathbf{#1}}\\% 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{\%}} % % Angle \newcommand\radian{\unit{rad}} \newcommand\degree{\unit{{}^\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 % % 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} \]

Oct 24, 2024 | 630 words | 6 min read

13.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.

Task Instructions#

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.

Part A: Logical Operators#

  1. Open up MATLAB and type edit in the Command Window. Then save your file as ma2_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 (MA2_Team_teamnumber.docx). Save this file as ma2_team_teamnumber.pdf. Then compute each of the operations under the %% CALCULATIONS section of your script.

    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.

      Note

      If there are any calculations that cause errors, comment it out and re-run the code.

      1. Ans_A = Aarray >= Barray

      2. Ans_B = Aarray .* Barray ~= 1

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

      4. Ans_D = Barray > Carray

      5. 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.

      Note

      If there are any calculations that cause errors, comment it out and re-run the code.

      1. Ans_F = any(Aarray) + any(Barray)

      2. Ans_G = all(Aarray) + all(Barray)

      3. Ans_H = all(Carray > 1)

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

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

      6. 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 ma2_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 with four elements starting at 4 and ending at -2 with increments of -2. Do this by typing Browvector = 4:-2:-2

      Note

      This notation start: step: end allows you to create vectors of any size with any starting value, increment size, and ending value.

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

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

      NOTE

      This is a 4x1 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 Task 1 & 2 Answer Sheet (ma2_team_teamnumber.pdf). Then compute each of the operations under the %% CALCULATIONS section of your script.

    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.

      Note

      If there are any calculations that cause errors, comment it out and re-run the code.

      1. Calc1 = Arowvector + Arowvector

      2. Calc2 = Arowvector + Browvector

      3. Calc3 = Arowvector + Ascalar

      4. Calc4 = Arowvector - Arowvector

      5. Calc5 = Arowvector - Crowvector

      6. Calc6 = Acolvector + Bcolvector

      7. 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.

      Note

      If there are any calculations that cause errors, comment it out and re-run the code.

      1. Calc8 = Arowvector * Browvector

      2. Calc9 = Arowvector .* Browvector

      3. Calc10 = Arowvector * Ascalar

      4. Calc11 = Arowvector .* Ascalar

      5. Calc12 = Arowvector ./ Browvector

      6. Calc13 = Arowvector ^ Ascalar

      7. Calc14 = Arowvector .^ Ascalar

Part C: Nested Conditionals#

Open up MATLAB and type edit in the Command Window. Then save your file as ma2_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 task 2 is to rewrite the set of nested conditional statements in MATLAB. Publish your script as ma2_team_2_c_teamnumber.pdf by using x = 40 and y = 40.

Previous Python Code

x = ***
y = ***
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('z=', z)
Table 13.3 Expected Output#

x

y

Output

40

40

z=4

20

20

z=400

55

20

Error:Unrecognised function or variable ‘z’

Note

Replace the *** for x and y in the Python code to run each case separately. Clear the variables in the workspace for this to run accurately each time.

Part D: Loop Structures#

Open up MATLAB and type edit in the Command Window. Then save your file as ma2_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 task 2 is to rewrite the set of loop structures in MATLAB. Publish your script as ma2_team_2_d_teamnumber.pdf.

Previous Python Code

i=1
x=3
y=6
while i<10:
    for z in [x,y]:
        i+=z
    if x<3:
        x+=1
print(i)

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

for index in w:
    if index <= 3:
        u=u+index
    print(f'u={u}')

Sample Output#

Sample Output

>> ma2_team_2_d_teamnumber 10 u=3 u=3 u=1 u=2