\[ \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 | 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#

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

  2. Open up the Answer Sheet (MA1_Team_teamnumber.docx). Save this file as ma1_team_teamnumber.pdf.

    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.

    b. Answer the questions for Task #2 Part A.

Part B: Common Functions in MATLAB#

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

  2. 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 the sin function. Use the help 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

  3. Once you have tested all the commands, answer the remaining question on the Answer Sheet.

Part C: Vectors and Matrices in MATLAB#

  1. In the same script ma1_team_2_teamnumber.m. Use clear and clc in the Command Window to remove any data and output from previous scripts.

  2. Create the matrix shown below:

       Amatrix = [ 1   1   1   1;
                  10  20  30  40;
                   2   4   6   8;
                 100 200 300 400]
    
  3. 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)

  4. 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 terms array indexing, colon, sort, and linspace.

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

    • Create Xmatrix with values identical to Amatrix except the second row is Bvector.

    • Create Yvector by extracting the third row in Amatrix.

    • Extract the element in row 2, column 3 of Amatrix and assign it to the variable z.

    • Replace the value in Amatrix row 1, column 1 with the value 55.

  6. Save your script as ma1_team_2_teamnumber.m.

  7. Save your script as ma1_team_2_teamnumber.py.