15110 Fall 2011 [Cortina/von Ronne]

Programming Assignment 2 - due Tuesday, September 13

Overview

For this assignment, you will create a Ruby source file (that is, a text file containing Ruby code) for each of the problems below. You should store all of these files in a folder named pa2.

Note: You are responsible for protecting your solutions to these problems from being seen by other students either physically (e.g., by looking over your shoulder) or electronically. In particular, since the lab machines use the Andrew File System to share files worldwide machines, you need to be careful that you do not put files in a place that is publicly accessible.

If you are doing the assignment on the Gates Hall Cluster machines we use in lab, our recommendation is that you should place your pa2 directory under ~/private/15110. that is, the new directory pa2 is inside a directory called 15110, which is inside the directory called private, which is inside your home directory. (Every new andrew account is set up with a private subdirectory within the account's home directory that only that account can access.) Please refer to Setting up Directories for instructions on creating and managing your directories.

The Formulas

  1. [correction] [1 point] The distance d in meters that an object dropped in a vacuum near the surface of the earth will fall in t seconds can be described by formula, $$d = \frac{1}{2}9.8t^2.$$

    1. Use a calculator to check the value of d when t is 2.5. You should find that d is 30.625.
    2. Now, define a Ruby function drop_distance(t) (that is, a function named drop_distance that takes a single parameter named t) and prints out the corresponding value of d. Put the text of your function in a file named drop_distance.rb in your pa2 directory.
    3. After you've saved the file you can test it with irb using the commands:
      irb(main):005:0> load "drop_distance.rb"
      => true
      irb(main):006:0> drop_distance(2.5)
      30.625=> nil
  2. [1 point] Consider Newton's second law, which can be expressed by the formula, $$F = ma.$$ Define a Ruby function force(m,a) that prints out the amount of force needed to cause an acceleration equal to a on an object that has a mass of m, placing the function in force.rb. What force in Newtons (1 N = 1 kg m/s2) is needed to accelerate a 3.5kg object at 2m/s2? Do the calculation by hand and using your Ruby function and verify that your Ruby funcction produces the correct result.

  3. [1 point] Consider the Pythagorean formula, $$c = \sqrt{a^2 + b^2}.$$ In hypotenuse.rb, define a Ruby function hypotenuse(a,b) that returns the length in meters of the hypotenuse of a right triangle that has legs of length a and b meters, respectively. Test the return value of hypotenuse(3,4). What value should it return?

  4. [1 point] Define a Ruby function in sphere_volume.rb called sphere_volume(r) that calculates and returns the volume of a sphere (in cubic meters) with radius r (in meters) using the equation $$V = \frac{4}{3}\pi r^3.$$ Pick a few different values for the radius, calculate the volume for those radii by hand, and compare those with the result of your sphere_volume(r) function.

  5. [1 point] Define a Ruby function in immunization_threshold.rb called immunization_threshold(reproduction_number) that calculates and returns the minimum proportion of the population that must be immunized for a disease, with a given basic reproduction number, to die out. (In epidemiology, the basic reproduction number of a disease is the number of people an individual with the disease will infect when no-one has any pre-existing immunity; if the basic reproduction number is above 1 (e.g., 2.5), the disease will spread in the absence of immunization.) The minimum proportion that needs to be immunized can be calculated using by the formula, $$q_c = 1 - \frac{1}{R_0}.$$ where qc is the critical immunization threshold and R0 is the disease's basic reproduction number. Test your function.

  6. [1 point] Define a Ruby function called future_val(present_value, interest_percentage, num_periods) in future_val.rb that returns the future value F of some present value to which interest has been added for some number of periods at the given interest rate, each period. This can be calculated according to the formula, $$F = P(1 + i/100)^n.$$ where P is the present value, i is the interest rate as a percent (i=3 for 3%), and n is the number of periods. Test your function.

  7. Consider a cubic polynomial of the form, $$f(x) = c_3x^3 + c_2x^2 + c_1x + c_0.$$ Define a Ruby function called compute_poly1(c3, c2, c1, c0) that prints a table of the values of f(x) for x = 1 to x = 50 (inclusive), given the coefficients of the polynomial in the order shown. (You should use a loop rather than repeating the code for the calculation 50 times.) Use the formula directly to compute these values. For example, if your polynomial is $$f(x) = 2x^3 + x^2 + 3x + 5,$$ then the output would look like:
    irb(main):048:0>  compute_poly1(2,1,3,5) 
    f(1) = 11
    f(2) = 31
    f(3) = 77
    f(4) = 161
    (etc.)
    

    Be sure to test your function using a few more polynomials besides the one we gave you to be more confident that your function is correct.

  8. An alternate way to compute the values of a cubic function (as in problem 7) is to use the method of finite differences that was used in Babbage's Difference Engine.

    Define a Ruby function called compute_poly2(d3f0, d2f0, d1f0, f0) that prints a table of the values of f(x) for x = 1 to x = 50 (inclusive), given the values of $$\Delta^3f(0), \Delta^2f(0), \Delta^1f(0), \text{ and } f(0),$$ respectively. Your function should use the method of finite differences to compute the function values.

    For example, if your polynomial is $$f(x) = 2x^3 + x^2 + 3x + 5,$$ then $$\Delta^3f(0) = 12,$$ $$\Delta^2f(0) = 14,$$ $$\Delta f(0) = 6, \text{ and }$$ $$f(0) = 5$$ (NOTE: Make sure you know where these values came from!) and the output of this function would look like:

    irb(main):048:0>  compute_poly2(12,14,6,5)  
    f(1) = 11
    f(2) = 31
    f(3) = 77
    f(4) = 161
    (etc.)
    

    Be sure to test your function using a few more polynomials besides the one we gave you to be more confident that your function is correct.

Submission

You should now have a pa2 directories that contains the files drop_distance.rb, force.rb, hypotenuse.rb, sphere_volume.rb, future_val.rb, immunization_threshold.rb, compute_poly1.rb, and compute_poly2.rb, each containing the corresponding function. Zip up your directory and upload it using the handin system. (The handin system will accept submissions beginning on Friday until the deadline Tuesday night.)