Mar 14, 2025 | 798 words | 8 min read
14.2.2. Task 2#
Learning Objectives#
Practice writing simple arithmetic expressions in MATLAB. Practice writing and manipulating simple vectors and matrices in MATLAB. Understand the usability of different functions within 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.
The editor is where you will write your entire code and comments. The command
window will primarily be used for outputs, function calls, and using the help command which is
explained below. You can also use the command window to do some quick calculations and display
variables during debugging, but these won’t be a part of your script.
Task Instructions#
Part A: Syntax in MATLAB#
Open up MATLAB and type
editin the Command Window. Then save your file as ma1_team_2_teamnumber.m. Make sure to use the MATLAB Template (ENGR133_MATLAB_Template.m). Also create a Python file to use for this task and save your file as ma1_team_2_teamnumber.py. Make sure to use the Python Template (ENGR133_Python_Template.py).Note
This launches the MATLAB editor/debugger and creates a new
.mfile. You can type the same commands in the MATLAB editor as you can in the Command Window. The only difference is that using the editor allows you to run a series of commands as often as you like. For this exercise and all future exercises, unless otherwise indicated, you should use the editor and script files to construct and comment your MATLAB code. The command window will primarily be used for function calls, using the ‘help’ command which is explained below, and displaying script outputs. You can also use the command window to do quick calculations and display variables during debugging, but they will not be a part of your script.Open up the Answer Sheet (
MA1_Team_2_Teamnumber.docx). Save this file as ma1_team_2_teamnumber.pdf.Note
You will submit this answer sheet as a PDF after Task 3.
a. For each of the operations listed in the table on the answer sheet calculate the results in both MATLAB and Python. Use the MATLAB file created in Step 1. Fill in the table with the outputs for both MATLAB and Python, including any error messages. If an equation stops the code from running, comment it out.
Note
To comment in MATLAB use a
%at the beginning of the line. To comment in Python use a#at beginning of the line.b. Answer the questions for Task #2 Part A.
Part B: Common Functions in MATLAB#
In MATLAB, find more information on the following functions using the official documentation. Fill in what you find on the Answer Sheet (
MA1_Team_teamnumber.docx).a.
sinb.
expc.
modd.
sqrte.
absf.
pig.
logh.
log10i.
acosj.
;(semicolons in relation to output)MATLAB also has a command line help utility that will give you more information about specific commands directly in the Command Window. For example, typing
help sindirectly in the Command Window will give you information on thesinfunction. Use thehelpcommand to find out more information on the commands listed below. Then fill in what you find on the Answer Sheet.a.
clcb.
clearc.
whosd.
whate.
whyf.
ansOnce you have tested all the commands, answer the remaining question on the Answer Sheet.
Part C: Vectors and Matrices in MATLAB#
In the same script ma1_team_2_teamnumber.m. Use
clearandclcin the Command Window to remove any data and output from previous scripts.Note
You can comment the code from previous parts by adding
%at the begining of the lineCreate the matrix shown below:
Amatrix = [ 1 1 1 1; 10 20 30 40; 2 4 6 8; 100 200 300 400]
Now type each of the commands below in your script, then run your script to find out what each of the commands do.
a.
Bvector = Amatrix(1,:)b.
Cvector = Amatrix(2,:)c.
Dvector = Amatrix(:,3)d.
Evector = sort(DVector)e.
Amatrix(3) = 30; Amatrixf.
Fvector = linspace(1,25,4)g.
Gvector = Evector * 5h.
Hvector = Amatrix(1:2)i.
Ivector = Amatrix(3:6)j.
Jmatrix = Amatrix(1:2,2:3)Open up the Answer Sheet (
MA1_Team_2_Teamnumber.docx). In the first table under Task #2 Part C, describe what each of commands above does when the script is run. If you need help, search the MATLAB documentation for the termsarray indexing,colon,sort, andlinspace.Given each of the descriptions below, write a MATLAB command that will complete the desired function and record your answers on the second table under Task #2 Part C.
Note
You may need to use more than one line and/or command to complete each function.
Create
Xmatrixwith values identical toAmatrixexcept the second row isBvector.Create
Yvectorby extracting the third row inAmatrix.Extract the element in row 2, column 3 of
Amatrixand assign it to the variablez.Replace the value in
Amatrixrow 1, column 1 with the value 55.
Save your script as ma1_team_2_teamnumber.m.
Save your script as ma1_team_2_teamnumber.py.
Deliverables |
Description |
|---|---|
ma1_team_2_teamnumber.py |
Your completed Python code. |
ma1_team_2_teamnumber.m |
Your completed MATLAB code. |
ma1_team_2_teamnumber.pdf |
Your answers for each part of this task. |