So, I've been assigned to write a program that reads the wind-tunnel test data, that of which is called the "tunnel.dat" in my program, and then allows the user to enter a flight-path angle. If the angle entered is within the bounds of the data set, the program would need to do linear interpolation to compute the corresponding coefficient of lift, given by

f_b = f_a + ((b-a)/(a-c)) * (f_c - f_a)

The problem I'm having is that the program has successfully compiled, however the result isn't turning out as it should be. It's returning "The coefficient of lift is inf." where it should be a value given and computed from the arrays. The loop doesn't stop, or I mean that the program continues running even though it has returned "inf".

I'm not sure where my problem stands because my function seems to be correct, but any helpful input would be great, thank you :)

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
using namespace std;

const int N = 17;

int main()
{
        double angle[N], clift[N];          //Declare and initialize objects.
        double fl_ang, result;
        ifstream fin("tunnel.dat");

        if (fin.fail())
        {        cerr << "Error opening input file." << endl;
                 exit(1);       }

        for (int k=0; k<17; k++)         //Read and input data from file.
        {        fin >> angle[k] >> clift[k];

                 cout << "Please enter the flight-path angle in degrees: ";
                 cin >> fl_ang;

        if (fl_ang <= 21 && fl_ang >= -4)
        {        if (fl_ang == angle[1])
                {        k=2;       }
                else
                {        k=1;       }
                do
                {       result = clift[k-1] + ((fl_ang - angle[k-1])/(angle[k] -
                                 angle[k-1])) * (clift[k] - clift[k-1]);
                        cout << "The coefficient of lift is " << result << "." << endl;          } 
                while (angle[k] < fl_ang);        }

        else
        {       cout << "Flight-path angle not in range." << endl;        }         }
       
        fin.close();         //Close file stream.

        return 0;       }

You need more parentheses in your equation.
is (b-a/a-c) supposed to be
1) (b-(a/a)-c)
2) ((b-a)/(a-c))
3) ((b-(a/a))-c)
etc.

What you have is #1...

Oh! I didn't even notice that, thank you!
However, when I enter the value of 0 degrees, the program returns a 0 when it's supposed to be 0.097. Also, when I do execute the program, it continually outputs the right value without stopping. I would need to use the Control+C command to stop the program. Any reasons why those would happen?

Where do you change angle[k] during your loop?

Oh, I see what you mean, and that's the reason why it continually returns the same thing because my loop hasn't stopped. I'm actually kind of stuck on that one, I tried adding in a "break;" after the while statement, and the loop only stops if I enter in a 0 (which still returns the wrong value) but doesn't stop the loop when I enter in other values.

if k is controlling the index of the array you are using (lines 20 and 21) then why do you keep setting the value of k to either 1 or 2 in lines 28 and 30?

So I tweaked my program so that I can use a separate function to do the linear interpolation. I passed down two arrays from the data file into the function, which were "angle" and "clift" as x[] and y[], along with the user input angle, ang, as b. If the user inputs an angle that was between -4 and 21, then there would be two cases: if the user had input an angle that was already on the list, or if the user inputs an angle in between two values on the list and therefore needing to the linear interpolation. Otherwise, the program would output that the angle entered was not within the range. The problem I'm having now is that, if I entered a value that was already on the list, it doesn't output the correct value, none of which are even on the list, and if I entered a, angle in between two values, the program doesn't perform the interpolation correctly. Any reasons for this?

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
using namespace std;

const int N = 17;
double finterp(const double x[], const double y[], double b);

int main()
{
        //Declare and initialize objects.
        double angle[N], clift[N];
        double n, ang, result;
        ifstream fin("tunnel.dat");

        if (fin.fail())
        {       cerr << "Error opening input file." << endl;
                exit(1);        }

        //Read and input data from file.
        for (int k=0; k<17; k++)
        {       fin >> angle[k] >> clift[k];

                cout << "Please enter the flight-path angle in degrees: ";
                cin >> ang;

        if (ang <= 21 && ang >= -4)
        {       result = finterp(angle, clift, ang);
                cout << "The coefficient of lift is " << result << "." << endl;        }
        else
        {       cout << "Flight-path angle not in range." << endl;        }        }

        //Close file stream.
        fin.close();
        return 0;
}

//Function.
double finterp(const double x[], const double y[], double b)
{        //Declare local objects
        double f_b, f_a, f_c;
        double a, c;

        for (int k=0; k<N; k++)
        {       if (b == x[k])
                        f_b = y[k];
                else
                        f_b = y[k] + ((b-x[k])/(x[k]-x[k+1])) * (y[k] - y[k+1]);         }
        return f_b;
}

It looks like you're now inputting the flight-path angle from the user after you've read only the first pair of values from the data file. Unless that's what you intended....

Simplify your thinking: the first thing you need to is read all of the data into the arrays. So do that first:

...
ifstream fin("tunnel.dat");
if (fin.fail())
{
    cerr << "Error opening input file." << endl;
    exit(1);
}
 
//Read and input data from file.
for (int k=0; k<17; k++)
{
    fin >> angle[k] >> clift[k];
}
 
//Close file stream.
fin.close();

Once you have that done, then move on to the next step, perhaps asking the user what flight-angle he/she wants to test.

Also, your finterp() code keeps re-calculating f_b, for all N intervals, regardless of whether you matched a given known data-point, returning only the (incorrect) value for the last element. If you can assume that your x[k] are in increasing order, then you need to first determine which interval is the correct one, and then perform the linear interpolation on that interval. Also, since you have N data points, you have only N-1 intervals over which to search, so adjust your for-loop accordingly. Finally, getting back to your initial "inf" problem, what happens if your x-values aren't strictly increasing? If x[k] == x[k+1] for some value of k, you'll have a divide-by-zero error, which will likely cause a result of "inf". Or it may simply have been a weird error resulting from your looping or computations.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.