Skip to main content
Library homepage
 
Engineering LibreTexts

5.5.1: Multiple Inputs Examples and Exercises (Smith)

  • 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) (4 pts) Create a function m-file called rocket_height.m

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

    Write these comment lines 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.

    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.

    This function has 1 output variable:

    h % (m) height as a function of time

    (Note the variable name, h, must be a different than the function name, height)

    (b) (4 pts) Inside the rocket_height.m function, do the following:

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

    b.2 (3 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

    (c) (4 pts) Create a test file that does the following:

    c.1 It defines these variables:

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

    h1 = 100 % (m)

    v1 = 15 % (m/s)

    c.2 It calls the height function

    h_out = rocket_height(t, h1, v1);

    c.3 It opens a new figure window.

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

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

    Answer

    Add texts here. Do not delete this text first.

    .


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