Skip to main content
Engineering LibreTexts

5: Examples of assignment scripts

  • Page ID
    99563
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

      An m-file for an assignment should have just the code that sets the variables and does the calculations. The teacher will run your code, so the assignment m-file does not need the results of the calculations. If you choose to put in the results, put them in as comments. To create a comment, use the % symbol.

      Example \(\PageIndex{1}\) Compute the area of a rectangle

      Assignment: Create an m-file that sets the width and height of a rectangle, then computes its area.

      Solution

      One solution is an m-file with this code:

      % Compute the area of a rectangle
      rect_width  = 3 % cm
      rect_height = 5 % cm
      rect_area   = rect_width * rect_height
      % rect_area = 15

      .

      This next example has 2 files.

      The 1st file is a script that does the calculation of the area of an isosceles triangle. It requires that the base and height of the triangle be defined before running the script. These are the 2 preconditions of the script.

      The 2nd file is a script that tests the 1st file. This 2nd file defines values to the base and height variables, then it has a line of code with the name of the 1st file. This line runs the 1st file. This 2nd file then defines a second set of values to the base and height variables and has a line of code with the name of the 1st file. This line runs the 1st file with the new values.

      Example \(\PageIndex{2}\) Using a test file to run a script with 2 sets of inputs

      Assignment: Create an 2 m-files. The 1st m-file computes computes the area of an isosceles triangle. Call this file isosceles_triangle_area.m

      It has preconditions that tri_base and tri_height are defined before running the script. 

      The second script file is a test file. Its defines this set of values:

      tri_base = 4
      tri_height = 3
      Then it runs the isosceles_triangle_area script by putting the name of the 1st script, "isosceles_triangle_area", on a line of code.

      After the area of the first triangle is computed, the test file defines this second set of values:

      tri_base = 10
      tri_height = 50

      Again, it runs the isosceles_triangle_area script by putting "isosceles_triangle_area" on a line of code.

      Solution

      The isosceles_triangle_area.m file:

      % Compute the area of an isocesles triangle
      % Preconditions (Inputs):
      % tri_base
      % tri_height
      % Postcondition (Output)
      % tri_area
      tri_area = (1/2)*tri_base*tri_height
       

      The isosceles_triangle_area_test.m file:

      % test the isosceles_triangle_area script
      % Triangle 1:
      % Define the inputs:
      tri_base = 4
      tri_height = 3
      % Compute the area:
      isosceles_triangle_area

      % Triangle 2:
      % Define the inputs:
      tri_base = 10
      tri_height = 50
      % Compute the area:
      isosceles_triangle_area

       

      .


      This page titled 5: Examples of assignment scripts is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Carey Smith.