14.4. Practice with Vectors and Functions¶
For this assignment, we will practice writing some simple functions that also use vectors. We will work on these simple programs in class to ensure that everyone is comfortable writing functions and working with data that is in a vector (array).
MATLAB Grader Practice with Vectors and Functions assignment page.
- Write a function that has one input argument, a row vector,
v
and one output argument, a row vectorw
that is of the same length asv
. The vectorw
contains all the elements ofv
, but in the exact opposite order. For example, isv
is equal to [1 2 3] thenw
must be equal to [3 2 1]. You are not allowed to use the built-in function flip. - Write a function that takes as input a row vector of distances in kilometers and returns two row vectors of the same length. Each element of the first output argument is the time in minutes that light would take to travel the distance specified by the corresponding element of the input vector. To check your math, it takes a little more than 8 minutes for sunlight to reach Earth which is 150 million kilometers away. The second output contains the input distances converted to miles. Assume that the speed of light is 300,000 km/s and that one mile equals 1.609 km.
- Write a function that is called like this:
amag = accelerate(F1,F2,m)
.F1
andF2
are three-element column vectors that represent two forces applied to a single object. The argumentm
equals the mass of the object in units of kilograms. The three elements of each force equal thex
,y
, andz
components of the force in Newtons. The output variableamag
is a scalar that is equal to the magnitude of the object’s acceleration. The function calculates the object’s acceleration vector a by using Newton’s law: , where is the sum ofF1
andF2
. Then it returns the magnitude ofa
. - Write a function called income that takes two row vectors of the same length as input arguments. The first vector, rate contains the number of various products a company manufactures per hour simultaneously. The second vector, price includes the corresponding per item price they sell the given product for. The function must return the overall income the company generates in a week assuming a 6-day work week and two 8-hour long shifts per day.