# 5.5.1: Multiple Inputs Examples and Exercises (Smith)

$$\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

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

##### Example $$\PageIndex{2}$$ 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

.

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

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'

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.

.

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 (Smith) is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.