Category Archives: Numerical Analysis

Calculating Polynomial Interpolation Coefficients

The interpolation of a dataset can be examined by polynomials, which is nice because polynomials are useful for complex curves approximations. In order to get the coefficients of the polynomial, since the dataset points are known, the below InterpolatingPolynomialCoefficients method can be used. This takes in a double array of tabulation points and polynomial points.

[cpp]
public: virtual Double __gc* InterpolatingPolynomialCoefficients(Double __gc* tabPoints __gc [], Double __gc* polyPoints __gc []) __gc []
{
Int32 __gc* length = Math::Min(tabPoints->Length, polyPoints->Length);
Double __gc* tempArray __gc [] = __gc new Double __gc*[length];
Double __gc* returnArray __gc [] = __gc new Double __gc*[length];
try
{
for (Int32 __gc* i = 0; (i < length); i++)
{
tempArray[i] = returnArray[i] = 0;
}
tempArray[(length – 1)] = -tabPoints[0];
PushToTempArray(tabPoints, length, tempArray);
PushToFinalArray(tabPoints, polyPoints, length, tempArray, returnArray);
}
catch (ArithmeticException __gc* exception)
{
}
return returnArray;
}

private: static void __gc* PushToFinalArray(Double __gc* tabPoints __gc [], Double __gc* polyPoints __gc [], Int32 __gc* length, Double __gc* tempArray __gc [], Double __gc* returnArray __gc [])
{
for (Int32 __gc* calcHold = 0; (calcHold < length); calcHold++)
{
Double __gc* lengthHold = length;
Int32 __gc* index = (length – 1);
while ((index >= 1))
{
lengthHold = ((index * tempArray[index]) + (tabPoints[calcHold] * lengthHold));
index–;
}
Double __gc* poolArrayAndHold = (polyPoints[calcHold] / lengthHold);
Double __gc* iArray = 1;
for (index = (length – 1); (index >= 0); index–)
{
returnArray[index] += (iArray * poolArrayAndHold);
iArray = (tempArray[index] + (tabPoints[calcHold] * iArray));
}
}
}
private: static void __gc* PushToTempArray(Double __gc* tabPoints __gc [], Int32 __gc* length, Double __gc* tempArray __gc [])
{
for (Int32 __gc* i = 1; (i < length); i++)
{
IntPtr __gc* x;
for (Int32 __gc* calcHold = ((length – 1) – i); (calcHold < (length – 1)); calcHold++)
{
tempArray[calcHold] -= (tabPoints[i] * tempArray[(calcHold + 1)]);
}
tempArray[*static_cast<__box Int32*>(x = *static_cast<__box IntPtr*>((length – 1)))] = (tempArray[*static_cast<__box Int32*>(x)] – tabPoints[i]);
}
}

[/cpp]

~~ These are the notes from my N.A. class @ UoM ~~

Share

Interpolating Data Points On A Two Dimensional Regular Grid

This was a pain in the ass to figure out. So this method is for basic Bicubic interpolation, a principal based on cubic interpolation, targeted to be called to work with values and derivatives on that plain at any given point. The title of the post kind of says it all.

[cpp]

public: virtual Double __gc* CalcBiCubicInterpolation(Double __gc* __gc [] coeff __gc [], Double __gc* dir1Down, Double __gc* dir1Up, Double __gc* dir2Down, Double __gc* dir2Up, Double __gc* dir1, Double __gc* dir2) __gc []
{
Double __gc* numArray __gc [] = __gc new Double __gc*[0];
try
{
Double __gc* HD1 = 0;
Double __gc* HD2 = 0;
Double __gc* HD3 = 0;
Double __gc* evalDir1 = (dir1Up – dir1Down);
Double __gc* evalDir2 = (dir2Up – dir2Down);
Double __gc* finDirCalc1 = ((dir2Up – dir1Down) / evalDir1);
Double __gc* finDirCalc2 = ((dir2 – dir2Down) / evalDir2);
HD1 = Process(finDirCalc1, HD1, coeff, finDirCalc2, ref HD3, ref HD2);
HD2 /= evalDir1;
HD3 /= evalDir2;
numArray = __gc new Double __gc*[3] {
HD1, HD2, HD3};
}
catch (ArithmeticException __gc* exception)
{
}
return numArray;
}

