swharden / SWHarden.com

The personal website of Scott W Harden
https://swharden.com
MIT License
4 stars 0 forks source link

Feedback on `Spline Interpolation with C#` #27

Open Marco-Sanguinetti opened 2 months ago

Marco-Sanguinetti commented 2 months ago

Regarding https://swharden.com/blog/2022-01-22-spline-interpolation/

In function FitMatrix() you see the following lines of code:

    A[n - 1] = 1.0f / dx1;
    B[n - 1] = 2.0f * A[n - 1];
    r[n - 1] = 3 * (dy1 / (dx1 * dx1));

    double[] cPrime = new double[n];
    cPrime[0] = C[0] / B[0];
    for (int i = 1; i < n; i++)
        cPrime[i] = C[i] / (B[i] - cPrime[i - 1] * A[i]);

Now arrays A, B, and r are defined for index "n-1" but array C is not. However, C[n-1] is required in the indicated for loop.

swharden commented 2 months ago

Hi @Marco-Sanguinetti, thanks for the feedback! Since this issue involves code included with ScottPlot, I created an issue over there to track arriving at a solution https://github.com/ScottPlot/ScottPlot/issues/3790

Marco-Sanguinetti commented 2 months ago

Hi @swharden . Thanks for following up. I caught it when porting the cubic spline interpolator to using List arrays (instead of plain indexed arrays). At runtime the reference to C[n-1] caused a fatal index out of range error. Your suggested fix seems reasonable to me, although in truth x[n] may be out of range so I used "(1.0f / dx1)" instead.