You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The function should interpolate to find ZI, the values of the underlying 2-D function Z at the points XI and YI. Vectors X and Y specify the points at which the data Z is given.
Similarly to interpolate1D, this function should use linear interpolation.
Inputs: X,Y,Z,XI,YI
Outputs: ZI
Here is some rough code that should, I think, do the job. I'm not sure if the declaration, or later the calculation, of the size of the input vectors is valid.
float
interpolate2D
(constfloat X[],
constfloat Y[],
constfloat Z[],
float XI,
float YI)
{
// Check sizes of inputssize_t n = sizeof(X);
size_t m = sizeof(Y);
size_t nZ = sizeof(Z[0]);
size_tmZ = sizeof(Z);
if (n != nZ)
return0;
if (m != mZ)
return0;
if ( (n < 1) || (m < 1) )
return0;
// Find xLow and xHighfloat xMin = X[0]; float xMax = X[n-1];
int xLowIndex; int xHighIndex;
float xPercent;
if (XI > xMin && XI < xMax) // XI is within vector X
{
int j = 0;
while (XI > X[j])
{
j = j + 1;
xLowIndex = j-1;
xHighIndex = j;
}
xPercent = (XI - X[xLowIndex]) / (X[xHighIndex] - X[xLowIndex]);
}
elseif (XI <= xMin) // XI is lower than minimum of vector X, use low edge
{
xLowIndex = 0;
xHighIndex = 0;
xPercent = 1.0;
}
elseif (XI >= xMax) // XI higher than maximum of vector X, use high edge
{
xLowIndex = n-1;
xHighIndex = n-1;
xPercent = 1.0;
}
else// XI is a NaN
{
return0;
}
float yMin = Y[0]; float yMax = Y[m-1];
int yLowIndex; int yHighIndex;
float yPercent;
if (YI > yMin && YI < yMax) // YI is within vector Y
{
int j = 0;
while (YI > Y[j])
{
j = j + 1;
yLowIndex = j-1;
yHighIndex = j;
}
yPercent = (YI - Y[yLowIndex]) / (Y[yHighIndex] - Y[yLowIndex]);
}
elseif (YI <= yMin) // YI is lower than minimum of vector Y, use low edge
{
yLowIndex = 0;
yHighIndex = 0;
yPercent = 1.0;
}
elseif (YI >= yMax) // YI higher than maximum of vector Y, use high edge
{
yLowIndex = m-1;
yHighIndex = m-1;
yPercent = 1.0;
}
else// YI is a NaN
{
return0;
}
float val1 = (Z[yLowIndex][xHighIndex] - Z[yLowIndex][xLowIndex]) * xPercent + Z[yLowIndex][xLowIndex];
float val2 = (Z[yHighIndex][xHighIndex] - Z[yHighIndex][xLowIndex]) * xPercent + Z[yHighIndex][xLowIndex];
return (val2 - val1) * yPercent + val1;
}
The text was updated successfully, but these errors were encountered:
The function should interpolate to find ZI, the values of the underlying 2-D function Z at the points XI and YI. Vectors X and Y specify the points at which the data Z is given.
Similarly to interpolate1D, this function should use linear interpolation.
Inputs:
X,Y,Z,XI,YI
Outputs:
ZI
Here is some rough code that should, I think, do the job. I'm not sure if the declaration, or later the calculation, of the size of the input vectors is valid.
The text was updated successfully, but these errors were encountered: