By Carey A. Smith
More information on creating function is at:
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.
Add example text here.
Function syntax: result = add1(a,b)
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;
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)
Add example text here.
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.