Dec 03, 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#
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
).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];
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.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.
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
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
, andfind
functions.Note
If there are any calculations that cause errors, comment it out and re-run the code.
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#
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
).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 typingBrowvector = 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]
Open the Task 1 & 2 Answer Sheet (ma2_team_teamnumber.pdf). Then compute each of the operations under the
%% CALCULATIONS
section of your script.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.
Calc1 = Arowvector + Arowvector
Calc2 = Arowvector + Browvector
Calc3 = Arowvector + Ascalar
Calc4 = Arowvector - Arowvector
Calc5 = Arowvector - Crowvector
Calc6 = Acolvector + Bcolvector
Calc7 = Arowvector + Bcolvector
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.
Calc8 = Arowvector * Browvector
Calc9 = Arowvector .* Browvector
Calc10 = Arowvector * Ascalar
Calc11 = Arowvector .* Ascalar
Calc12 = Arowvector ./ Browvector
Calc13 = Arowvector ^ Ascalar
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)
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