Dec 03, 2024 | 675 words | 7 min read
12.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.
Task Instructions#
Part A: Syntax in MATLAB#
Open up MATLAB and type
edit
in 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
.m
file. 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.Open up the Answer Sheet (
MA1_Team_teamnumber.docx
). Save this file as ma1_team_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.
sin
b.
exp
c.
mod
d.
sqrt
e.
abs
f.
pi
g.
log
h.
log10
i.
acos
j.
;
(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 sin
directly in the Command Window will give you information on thesin
function. Use thehelp
command to find out more information on the commands listed below. Then fill in what you find on the Answer Sheet.a.
clc
b.
clear
c.
whos
d.
what
e.
why
f.
ans
Once 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
clear
andclc
in 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; Amatrix
f.
Fvector = linspace(1,25,4)
g.
Gvector = Evector * 5
h.
Hvector = Amatrix(1:2)
i.
Ivector = Amatrix(3:6)
j.
Jmatrix = Amatrix(1:2,2:3)
Open up the Answer Sheet (
MA1_Team_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
Xmatrix
with values identical toAmatrix
except the second row isBvector
.Create
Yvector
by extracting the third row inAmatrix
.Extract the element in row 2, column 3 of
Amatrix
and assign it to the variablez
.Replace the value in
Amatrix
row 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.