All work must be submitted by 7:50am on Tuesday, Feb 2. Submit the programming work online to http://culearn.colorado.edu. No late work is accepted, so submit what you have before the due date!
You may talk with other students and instructors about the assignments, but you may not look at or copy code written by others. The penalty for violating this code is an F for the entire semester.
Starting with this homework, all your work must follow these ten style items. Thank you! (See snowmen.cxx for examples of this style.)
This week, we will learn about writing your own non-void functions to compute numerical value that is sent back to the main program (or elsewhere). We've already used several of these functions (such as sin and cos), but now you'll write four of your own. These particular four functions will later be used in HW 4 that simulates the orbit of a planet.
Your functions must have these exact prototypes. You may copy and paste these descriptions into your program, which must be called functions.cxx:
//----------------------------------------------------------------------------- // Function Prototypes // A function to calculate the solution to Kepler's equation. The equation is // x = ma + ecc*sin(x), and the answer returned is x, within EPSILON. double kepler_solution(double ma, double ecc); // orbit_angle(ecc, period, t) computes the angle of a planet's position // in polar coordinates at time t. ecc is the ecentricity of the orbit, // and period is the time for one full orbit. period and t must be in the // same units. double orbit_angle(double ecc, double period, double t); // orbit_dist(a, ecc, theta) computes the distance of a planet // from the sun when it is at angle theta in polar coordinates. // a is the semi-major axis; ecc is the orbit's eccentricity. The units // of the answer are the same as the units of a. double orbit_dist(double a, double ecc, double theta); // pixel(v, v0, v1, pmax) converts v from an interval that ranges from v0 to v1 // into an integer interval that ranges from 0 to pmax. Note that when v = v0, // the answer is always 0, and when v = v1, the answer is always pmax. // Example 1: Suppose an x-coordinate called x ranges from -2 (on the left // side of the screen) to +3 (on the right side of the screen), and // we want to figure out the corresponding pixel x coordinate for a screen // that is 400 pixel wide. Then call pixel(x, -2, 3, 400). // Example 2: Suppose a y-coordinate called y ranges from +4 (on the top // of the screen) to -2 (on the bottom of the screen), and // we want to figure out the corresponding pixel y coordinate for a screen // that is 300 pixels tall. Then call pixel(y, 4, -2, 300). Note that in this // case v0 > v1 since we want bigger coordinates at the top of the screen. int pixel(double v, double v0, double v1, int pmax); //-----------------------------------------------------------------------------
Mathematicians and astronomers (beginning with Kepler) have spent much effort trying to find a technique that solves for the unknown value x in this equation. Here is the technique that we will use:
Declare a global named constant, EPSILON to be 0.001.
Then start with a guess of x = 0. Then calculate how much
of an error this guess has. The error amount is just the absolute
value of the left side of the equation (x) minus the right side of the
equation (ma + ecc*sin(x)). You can use the function
abs from <cmath> to compute an
absolute value. If this error is less than EPSILON, then
you are done (return this value of x as your answer). But if the error
is greater than or equal to epsilon, then change your new guess to be
the right side of the equation (ma + ecc*sin(x)), and then
check the error again. Keep going until you get an error that's below
EPSILON.
Calculating the orbit angle requires three steps:
<cmath>.
The orbit distance is computed as: a*(1 - ecc*ecc)/(1 + ecc*cos(theta))
You'll need to figure out this algorithm yourself. Draw some pictures! You'll probably figure out how to compute the answer as a double number first, but then convert it to int before returning it (without any warnings from the compiler!)
You must write a short interactive main program that lets the user test each of the functions once. Here is a sample dialog with the program. You must match this exactly (otherwise, the TA's grading program will fail your work!) (Note: If your answers are only slightly different, that's usually okay. It's caused by doing arithmetic operations in slightly different orders.) Note the message at the end, too! (The numbers in boldface are the ones typed by the example user.)
Please type ma and ecc for testing kepler_solution: 2.2 0.2 kepler_solution is 2.3436 Please type ecc, period and t for testing orbit_angle: 0.2 60 9.1 orbit_angle is 1.32472 Please type a, ecc and theta for testing orbit_dist: 0.34 0.2 2.34 orbit_dist is 0.379144 Please type v, v0, v1 and pmax for testing pixel: 0.38 -0.68 0.68 400 pixel is 311 Thank you, Mr. Kepler.
orbit_angle must call your own
kepler_solution function to do Step 2.