Skip to main content
Engineering LibreTexts

5.5.1: Multiple Inputs Examples and Exercises

  • Page ID
    84403
  • \( \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}}\)

    By Carey A. Smith

    More information on creating function is at:

    https://www.mathworks.com/help/matlab/ref/function.html?s_tid=doc_ta

    Example \(\PageIndex{1}\) atan2(y,x) is a built-in function with 2 inputs

    Syntax: angle = atan2(y, x). It computes the arctangent of (y/x).
    Having the 2 inputs, it can tell which quadrant the angle is in.
    Thus, atan2(1, -2) = 2.6779 radians, while atan2(-1, 2) = -0.4636 radians.
    By contrast, the corresponding function with 1 input is atan().

    atan(-1/2) = -0.4636 radians; it can’t distinguish the 2 cases.

    Solution

    Add example text here.

    Example \(\PageIndex{2}\) add1(a,b)

    Function syntax: result = add1(a,b)

    % add1.m:
    function result = add1(a,b)
    % This adds vectors a and b
    % The inputs are a and b
    % a and b need to be vectors of the same length
    % When there are 2 or more inputs, the inputs are separate by commas
    % The output is "result"
    result = a + b;
    end

    Test code: add_test1.m:
    % Clear any variables etc.
    clear all; close all; format compact; clc;
    x = 1:4
    y = [0, 1, 10, 100]
    z = add1(x,y)

    Solution

    Add example text here.

    .

    Exercise \(\PageIndex{1}\) Rocket Height

    A student launches a model rocket that has no parachute.

    a. (5 pts) Create a function m-file called rocket_height_your_name.m

    (Don't use height.m--that's a built-in function.)

    The function has 3 input variables (t, h1, v1) and 1 output variable, h.

    b. (3 pts) Write these comment lines at the beginning of the function file that describe what the function does.

    % This function computes the height of a model rocket vs. time after the engine burns out.

    % t % (s) time vector, starting at engine burn out.

    % h1 % (m) height at engine burn out.

    % v1 % (m/s) velocity at engine burn out.

    h % (m) height as a function of time (output)

    When the engine burns out, the rocket is at a height of h1 meters with an upward velocity of v1.

    At this point, gravity is the only force. (The rocket is aerodynamic, so ignore air friction.)

    The rocket will continue to move up for a while, then come down.(Note the variable name, h, must be a different than the function name, height)

    c. (3 pts) Inside the rocket_height_your_name.m function file, do the following:

    c.1 (1 pt) Set a local variable: g = 9.8 % m/s^2

    c.2 (2 pts) Compute the formula for the height of the rocket as a function of time:

    h = h1 + v1*t + (-1/2)*g*t.^2; %(m) height

    d. (5 pts) Create a test file that does the following:

    d.1 It defines these variables:

    t = 0 : 0.1 : 6.3; % (s) time vector

    h1 = 100 % (m)

    v1 = 15 % (m/s)

    d.2 It calls the height function

    h_out = rocket_height(t,h1, v1);

    d.3 It opens a new figure window.

    d.4 It plots h_out vs. t. (t is the x-axis. h_out is the y-axis)

    d.5 It turns the grid on and labels the x and y axes.

    Answer

    Add texts here. Do not delete this text first.

    .

    Exercise \(\PageIndex{2}\) rectangle_draw

    Write a function rectangle_draw_YourName. It has 3 inputs and no output variables, but it does create a plot.

    % The function's declaration (1st line) is:

    function [ ] = rectangle_draw(width, height, color)

    It has 3 input variables: 
    %  width (x direction)

    %  height (y direction)

    %  color: Can be any one these: 'b', 'c', 'g', 'k', 'r', or 'y'

    % The [ ] in the function's declaration indicates that it has no output variable. It draws a figure instead of computing an output value.

    Part A. The function will draw individual 4 lines to create the rectangle.

    Inside the function, open a figure and set hold on with these 2 lines of code, so that all the lines will be plotted on 1 plot:

    figure

    hold on

    Then draw 4 line segments to create a rectangle. Think of the base paths on a baseball field. The algorithm is as follows.

    %% The 1st line segment is like going from home plate to 1st base.

    % The code to draw this line needs to specify the starting and ending x and y coordinates for each line segment.

    % This line is horizontal, so the y coordinates of both points are the same.

    % Home to 1st base:
    x1 = [0, width];
    y1 = [0, 0];
    plot(x1, y1, color, 'LineWidth', 10)

    % Note that the x and y vectors have only 2 values.

    %% The 2nd line segment is like going from 1st to 2nd base

    % This line is vertical, so the x coordinates of both points are the same.

    %% 1st base to 2nd base:
    x2 = [width, width];
    y2 = [0,     height];
    plot(x2, y2, color, 'LineWidth', 10)

    %% The 3rd line segment is like going from 2nd to 3rd base

    % Try to figure this out.

    %% The 4th line segment is like going from 3rd base to home plate

    % Try to figure this out.

    Part B.

    %% Write a test file that tests your function with these inputs:

    width = 40;

    height = 30; 

    color = your choice of 'b', 'c', 'g', 'k', 'r', or 'y'

    In your test file after calling your Rectangle_draw function, add these lines to provide a 10% white-space margin around your rectangle.

      xlim([-0.1*width, 1.1*width])
      ylim([-0.1*height, 1.1*height])

    These functions adjust the x and y axis of the plot, but don't change the rectangle.

    .

    Partial Answer

    The code to draw the 1st 2 lines is given above. The code to drw the 3rd is:

    %% 2nd base to 3rd base:
    x3 = [width,  0];
    y3 = [height, height];
    plot(x3,y3,color,'LineWidth',10)

    .

    .


    This page titled 5.5.1: Multiple Inputs Examples and Exercises is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.