\[ \begin{align}\begin{aligned}\newcommand\blank{~\underline{\hspace{1.2cm}}~}\\% Bold symbols (vectors) \newcommand\bs[1]{\mathbf{#1}}\\% Poor man's siunitx \newcommand\unit[1]{\mathrm{#1}} \newcommand\num[1]{#1} \newcommand\qty[2]{#1~\unit{#2}}\\\newcommand\per{/} \newcommand\squared{{}^2} \newcommand\cubed{{}^3} % % Scale \newcommand\milli{\unit{m}} \newcommand\centi{\unit{c}} \newcommand\kilo{\unit{k}} \newcommand\mega{\unit{M}} % % Percent \newcommand\percent{\unit{\%}} % % Angle \newcommand\radian{\unit{rad}} \newcommand\degree{\unit{{}^\circ}} % % Time \newcommand\second{\unit{s}} \newcommand\s{\second} \newcommand\minute{\unit{min}} \newcommand\hour{\unit{h}} % % Distance \newcommand\meter{\unit{m}} \newcommand\m{\meter} \newcommand\inch{\unit{in}} \newcommand\foot{\unit{ft}} % % Force \newcommand\newton{\unit{N}} \newcommand\kip{\unit{kip}} % kilopound in "freedom" units - edit made by Sri % % Mass \newcommand\gram{\unit{g}} \newcommand\g{\gram} \newcommand\kilogram{\unit{kg}} \newcommand\kg{\kilogram} \newcommand\grain{\unit{grain}} \newcommand\ounce{\unit{oz}} % % Temperature \newcommand\kelvin{\unit{K}} \newcommand\K{\kelvin} \newcommand\celsius{\unit{{}^\circ C}} \newcommand\C{\celsius} \newcommand\fahrenheit{\unit{{}^\circ F}} \newcommand\F{\fahrenheit} % % Area \newcommand\sqft{\unit{sq\,\foot}} % square foot % % Volume \newcommand\liter{\unit{L}} \newcommand\gallon{\unit{gal}} % % Frequency \newcommand\hertz{\unit{Hz}} \newcommand\rpm{\unit{rpm}} % % Voltage \newcommand\volt{\unit{V}} \newcommand\V{\volt} \newcommand\millivolt{\milli\volt} \newcommand\mV{\milli\volt} \newcommand\kilovolt{\kilo\volt} \newcommand\kV{\kilo\volt} % % Current \newcommand\ampere{\unit{A}} \newcommand\A{\ampere} \newcommand\milliampereA{\milli\ampere} \newcommand\mA{\milli\ampere} \newcommand\kiloampereA{\kilo\ampere} \newcommand\kA{\kilo\ampere} % % Resistance \newcommand\ohm{\Omega} \newcommand\milliohm{\milli\ohm} \newcommand\kiloohm{\kilo\ohm} % correct SI spelling \newcommand\kilohm{\kilo\ohm} % "American" spelling used in siunitx \newcommand\megaohm{\mega\ohm} % correct SI spelling \newcommand\megohm{\mega\ohm} % "American" spelling used in siunitx % % Inductance \newcommand\henry{\unit{H}} \newcommand\H{\henry} \newcommand\millihenry{\milli\henry} \newcommand\mH{\milli\henry} % % Power \newcommand\watt{\unit{W}} \newcommand\W{\watt} \newcommand\milliwatt{\milli\watt} \newcommand\mW{\milli\watt} \newcommand\kilowatt{\kilo\watt} \newcommand\kW{\kilo\watt} % % Energy \newcommand\joule{\unit{J}} \newcommand\J{\joule} % % Composite units % % Torque \newcommand\ozin{\unit{\ounce}\,\unit{in}} \newcommand\newtonmeter{\unit{\newton\,\meter}} % % Pressure \newcommand\psf{\unit{psf}} % pounds per square foot \newcommand\pcf{\unit{pcf}} % pounds per cubic foot \newcommand\pascal{\unit{Pa}} \newcommand\Pa{\pascal} \newcommand\ksi{\unit{ksi}} % kilopound per square inch \newcommand\bar{\unit{bar}} \end{aligned}\end{align} \]

Oct 24, 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.

(12.1)#\[p = \frac{2^{3^2}}{13} + \frac{180}{\ln(60)} + \sqrt{10}\]
(12.2)#\[q = \left( 15 - \frac{12^2}{4}\left( 3^8 + 4 \right) \right)^2\]
(12.3)#\[r = \left| 7\cot^{-1}\left( \frac{11\pi}{6} \right) \right|\]

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).

(12.4)#\[a = x^5 z^2 - \left( \frac{7x}{2z} \right)^{5/3}\]
(12.5)#\[b = \frac{443x}{2z} + \frac{e^{-xz}}{x - z}\]
(12.6)#\[c = \frac{\ln(x)}{\sin(z)}\]
(12.7)#\[d = \log(x)\]

Part C#

  1. Download the script MA1_matrix_magic_template.m.

  2. Make sure to fill out all header information, including a short description of the code.

  3. Use MATLAB to learn what these built-in functions do: zeros(), ones(), trace(), and sum().

  4. In the INITIALIZATION section of your script file, create vector V and matrix M using the ones() 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}\]
  5. 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 of V

    b. Then, replace the bottom right 3x3 matrix of M with the second element of V

    c. Next, replace the value in the top left corner of M with the third element of V

    d. Replace the element of M in the fourth row and the second column with the fifth element of V

    e. Replace the last column of M with the last column of M multiplied by the fourth element of V

    f. Finally, multiply every element in M by the last element in V

    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}\]
  6. 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 to C.

    b. Copy from M a 2-element row vector [-4 -1] and assign it to D

    c. Create 1x4 row vector E that inserts C between the first and fifth elements in the fourth row of M 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 inserts D with the third and fifth elements in the first row of M to create the vector [-4 -1 -1 -2.5] and uses square brackets to complete the insertion in one line of code.

  7. 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 the zeros() function.

    b. Use only the matrix M and the vectors E and F to replace the center 2x2 matrix of vals, 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}\]
  8. 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 of vals

    b. Concatenate vector X to the top of matrix vals to create a new matrix, G.

    c. Create a column vector Y that contains the sums of value across each row of G.

    d. Concatenate vector Y to the right of matrix G 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.

  9. In the FORMATTED TEXT DISPLAY section of your script file, use three fprintf() statements to display your results as shown below. Use proper formatting so that you do not print more decimal places than necessary.

  10. Publish your script as ma1_ind_1_username.pdf.

  11. Save your script as ma1_ind_1_username.m.

  12. Save your flowchart in ma1_ind_1_ans_username.pdf.

  13. 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.