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.

  1. Write a function that has one input argument, a row vector, v and one output argument, a row vector w that is of the same length as v. The vector w contains all the elements of v, but in the exact opposite order. For example, is v is equal to [1 2 3] then w must be equal to [3 2 1]. You are not allowed to use the built-in function flip.
  2. 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.
  3. Write a function that is called like this: amag = accelerate(F1,F2,m). F1 and F2 are three-element column vectors that represent two forces applied to a single object. The argument m equals the mass of the object in units of kilograms. The three elements of each force equal the x, y, and z components of the force in Newtons. The output variable amag 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: F = ma, where F is the sum of F1 and F2. Then it returns the magnitude of a.
  4. 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.