The due date is 10pm on Friday, Sep 23.
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 the these 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 a 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 five of your own. These particular five functions will later be used in our next homework 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. If you wish, you may copy Michael's version of the pixel code and put it in your homework---but in that case, please put a note with the code to indicate where it came from!
//----------------------------------------------------------------------------- // Function Prototypes // sr(double z)--- This function uses the iterative technique shown in // class to find a value of x that is very close to solving the // equation: x = 0.5(x + z/x) // As we did in class, the while-loop stops when the error in x is // less than 0.001. Notes: The initial guess for the equation must be // 1.0. The input parameter, z, must be positive. The answer should be // close to the square root of z. double sr(double z); // pixel(wx, w0, w1, p0, p1) converts wx from an interval that ranges from w0 to w1 // into an integer interval that ranges from p0 to p1. Note that when wx = w0, // the answer is always p0, and when wx = w1, the answer is always p1. // 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, 0, 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, 0, 300). Note that in this // case w0 > w1 since we want bigger coordinates at the top of the screen. int pixel(double wx, double w0, double w1, int p0, int p1); // 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); //-----------------------------------------------------------------------------
I'll develop this function in class, so all you need to do is insert it in your code with a comment that indicates where it came from!
You must use the same technique we used in class to iteratively solve an equation that involves one unknown variable, x. In this case, the equation is x = 0.5(x + z/x). The value z is a given value (a parameter to the sr function). The initial guess is 1.0, and the loop should stop when the error is below 0.001.
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 must write a short interactive main program that lets the user test each of the functions once. At the end, it prints a thank-you message, "Thank you, Mr. Kepler." 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--within 0.001--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 wx, w0, w1, p0 and p1 for testing pixel: 0.38 -0.68 0.68 0 400 pixel is 311 Please type z for testing sr: 200 the square root of z is approximately 14.1421 Thank you, Mr. Kepler.
orbit_angle must call your own
kepler_solution function to do Step 2.