\[ \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 | 407 words | 4 min read

12.1.2. Task 0#

Learning Objectives#

Practice writing simple arithmetic expressions in MATLAB. Practice writing simple vectors and matrices in MATLAB.

Task Instructions#

In this task, you will be writing simple arithmetic expressions and learning how to initialize vectors and matrices in MATLAB.

  1. Make a copy of the MATLAB template file ENGR133_MATLAB_Template.m and rename the file to ma1_pre_1_username.m.

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

  3. In the INITIALIZATION section, prompt the user to initialize the variables, a, b, and c to \(5\), \(1\), and \(2\) using the built-in input function.

  4. In the CALCULATION section create three variables that compute the following three equations in MATLAB using appropriate syntax and function calls where needed (Be careful with order of operations! Make sure that all trigonometric functions are computed in radians.):

    a. \(a*b^4 + \tan(c)\)

    b. \(\left( \frac{\pi}{c} - b! \right) + \frac{a}{15}\)

    c. \(b^5 + \frac{c}{4} + \sin^{-1}(1)\)

  5. In the OUTPUTS section, print the results to the command window rounded to three decimal places using the fprintf in-built function.

  6. Create a new section called VECTOR/MATRIX INITIALIZATION, in which,

    1. initialize a row vector v1 = [1, 17, 4] and

    2. initialize a \(3\) x \(3\) matrix m1 = [4, 6, 19; 3, 52, 6; 7, 9, 14].

  7. Create another section called VECTOR/MATRIX CALCULATION section, in which,

    1. replace the second value in v1 with a value of \(8\) and

    2. replace the first row in matrix m1 with the row vector v1.

  8. Allow both vector v1 and matrix m1 to be shown in the command window.

  9. Publish your script as ma1_pre_1_username.pdf.

    Refer to How to Publish in MATLAB video.

    Note

    For publishing, all user input statements must be commented out, and the corresponding values should be hardcoded, as shown below. This applies only when preparing for publishing, and it should be done for all remaining assignments in MATLAB. Remember, this only concerns user input commands and does not apply to function input arguments.

    %a = input("Enter a:");
    a = 5;
    
  10. Save your script as ma1_pre_1_username.m.

  11. Save your flowchart as ma1_pre_username.pdf.

  12. Submit all three files to Gradescope.

Sample Output#

Use the values in Table 12.2 below to test your program.

Table 12.2 Test Cases#

Case

a

b

c

1

5

1

2

Ensure your program’s output matches the provided samples exactly. This includes all characters, white space, and punctuation. In the samples, user input is highlighted like this for clarity, but your program should not highlight user input in this way.

Case 1 Sample Output

>> ma1_pre_1_username Enter a:5 Enter b:1 Enter c:2 The result to the first calculation is 2.815 The result to the second calculation is 0.904 The result to the third calculation is 3.071

v1 =

1 8 4

m1 =

1 8 4 3 52 6 7 9 14