Linear_interp


MPD78
06-18-2009, 01:30 PM
Hello all,

I am using the Linear_interp function and following the example in chapter 3 of the book and I am having some trouble (as usual) with the passing of arguments to the fucntion.

My code is show below

#include "interp1d.h"

using namespace std;

Doub pause; /* Just a dummy variable used to keep the
output screen up*/

// made up arrays of data
Doub xx[] = {1,2,3,4,5,6,7,8,9,10}; //
Doub yy[] = {100,200,300,400,500,600,700,800,900,1000};

int main()
{
Int n=10;
VecDoub xx(n), yy(n);
Linear_interp myfunc(xx,yy);

Doub x,y;
x=1.5;

y=myfunc.interp(x);

cout << y << endl;
cin >> pause;
return 0;
}

My answer should be 150.

Any help would be appreciated as always.

Thanks
Matt D.

davekw7x
06-18-2009, 03:43 PM
Hello allHi.

I am using the Linear_interp function.


//
// davekw7x
//

#include "../code/nr3.h"
#include "../code/interp_1d.h"
#include "../code/interp_linear.h"

int main()
{
// Arrays of data points
Doub array_x[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Doub array_y[] = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };

//
// Calculate the number of elements so that it's easy
// to change the array size for additional experimentation.
//
Int n = sizeof(array_x)/sizeof(array_x[0]);

//
// Declare VecDoubs of data points and initialize them from the arrays
//
VecDoub xx(n, array_x);
VecDoub yy(n, array_y);

//
// Create the Linear_interp object
//
Linear_interp lint(xx, yy);

Doub x;
cout << "Enter a value for x: ";
while (cin >> x) {

//
// Calculate interpolated value
//
Doub y = lint.interp(x);

cout << "Using linear interpolation, the interpolated value at x = "
<< x << " is " << y << endl << endl;

cout << "Enter a value for x: ";
}

cout << "Goodbye for now." << endl;


return 0;
}



A run:

Enter a value for x: 1.5
Using linear interpolation, the interpolated value at x = 1.5 is 150

Enter a value for x: 3.14
Using linear interpolation, the interpolated value at x = 3.14 is 314

Enter a value for x: -10
Using linear interpolation, the interpolated value at x = -10 is -1000

Enter a value for x: quit
Goodbye for now.


Regards,

Dave

MPD78
06-19-2009, 08:19 AM
Dave,

Thank you very much.

Matt