Mar 14, 2025 | 690 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.
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
editin 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
%% INTIALIZATIONsection, 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_2_teamnumber.docx). Then compute each of the operations under the%% CALCULATIONSsection 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 >= BarrayAns_B = Aarray .* Barray ~= 1Ans_C = Barray < (Aarray – Ans_B) <= (Ans_B < 1) * 3Ans_D = Barray > CarrayAns_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, andfindfunctions.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
editin 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 = 3Arowvector = [0 1 2 3]Browvectorwith four elements starting at 4 and ending at -2 with increments of -2. Do this by typingBrowvector = 4:-2:-2Note
This notation start: step: end allows you to create vectors of any size with any starting value, increment size, and ending value.
Crowvectorwith 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 Answer Sheet from part A. Then compute each of the operations under the
%% CALCULATIONSsection 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 + ArowvectorCalc2 = Arowvector + BrowvectorCalc3 = Arowvector + AscalarCalc4 = Arowvector - ArowvectorCalc5 = Arowvector - CrowvectorCalc6 = Acolvector + BcolvectorCalc7 = 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 * BrowvectorCalc9 = Arowvector .* BrowvectorCalc10 = Arowvector * AscalarCalc11 = Arowvector .* AscalarCalc12 = Arowvector ./ BrowvectorCalc13 = Arowvector ^ AscalarCalc14 = Arowvector .^ Ascalar
Save the Answer Sheet as ma2_team_2_teamnumber.pdf.
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_pub_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: Unrecognized 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_pub_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
Deliverables |
Description |
|---|---|
ma2_team_2_a_teamnumber.m |
Your completed MATLAB code for part A. |
ma2_team_2_b_teamnumber.m |
Your completed MATLAB code for part B. |
ma2_team_2_c_teamnumber.m |
Your completed MATLAB code for part C. |
ma2_team_2_d_teamnumber.m |
Your completed MATLAB code for part D. |
ma2_team_2_c_pub_teamnumber.pdf |
PDF with published MATLAB code for part C. |
ma2_team_2_d_pub_teamnumber.pdf |
PDF with published MATLAB code for part D. |
ma2_team_2_teamnumber.pdf |
Your answers for each part of this task. |