## Hot Air Balloons - 2 (Program: "balroot")

In the previous exercise, each time you executed the program you entered a value for altitude, and the program evaluated the payload that could be lifted to that altitude. The more adventurous among you may have read Edgar Allen Poe's classic short stories, including "The Unparalleled Adventure of One Hans Pfaall" (~1835), describing how a Dutch adventurer ballooned to the moon (about 200,000km). Just for fun, grab some oxygen and try to see what would happen if you entered an altitude of say 2e8m on the keyboard. You may be intrigued by the response - what in the atmosphere does it mean? Essentially you are attempting to use the equations beyond their specified domain of usage (0 to 11,000m). I shudder at the thought of what would happen if you attempted an altitude of -6,380,000m, which would bring you directly to the center of the earth! (Note: to enter this value type: -6.38e6). In this exercise you will modify your payload function find_payload by trapping the condition of an out-of-range altitude, and returning a value of zero in this case.

Consider again the sequence of equations that we used to evaluate the payload lifted to a certain altitude:

Notice that the independent variable is the altitude (z), thus given the value of altitude we can determine the amount of payload that can be lifted to that altitude. However the problem is normally posed in the reverse order. We normally know what the payload is and would like to know to what altitude we can lift this specific payload. Unfortunately the equations given above cannot be easily solved in the reverse order.

The approach that you used in the previous exercise was the 'brute force' approach: obtain enough (altitude, payload) coordinate pairs for a suitable plot of the payload curve. Thus each time you executed the program you obtained a single coordinate pair. Furthermore you had to painstakingly plot the payload curve by hand, so as to determine the altitude reached for a certain specific payload mass. This was a time consuming task however you probably derived a great feeling of satisfaction in completing it.

In this exercise you will use a "root-finding" method to extend your previous program, which will allow you to evaluate the altitude reached for a certain specific payload mass without having to plot the payload curve. This will be done by adding two new methods to our Balloon class: find_altitude and dif_payload. Essentially we would like to find the value of altitude that will make the value returned by method find_payload equal to the required payload.

Consider the method dif_payload, which is defined as follows:

 ``` float dif_payload(float altitude, float req_payload){ // returns difference in payload - the root of dif_payload // is the altitude which balloon can attain with req_payload return (find_payload(altitude) - req_payload); }```

Thus the value of altitude that makes the value returned by find_payload equal to the required payload req_payload is the same value of altitude that makes dif_payload equal zero. In other words we would like to determine the root of the function dif_payload. In lab5 you will develop a function sin_root to evaluate the root of the function sin(x) using the Bisection method. This is a very similar problem, hence you will be able to apply the function that you develop in lab5 directly to this exercise.

This exercise is an extension of the previous exercise in that you will need to modify the main function module, the method find_payload, and add two new methods to the Balloon class: find_altitude and dif_payload. One approach to developing this program is to first copy your balloon.cpp program to a new file balroot.cpp, and then modify balroot.cpp using the vi (or nano) editor as follows:

```cp balloon.cpp balroot.cpp
vi balroot.cpp```

The structure diagram of the modified program follows:

• Before modifying the method find_payload we will first change its structure by separating the method prototype from the method definition, similar to what is normally done with functions (refer: Friedman & Koffman Chapter 3, Section 3.4 "Function Prototypes"). Thus the method prototype will remain in the class and the method definition will be added to the end of the main function. The format of the method prototype is similar to a function prototype as follows:
//===============================================================
float
//
determine balloon payload [kg] as function of altitude [m]
//
pre: all private and public variables & altitude have values
//
post: value of payload is returned
//
if altitude negative or >11,000m payload is not evaluated
//
and payload mass of zero is returned
//
===============================================================
The method definition follows the main function and uses the "double colon" modifier to associate it with the class Balloon. It would be typically as shown below. Note that the pre- and post-conditions should be written both in the prototype and in the method definition.
//================================================================
float
{ //
determine balloon payload [kg] as function of altitude [m]
|
| (the method definition goes here)
|
return
}
-----------------
• Modify the method definition find_payload so as to limit the altitude range to within 0 and 11,000 meters. This should be done by means of an if statement within the method find_payload, i.e. the function should protect itself from abuse, and not rely on the main program to supply valid altitude data. If in fact the value of altitude supplied is out-of-range, the function should print a warning message and return a payload value of zero.
-----------------
• Develop a new method dif_payload which has a two arguments altitude and req_payload, and is defined as shown above. Notice that since this method is a "one-liner" it does not require a separate prototype.
-----------------
• Develop a new method find_altitude which will evaluate the root of the function dif_payload between specified limits of altitude and within a specified accuracy epsilon. This method should be very similar to the function sin_root that you will develop in lab5, thus this is an important lab to attend and understand.
-----------------
• Finally, modify the main function to enter data and invoke the new functions as in the above structure diagram.

Notice that function find_payload is not invoked directly by the main function, but only indirectly through function find_altitude. Recall that you are not attempting to determine a value of payload, but the altitude reached for a specific required payload.

A typical program execution output follows:

```enter balloon volume (typically 1000 cu.m):
1000
enter balloon empty mass (typ. 100)[kg]
100
balloon initialised as follows:
balloon volume: 1000[cu.m]
balloon empty mass: 100[kg]
standard atmospheric conditions:
temperature lapse rate: -0.0065[deg C/m]
temperature at sea level: 15[deg C]
standard pressure at sea level: 101325[Pa]
find altitude as a function of required payload
150