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.
[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.$$
irb(main):005:0> load "drop_distance.rb"
=> true
irb(main):006:0> drop_distance(2.5)
30.625=> nil
[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/s^{2}) is needed to accelerate a 3.5kg object at 2m/s^{2}? Do the calculation by hand and using your Ruby function and verify that your Ruby funcction produces the correct result.
[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?
[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.
[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 q_{c} is the critical immunization threshold and R_{0} is the disease's basic reproduction number. Test your function.
[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.
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.
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.