Assignment 04

Due: Wednesday, February 26, 2014 at noon 100 pts

Now, in order to compute ex for some input value of x, you will use the fact

ex = 1 + x1/1! + x2/2! + x3/3! + x4/4! + x5/5! + ...

Once again, this is way coooool. But the unfortunate thing is that you don't have infinite time to calculate this forever. So, once again, you will go for an approximation. You will include the first 6 terms (as I have shown you here before the "+ ..."). This won't be the best approximation, but oh well. Ralph won't know any different. Note: this is not an iterative process; you are just adding in terms. The more terms you add in, the closer you get to the answer. For the curious, this is a truncated Maclaurin series and you will learn about them in calc II. If you would like to view Ralph's understanding of what an exponential curve looks like, just click on the picture of Ralph above.

Now, it should be clear from what I have shown you above, the most difficult concept in math you need to understand in this assignment is polynomials. If you have trouble with any of this, don't hesitate to ask your instructor. Don't, under any circumstances, ask Ralph.

When you submit: As usual, when you submit, the program will run. You are to input the following:

  • choose option 2

  • upon reading your error message, choose option 1 and input 5

  • choose option 2

  • choose option 1 and input 0.69315

  • choose option 4

  • choose option 1 and input 4

  • choose option 3

  • quit

Optional: Once again, if this isn't enough to keep you busy programming, there are several things you can do to add to the program to make it more robust. Here are some ideas. And if you want bonus points for your work, negotiate with your instructor.

  • for the fourth root option, query the user for the root value. For example, the menu option would read "nth root". Upon choosing that option, input the positive int n for calculating x1/n. Then look up Newton's method (or come see me) for the math behind the formula so you can write a general formula for the nth root.

  • for the root option, input from the user how many iterations they wish to employ

  • for the root option, input the precision they wish for their answer

  • for the exponential, input the number of terms to be included in the sum

  • and many many more!!

As usual, be sure to ask if you have any questions.

Instructions: You know how to submit; do it in the usual way.Background: Oh Ralph, poor kid. Now he wants your help to impress his (perceived) girlfriend, Lisa. As you might know, Lisa is a genius and knows a lot of math. He hears her say things like factorial, square root, sine, hyperbolic functions, this is a Hausdorff space and that isn't, and he really wants to know how to spell Fermat and Bieberbach. So, you are going to write a program that will do some math for Ralph. Of course, you need to understand some basic math concepts. Besides learning some coding in C++ in this assignment, you will also learn some math. Don't worry, it will only hurt a little.Specifications: Your program is to be menu-driven. That means that your program will present a menu of choices for the user and will terminate only when he/she chooses 'quit' as an option. (Pi will not be on the menu.) The menu you will present will have 5 options and look something like this: MENU

1. Enter a positive real number, x

2. Factorial of x

3. Fourth root of x

4. X-ponential of x (exp(x))

5. Quit

Before you have a heart attack, we will show you how to compute the fourth root of a number, and ex. All you really need to know is what a polynomial is. First, you must write the code for this program so that anyone choosing options 2, 3, or 4 before choosing option 1 will get a error message informing them they must first enter a value of x to work with. Also, each time an option is chosen, the indicated value is computed and displayed and then the menu is presented again. You are to use the switch-case statement to handle the menu choices, and you are to use appropriate loops to compute the indicated mathematical values. You are not to use the cmath library; you are to compute using the math shown below. For the first option (calculating a factorial), cast the input value to an integer and then continue the calculation. That casting is only temporarily for the first option only. Hence, an input of 8.3 would have 8! returned by your code.

Math: Ok, so here is some math. First, you must know what K ! is for some non-negative integer is. Here is how it is defined:

K ! = 1 x 2 x 3 x 4 x 5 x ... x K, for positive integer K, and

0 ! = 1.

Note: I'm using K since I don't want to confuse X with the "times" symbol, x. Thus, 4 ! = 4 x 3 x 2 x 1 = 24 and 7 ! = 5040.

Second, you will code an iterative method (Newton's method) to compute the fourth root of x. What that means is that you compute a new x value based on the old x value. Here's the formula:

xn+1 = (3xn + A/xn3) / 4 for n = 0, 1, 2, ...

So, A is the number you are trying to find the fourth root of (e.g. fourth root of 16 is 2 since 24 is 16), x0 is an initial guess, and

x1 = (3x0+ A/x03) / 4,

x2 = (3x1+ A/x13) / 4,

x3 = (3x2+ A/x23) / 4,

etc.

If you keep doing this forever, the last x value you compute is the fourth root of A. Cooool? Well, yeeaaahhhh, sort of. You don't have infinite time, hence you cannot iterate forever. But, Newton's method "converges" to a value very close to the answer really quickly no matter what your first guess is. So, you will code it to repeat this computation just 6 times and use an initial guess (that's x0) of 20 regardless of what you are trying to take the fourth root of (that's A). (And to be sure you understand, A is the value you prompt for in option 1.) Try this with your calculator: in the formula above, let A be 16 and start with 20 as an initial guess at the 4th root. You'll see that the xn's get really close to 2 pretty fast.