SUNY Geneseo Department of Mathematics

Lab 1—Expressions and Functions

Math 230 02
Spring 2017
Prof. Doug Baldwin

Complete by Monday, January 30
Grade by Thursday, February 2

Purpose

This lesson develops your ability to (1) write calculations as Matlab expressions, (2) define functions that carry out a simple calculation, and (3) use programmer-defined functions to solve problems.

Background

This lesson is based on material in covered in classes on January 23 and 25. This material was introduced through two video tutorials, namely on arithmetic (at https://www.youtube.com/watch?v=yAer1yVLBLk) and function definitions (at https://www.youtube.com/watch?v=QYOUq2KI1uo).

The second problem requires some knowledge of counting and statistics that you can find in the following videos.

Activity

Write Matlab functions as directed below. Also use calls to your functions from the command window to solve the specific problems described below.

Problem 1

Write (at least) two functions that find values of x at which polynomials of the form ax2 + bx + c are zero. Recall that the quadratic equation says that the polynomial is 0 at the two values x = (-b - √(b2 - 4ac)) / 2a and x = (-b + √(b2 - 4ac)) / 2a. One of your functions should return the first of these values and the other should return the second. Both functions should take a, b, and c as arguments. If you find it helpful to define one or more additional functions, you may do so.

Test your functions by calling them from the command window to find zeros of x2 - 2x - 3 and 3x2 + x + 2. The first of these is 0 at x = -1 and x = 3. The second has complex roots, which Matlab should find. The second polynomial also tests a common mistake in writing code for the quadratic equation — be sure you know in advance what the roots are supposed to be, and that your function actually returns the values you expect. This strategy of knowing in advance what a program should do, and then checking that it really does, is central to any program testing.

Problem 2

The current (as of January, 2017) rules for winning the top two prizes in the Powerball lottery are as follows: each lottery drawing consists of 5 distinct numbers chosen randomly from the set 1 through 69, plus 1 number (the Powerball) chosen from the set 1 through 26. To play the lottery, you basically guess what the numbers will be. If you guess all 6 correctly, you win the jackpot. If you guess the first 5 correctly but not the Powerball, you win a second-place prize of $1,000,000. Note that the order in which you guess the 5 regular numbers relative to the order in which they are drawn doesn’t matter — for example if you pick 1, 2, 3, 4, 5, and the drawing is 5, 4, 3, 2, 1, you still have a match.

Powerball as described above can be generalized to a game in which the number of balls to match is any positive integer, k, the numbers on the balls range from 1 to n, and the numbers on the Powerball range from 1 to p. In the following, I’ll call this generalized game “k of n plus p Powerball” (So, for example, standard Powerball is “5 of 69 plus 26 Powerball.”)

Write Matlab functions to calculate the answers to the following questions. Later questions generally build on the results of earlier ones, so you may find it helpful to call earlier functions from later ones.

Step 1

Write a function that takes two positive integers, k and n, as its arguments and that calculates how many possible sets of k numbers between 1 and n there are.

Step 2

Write another function that takes two positive integers, k and n, as its arguments and that calculates the probability that a player who buys 1 ticket will match the k regular numbers in k of n plus p PowerBall. (Note that p doesn’t matter to this calculation, and so needn’t appear in your function.)

Step 3

Write a function that takes three positive integers, k, n, and p as its arguments and calculates the probability that a player who buys 1 ticket will win the jackpot in k of n plus p Powerball (i.e., will match the k regular numbers and the Powerball).

Step 4

Write a function that takes three positive integers, k, n, and p as its arguments and calculates the probability that a player who buys 1 ticket will win a second-place prize in k of n plus p Powerball. Warning: this is not quite the probability of matching the k regular numbers!

Step 5

Write one last function that takes three positive integers, k, n, and p, and a real number, J, as its arguments and that calculates the expected winnings of a player who buys one ticket in k of n plus p Powerball, assuming the jackpot is J dollars and that the second prize is always $1,000,000. Assume that the only ways of winning are to win the jackpot, or to win the second place prize, and ignore the cost of buying the ticket.

Step 6

Finally, test your functions by finding the probabilities of winning the jackpot and second prize in 5 of 69 plus 26 Powerball, i.e., the standard game. You can check your answers against the official odds at http://www.powerball.com/powerball/pb_prizes.asp. Also calculate the expected winnings in 5 of 69 plus 26 Powerball if the jackpot is $300,000,000.

Follow-Up

I will grade this exercise in a face-to-face meeting with you. During this meeting I will look at your solution, ask you any questions I have about it, answer questions you have, etc. Please bring your solution on your computer, as that will speed the process along.

Sign up for a meeting via Google calendar. Please make the meeting 15 minutes long, and schedule it to finish before the end of the “Grade By” date above.