Chapter 3 interp_2d.h


MPD78
11-02-2009, 08:16 AM
Hello all,

I have an application that involves interpolating in 2 dimensions on a table of steam superheat enthalpy values.

The enthalpy is interpolated with a temperature (C) and a pressure (MPa).

I followed the example of how to get the routine working but I am having trouble with arrays.

I have x1[] as the temperature (C) array.

I have x2[] as the pressure (MPa) array.

I have yy[m][n] as the enthalpy (kJ/kg) array.

Here is my main function.

#include "interp_2d.h"

int main()
{
Doub pause; // Dummy variable to keep the output window visible
Doub ans; // Interpolatd results

Doub x1[] = {50,100,150,200,250,300,400,500,600,700}; // Temperature array (C)

Doub x2[] = {.010, 0.050, 0.10}; // Pressure array (MPa)

const Int m = sizeof(x1)/sizeof(x1[0]); // Obtain the size of the array x1
const Int n = sizeof(x2)/sizeof(x2[0]); // Obtain the size of the array x2

Doub yy[m][n] = {{2592.6, 0, 0}, {2687.5, 2682.5, 2676.2}, {2783.0, 2780.1, 2776.4}, {2879.5, 2877.7, 2875.3},
{2977.3, 2976.0, 2974.3}, {3076.5, 3075.5, 3074.3}, {3279.6, 3278.9, 3278.2}, {3489.1, 3488.7, 3488.1},
{3705.4, 3705.1, 3704.7}, {3928.7, 3928.5, 3928.2}}; // Enthalpy array (kJ/kg)

Doub xx1 = 175; // Temperature to interpolate with.
Doub xx2 = 0.025; // Pressure to interpolate with.

MatDoub yy(m,n);
VecDoub x1(m);
VecDoub x2(n);

Bilin_interp myfunc(x1,x2,yy); // Instance the Bilin_interp struct

ans = myfunc.interp(xx1,xx2);

cout << "Interpolated answer is " << ans << endl;

cin >> pause; // Dummy input to keep the output window visible

return 0;
}


When this code is compiled, I recieve the following error messages.

:error C2040: 'yy' : 'MatDoub' differs in levels of indirection from 'Doub [10][3]'

:error C2040: 'x1' : 'VecDoub' differs in levels of indirection from 'Doub [10]'

:error C2040: 'x2' : 'VecDoub' differs in levels of indirection from 'Doub [3]'

I am not sure what I have done wrong. As always, any help would be greatly appreciated.

Thanks
Matt

davekw7x
11-02-2009, 09:08 AM
...
:error C2040: 'yy' : 'MatDoub' differs in levels of indirection from 'Doub [10][3]'

:error C2040: 'x1' : 'VecDoub' differs in levels of indirection from 'Doub [10]'

:error C2040: 'x2' : 'VecDoub' differs in levels of indirection from 'Doub [3]'


You want to create a VecDoub object using an array as an initializer. Now, it should be obvious that the VecDoub and the array can not have the same name, right? You have to use the name of the array in the constructor.

So it could go something like:


// The array used by the x1 constructor
Doub x1_a[] = { 50, 100, 150, 200, 250, 300, 400, 500, 600, 700 };

// The array used by the x2 constructor
Doub x2_a[] = { .010, 0.050, 0.10 };

const Int m = sizeof(x1_a) / sizeof(x1_a[0]); // 10
const Int n = sizeof(x2_a) / sizeof(x2_a[0]); // 3
.
.
.
VecDoub x1(m, x1_a);
VecDoub x2(n, x2_a);


To initialize a MatDoub from an array, here's how it goes:

Create a 1-D array that has size equal to number of rows times number of columns. The initializer for the array has matrix elements stored in row-major order. (That is, first row followed by second row, etc.)

In the following listing I arranged the elements so that I could visually double-check the matrix values, but it is a 1-D array and the initializer is simply a comma-separated list of doubles.


// A 1-D array used by the yy constructor
Doub yy_a[m*n] = {
2592.6, 0.0, 0.0, // The first row
2687.5, 2682.5, 2676.2, // The second row
2783.0, 2780.1, 2776.4, // Etc.
2879.5, 2877.7, 2875.3,
2977.3, 2976.0, 2974.3,
3076.5, 3075.5, 3074.3,
3279.6, 3278.9, 3278.2,
3489.1, 3488.7, 3488.1,
3705.4, 3705.1, 3704.7,
3928.7, 3928.5, 3928.2
};
.
.
.
MatDoub yy(m, n, yy_a);
.
.
.


.
.
.

Interpolated answer is 2830.37


Regards,

Dave

MPD78
11-02-2009, 09:30 AM
Thanks Dave,

I thought the names were off ... but I was following the example declaration on page 133. Perhaps, I was following too close.

Anyways, it works correctly now.

Thanks again.

Matt