Mar 14, 2025 | 698 words | 7 min read
14.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_1_Username.docx.
Save this file as
ma1_ind_1_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_1_Username.docx.
Save this file as
ma1_ind_1_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
INITIALIZATIONsection of your script file, create vectorVand matrixMusing theones()function.a. Create vector
Vas 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:(14.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 CREATIONsection of your script file, perform the following:a. Replace each element in the last row of the
Mwith the first element ofVb. Then, replace the bottom right 3x3 matrix of
Mwith the second element ofVc. Next, replace the value in the top left corner of
Mwith the third element ofVd. Replace the element of
Min the fourth row and the second column with the fifth element ofVe. Replace the last column of
Mwith the last column ofMmultiplied by the fourth element ofVf. Finally, multiply every element in
Mby the last element inVNote
Do not hardcode assignments unless instructed to do so.
At this point, your
Mmatrix should look like this:(14.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 CONCATENATIONsection of your script file, perform the following:a. Copy from
Ma 2-element row vector[0 6]and assign it toC.b. Copy from
Ma 2-element row vector[-4 -1]and assign it toDc. Create 1x4 row vector
Ethat insertsCbetween the first and fifth elements in the fourth row ofMto 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
Fthat insertsDwith the third and fifth elements in the first row ofMto 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 ELEMENTSsection of your script file, perform the following:a. Create a new matrix called
valswhich is a 4x4 matrix made up of zeros using thezeros()function.b. Use only the matrix
Mand the vectorsEandFto 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.(14.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 MATRIXsection of your script file, perform the following:a. Create a row vector
Xthat contains the sums of elements in each column ofvalsNote
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
Xwill be \(-4 + -1 + -1 + -2.5 = -8.5\)b. Concatenate vector
Xto the top of matrixvalsto create a new matrix,G.Note
Concatenation requires the use of square brackets
c. Create a column vector
Ythat contains the sums of value across each row ofG.d. Concatenate vector
Yto the right of matrixGto 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 DISPLAYsection 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 ofHto identify the appropriate values ofHto display.Publish your script as ma1_ind_1_pub_username.pdf.
Save your script as ma1_ind_1_username.m.
Save your flowchart in ma1_ind_1_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.
Deliverables |
Description |
|---|---|
ma1_ind_1_username.m |
Your completed MATLAB code for part C. |
ma1_ind_1_pub_username.pdf |
PDF with published MATLAB code for part C. |
ma1_ind_1_username.pdf |
Your answers for part A and B. Flowchart(s) for part C. |