private: static Double __gc* Process(Double __gc* finDirCalc1, Double __gc* HD1, Double __gc* __gc [] coeff __gc [], Double __gc* finDirCalc2, Double __gc*& HD3, Double __gc*& HD2)
{
for (Int32 __gc* i = 3; (i >= 0); i–)
{
HD1 = (((finDirCalc1 * HD1) + (((((coeff[i][3] * finDirCalc2) + coeff[i][2]) * finDirCalc2) + coeff[i][1]) * finDirCalc2)) + coeff[i][0]);
HD3 = (((finDirCalc1 * HD3) + ((((3 * coeff[i][3]) * finDirCalc2) + (2 * coeff[i][2])) * finDirCalc2)) + coeff[i][1]);
HD2 = (((finDirCalc2 * HD2) + ((((3 * coeff[3][i]) * finDirCalc1) + (2 * coeff[2][i])) * finDirCalc1)) + coeff[1][i]);
}
return HD1;
}

[/cpp]

~~ These are the notes from my N.A. class @ UoM ~~

Share

Calculating The Second Derivatives Of A Piecewise Cubic Polynomial

The purpose of this method is to provide the faculty to calculate the second derivatives of a piecewise cubic polynomial with continuous first and second derivatives when there are known tabulation points. For more information on Spline interpolation you can view this article here. Naturally, this returns an typed double array containing the second derivatives of the interpolation function at the tabulation points. There are going to be four parameters used, a double array specifying the tabulation points, one for values of the function evaluated at the interpolation points, and the two to handle the derivative of the interpolation function consumption. There is currently a limit in this calculation with the provided arrays, but meh.

[cpp]
public: virtual Double __gc* cubicSpline2ndDifferential(Double __gc* tabPoints __gc [], Double __gc* funcVals __gc [], Double __gc* dervAt0X, Double __gc* dervAtN) __gc []
{
Double __gc* place;
Double __gc* holder;
Int32 __gc* num = Math::Min(tabPoints->Length, funcVals->Length);
Double __gc* helpArray __gc [] = __gc new Double __gc*[num];
Double __gc* returnArray __gc [] = __gc new Double __gc*[num];
if (dervAt0X > 9.9E+29)
{
returnArray[0] = helpArray[0] = 0;
}
else
{
returnArray[0] = -0.5;
helpArray[0] = ((3 / (tabPoints[1] – tabPoints[0])) * (((funcVals[1] – funcVals[0]) / (tabPoints[1] – tabPoints[0])) – dervAt0X));
}
for (Int32 __gc* i = 1; (i < (num – 1)); i++)
{
Double __gc* calcedTab = ((tabPoints[i] – tabPoints[(i – 1)]) / (tabPoints[(i + 1)] – tabPoints[(i – 1)]));
Double __gc* calcedArray = ((calcedTab * returnArray[(i – 1)]) + 2);
returnArray[i] = ((calcedTab – 1) / calcedArray);
helpArray[i] = (((funcVals[(i + 1)] – funcVals[i]) / (tabPoints[(i + 1)] – tabPoints[i])) – ((funcVals[i] – funcVals[(i – 1)]) / (tabPoints[i] – tabPoints[(i – 1)])));
helpArray[i] = ((((6 * helpArray[i]) / (tabPoints[(i + 1)] – tabPoints[(i – 1)])) – (calcedTab * helpArray[(i – 1)])) / calcedArray);
}
if (dervAtN > 9.9E+29)
{
place = holder = 0;
}
else
{
place = 0.5;
holder = ((3 / (tabPoints[(num – 1)] – tabPoints[(num – 2)])) * (dervAtN – ((funcVals[(num – 1)] – funcVals[(num – 2)]) / (tabPoints[(num – 1)] – tabPoints[(num – 2)]))));
}
returnArray[(num – 1)] = ((holder – (place * helpArray[(num – 2)])) / ((place * returnArray[(num – 2)]) + 1));
for (Int32 __gc* j = (num – 2); (j >= 0); j–)
{
returnArray[j] = ((returnArray[j] * returnArray[(j + 1)]) + helpArray[j]);
}
return returnArray;
}
[/cpp]

~~ These are the notes from my N.A. class @ UoM ~~

Share