Dec 03, 2024 | 670 words | 7 min read
12.3.1. Task 1#
Learning Objectives#
The following exercise highlights the usage of MATLAB as a calculator. It also provides practice in writing and manipulating simple vectors and matrices in MATLAB.
Task Instructions#
Part A#
Use MATLAB to calculate the value of each expression. Record the
MATLAB command and the result of each of the variables on
MA1_Ind_Task1_Answersheet.docx
.
Save this file as
ma1_ind_1_ans_username.pdf.
Part B#
Define \(x\) and \(z\) as x = 1.3
and z = 4.7
. Then
evaluate each expression. Record the MATLAB command and result of
each variable on
MA1_Ind_Task1_Answersheet.docx
.
Save this file as
ma1_ind_1_ans_username.pdf (The same
file used in Part A).
Hint
\(\ln()\) or \(\log_e()\) is commonly known as the natural logarithm and \(\log()\) or \(\log_{10}()\) is commonly known as log to the base 10.
Part C#
Download the script
MA1_matrix_magic_template.m
.Make sure to fill out all header information, including a short description of the code.
Use MATLAB to learn what these built-in functions do:
zeros()
,ones()
,trace()
, andsum()
.In the
INITIALIZATION
section of your script file, create vectorV
and matrixM
using theones()
function.a. Create vector
V
as follows:V = [3 -6 4 2.5 0 10 -1]
b. Use the
ones()
function to create a matrix,M
, that matches the matrix below:(12.8)#\[\begin{split}M = \begin{bmatrix} 1 & 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 & 1 \\ 1 & 1 & 1 & 1 & 1 \end{bmatrix}\end{split}\]In the
MATRIX CREATION
section of your script file, perform the following:a. Replace each element in the last row of the
M
with the first element ofV
b. Then, replace the bottom right 3x3 matrix of
M
with the second element ofV
c. Next, replace the value in the top left corner of
M
with the third element ofV
d. Replace the element of
M
in the fourth row and the second column with the fifth element ofV
e. Replace the last column of
M
with the last column ofM
multiplied by the fourth element ofV
f. Finally, multiply every element in
M
by the last element inV
Note
Do not hardcode assignments unless instructed to do so.
At this point, your
M
matrix should look like this:(12.9)#\[\begin{split}M = \begin{bmatrix} -4 & -1 & -1 & -1 & -2.5 \\ -1 & -1 & -1 & -1 & -2.5 \\ -1 & -1 & 6 & 6 & 15 \\ -1 & 0 & 6 & 6 & 15 \\ -3 & -3 & 6 & 6 & 15 \end{bmatrix}\end{split}\]In the
COPY AND CONCATENATION
section of your script file, perform the following:a. Copy from
M
a 2-element row vector[0 6]
and assign it toC
.b. Copy from
M
a 2-element row vector[-4 -1]
and assign it toD
c. Create 1x4 row vector
E
that insertsC
between the first and fifth elements in the fourth row ofM
to create the vector[-1 0 6 15]
and uses square brackets to complete the insertion in one line of code.d. Create 1x4 row vector
F
that insertsD
with the third and fifth elements in the first row ofM
to create the vector[-4 -1 -1 -2.5]
and uses square brackets to complete the insertion in one line of code.In the
REPLACE MATRIX ELEMENTS
section of your script file, perform the following:a. Create a new matrix called
vals
which is a 4x4 matrix made up of zeros using thezeros()
function.b. Use only the matrix
M
and the vectorsE
andF
to replace the center 2x2 matrix ofvals
, as well as the first and last columns, so that it matches the matrix below once these replacements are complete.(12.10)#\[\begin{split}vals = \begin{bmatrix} -4 & 0 & 0 & -1 \\ -1 & 0 & 6 & 0 \\ -1 & -3 & 6 & 6 \\ -2.5 & 0 & 0 & 15 \end{bmatrix}\end{split}\]In the
FINAL MATRIX
section of your script file, perform the following:a. Create a row vector
X
that contains the sums of elements in each column ofvals
Note
You are expected to sum up all the elements that lie in the same column but go across all rows. This means that the first element in vector
X
will be \(-4 + -1 + -1 + -2.5 = -8.5\)b. Concatenate vector
X
to the top of matrixvals
to create a new matrix,G
.Note
Concatenation requires the use of square brackets
c. Create a column vector
Y
that contains the sums of value across each row ofG
.d. Concatenate vector
Y
to the right of matrixG
to create a new matrix,H
.e. Replace the lower right corner value of the matrix H with the sum of the first four diagonal values from the matrix
vals
, starting from the upper left corner and moving toward the lower right corner.Hint
This can be done with one built-in function.
In the
FORMATTED TEXT DISPLAY
section of your script file, use threefprintf()
statements to display your results as shown below. Use proper formatting so that you do not print more decimal places than necessary.Note
Do not hardcode the numerical values within your
fprintf()
statements; use array indexing ofH
to identify the appropriate values ofH
to display.Publish your script as ma1_ind_1_username.pdf.
Save your script as ma1_ind_1_username.m.
Save your flowchart in ma1_ind_1_ans_username.pdf.
Submit all three files to Gradescope.
Sample Output#
Sample Output
>> ma1_ind_1_username After doing step 8.e, the value in the center of H is 6. After doing step 8.e, the value in the upper left of H is -8.5, and the value in the upper right of H is 20.5. After doing step 8.e, the value in the lower left of H is -2.5, and the value in the lower right of H is 17.