How to open an existing file and use the data to construct a function

Mar 3, 2011 at 5:40pm
Hi,

I am trying to do the following: I have got a file with two columns, "2col.txt", and I would like to use this file to construct a function on a program: on the first column, I have got the values of the variable "x", and on the second one, the values of f(x). I would like to identify the elements of the first column of the file with "X" and the elements of the second column with Function[X], and I have written the following:

complex* Function
Function = new complex[XStepMax] // XStepMax=number of rows of the file.

How can I continue? Thank you very much!
Mar 3, 2011 at 5:58pm
What do you mean by saying 'construct a function'? You want to just store the data in a way you can easily retrieve f(x) using x or you want to create a polynomial approximation of that function?

EDIT: Also, what is the format of the file?
Last edited on Mar 3, 2011 at 6:06pm
Mar 3, 2011 at 8:20pm
Assuming that your file consists of two columns of doubles and you want to store (x,f(x)) pairs, the most straightforward way to do this is to use a map (if the values of x are equidistant, you can use a vector):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <fstream>
#include <map>
using namespace std;

int main()
{
    map<double,double> F;

    ifstream fin("2col.txt");

    double x, fx;

    while (true)
    {
        fin >> x >> fx;
        if (!fin) break;

        F[x]=fx;
    }

    cout << "f(1)=" << F[1] << endl;
    cout << "f(3)=" << F[3] << endl;
    cout << "f(8.5)=" << F[8.5] << endl;
    cout << "f(10)=" << F[10] << endl;

    cin.get();
    return 0;
}

Using this as input (2col.txt):

1 2
2 1.2
3 -3.1
5 -2
6 5.6
8.5 -1.2
9.5 2.3
10 4.4

I get this as output:

f(1)=2
f(3)=-3.1
f(8.5)=-1.2
f(10)=4.4

What troubles me is that 'complex' thing you have there. Are the values of x and f(x) supposed to be complex numbers? In that case, what does a complex number look like inside your file?
Last edited on Mar 3, 2011 at 8:22pm
Mar 4, 2011 at 9:53am
I am sorry, I did not explain my question as good as posible. The situation is the following:
I have got a pointer: X[x], were x is integer and I define X[x] as
double* X
X = new double [xmax]

Apart from this, I have got a function, Function[x], which value could be complex:
complex* Function
Function = new complex[xmax]

And I would like to do the following: take the file 2col.txt, and identify the first column with X[x], and the second one with Function[x]. Is that possible?

Thank you very much, your answers are very useful!
Mar 4, 2011 at 9:58am
(all the numbers that are on the file are reals, complex numbers do not appear, but I have to define the Function as complex, because what we get from the file is the initial function, and then I have to change the Function, and it could have complex values; that is the reason why I have to define it as "complex*")
Mar 4, 2011 at 2:00pm
Are you supposed to get xmax from the file?
If not, you can just use arrays with an arbitrarily big xmax.

1
2
3
4
const int xmax=1000;

double X[xmax];
complex<double> Function[xmax];

Then, open the file and fill these two arrays.
Using a variable to represent the current file row (0, 1, 2, ...) could be useful.

Useful links:

http://cplusplus.com/doc/tutorial/files/
http://cplusplus.com/reference/std/complex/complex/
Last edited on Mar 4, 2011 at 2:01pm
Mar 7, 2011 at 6:44pm
Ok!! Thanks very much for your help!! Finally I have obtained what I wanted! ;)
Topic archived. No new replies allowed.