Strange result of spap2










0















I encounter strange results from spap2 on some data:



enter image description here



The actual data is the blue curve, red circles are the knots I am using and yellow curve is the display of the cubic spline curve.



The code is quite simple, I cannot figure out what is the problem:



spgood = spap2(knots_zY, 4, ec, Y);
plot(ec, Y);
hold on;
scatter(knots_zY, Y(ec==knots_zY));
fnplt(spgood)


ec is the vector -4.12:0.02:-0.54.
Y is the following vector:



4.1291 4.0732 4.0173 4.2624 4.3826 4.3267 4.2708 4.4367 4.3808 4.1031 4.1721 3.8152 4.1572
4.1013 4.0454 3.5916 3.8367 3.7808 3.8218 3.6690 3.9141 3.7333 3.8023 3.3204 3.5656 3.4305
3.5787 3.3978 3.3419 3.2860 3.4062 3.4753 3.5706 3.2385 3.1826 3.4947 3.5315 3.1746 3.2089
3.2276 3.1940 2.9162 3.0364 3.0263 2.8155 2.7596 2.9555 2.8996 2.9081 2.7322 2.8524 2.6397
2.7662 2.5279 2.5417 2.2005 2.3409 2.5108 2.5202 2.3359 2.3660 2.3100 2.1682 2.1123 2.2140
2.1288 2.1116 1.9856 2.0089 1.8845 1.9148 1.9308 1.7273 1.7642 1.7326 1.6606 1.7378 1.6570
1.5815 1.5701 1.4630 1.5503 1.5181 1.4385 1.3083 1.3168 1.2991 1.2523 1.1390 0.9988 1.0373
0.9913 1.0113 0.9754 0.8912 0.8790 0.7491 0.7557 0.7544 0.7119 0.7031 0.6843 0.6418 0.5938
0.5193 0.5334 0.4312 0.4839 0.4437 0.3992 0.3689 0.3287 0.3348 0.3076 0.2274 0.2174 0.1970
0.2188 0.1760 0.1384 0.1773 0.1342 0.1388 0.1097 0.0830 0.0782 0.0725 0.0863 0.0581 0.0466
0.0398 0.0431 0.0187 0.0187 0.0176 0.0167 0.0231 0.0033 -0.0117 -0.0016 0.0084 -0.0055 -0.0120
-0.0080 -0.0064 -0.0075 -0.0134 -0.0075 0.0012 -0.0077 -0.0024 0.0006 0.0010 0.0043 0.0016 0.0018
0.0042 0.0030 0.0029 0.0029 0.0021 0.0013 -0.0002 -0.0020 -0.0030 -0.0032 -0.0002 -0.0013 0.0035
0.0028 -0.0000 -0.0057 -0.0032 0.0020 0.0597 0.1835 0.5083 1.0275 1.6448 3.0549


The knots are defined with the following 12 values:



-4.1200 -3.9400 -3.5400 -3.3000 -3.1400 -2.6800 -2.3600 -2.0600 -1.5000 -1.1600 -0.7000 -0.5400


I don't expect a nice fit, but at least the spline fit sticks with the knots ... but here the result is completely erroneous. I am stuck with this, unable to see where is the problem with this data sample.



Note: the knots are computed in a separate algorithm and should be used for the interpolator, getting a good fit is not the question here. The question is why the spline fit does not pass through the knots.










share|improve this question



















  • 2





    Please post a Minimal, Complete, and Verifiable example

    – Wolfie
    Nov 13 '18 at 17:05











  • uk.mathworks.com/help/curvefit/examples/… may help

    – nkjt
    Nov 13 '18 at 20:02











  • @nkjt The knots are already chosen, your link does not help. My problem is that the spline must pass through the knots, and here it does not. On your link, even if the fit is bad in the first example, the curve passes through the knots.

    – Bentoy13
    Nov 14 '18 at 8:16











  • @Wolfie Indeed. I have added all data values (not so long but still), I don't see how to post a MCVE here with less info.

    – Bentoy13
    Nov 14 '18 at 12:16






  • 1





    The emphasis on MCVE isn't minimal (the edit you've provided is good), the emphasis is complete, i.e. previously we couldn't run your code and reproduce the results, now we can :)

    – Wolfie
    Nov 14 '18 at 12:18















0















I encounter strange results from spap2 on some data:



enter image description here



The actual data is the blue curve, red circles are the knots I am using and yellow curve is the display of the cubic spline curve.



The code is quite simple, I cannot figure out what is the problem:



spgood = spap2(knots_zY, 4, ec, Y);
plot(ec, Y);
hold on;
scatter(knots_zY, Y(ec==knots_zY));
fnplt(spgood)


ec is the vector -4.12:0.02:-0.54.
Y is the following vector:



4.1291 4.0732 4.0173 4.2624 4.3826 4.3267 4.2708 4.4367 4.3808 4.1031 4.1721 3.8152 4.1572
4.1013 4.0454 3.5916 3.8367 3.7808 3.8218 3.6690 3.9141 3.7333 3.8023 3.3204 3.5656 3.4305
3.5787 3.3978 3.3419 3.2860 3.4062 3.4753 3.5706 3.2385 3.1826 3.4947 3.5315 3.1746 3.2089
3.2276 3.1940 2.9162 3.0364 3.0263 2.8155 2.7596 2.9555 2.8996 2.9081 2.7322 2.8524 2.6397
2.7662 2.5279 2.5417 2.2005 2.3409 2.5108 2.5202 2.3359 2.3660 2.3100 2.1682 2.1123 2.2140
2.1288 2.1116 1.9856 2.0089 1.8845 1.9148 1.9308 1.7273 1.7642 1.7326 1.6606 1.7378 1.6570
1.5815 1.5701 1.4630 1.5503 1.5181 1.4385 1.3083 1.3168 1.2991 1.2523 1.1390 0.9988 1.0373
0.9913 1.0113 0.9754 0.8912 0.8790 0.7491 0.7557 0.7544 0.7119 0.7031 0.6843 0.6418 0.5938
0.5193 0.5334 0.4312 0.4839 0.4437 0.3992 0.3689 0.3287 0.3348 0.3076 0.2274 0.2174 0.1970
0.2188 0.1760 0.1384 0.1773 0.1342 0.1388 0.1097 0.0830 0.0782 0.0725 0.0863 0.0581 0.0466
0.0398 0.0431 0.0187 0.0187 0.0176 0.0167 0.0231 0.0033 -0.0117 -0.0016 0.0084 -0.0055 -0.0120
-0.0080 -0.0064 -0.0075 -0.0134 -0.0075 0.0012 -0.0077 -0.0024 0.0006 0.0010 0.0043 0.0016 0.0018
0.0042 0.0030 0.0029 0.0029 0.0021 0.0013 -0.0002 -0.0020 -0.0030 -0.0032 -0.0002 -0.0013 0.0035
0.0028 -0.0000 -0.0057 -0.0032 0.0020 0.0597 0.1835 0.5083 1.0275 1.6448 3.0549


The knots are defined with the following 12 values:



-4.1200 -3.9400 -3.5400 -3.3000 -3.1400 -2.6800 -2.3600 -2.0600 -1.5000 -1.1600 -0.7000 -0.5400


I don't expect a nice fit, but at least the spline fit sticks with the knots ... but here the result is completely erroneous. I am stuck with this, unable to see where is the problem with this data sample.



Note: the knots are computed in a separate algorithm and should be used for the interpolator, getting a good fit is not the question here. The question is why the spline fit does not pass through the knots.










share|improve this question



















  • 2





    Please post a Minimal, Complete, and Verifiable example

    – Wolfie
    Nov 13 '18 at 17:05











  • uk.mathworks.com/help/curvefit/examples/… may help

    – nkjt
    Nov 13 '18 at 20:02











  • @nkjt The knots are already chosen, your link does not help. My problem is that the spline must pass through the knots, and here it does not. On your link, even if the fit is bad in the first example, the curve passes through the knots.

    – Bentoy13
    Nov 14 '18 at 8:16











  • @Wolfie Indeed. I have added all data values (not so long but still), I don't see how to post a MCVE here with less info.

    – Bentoy13
    Nov 14 '18 at 12:16






  • 1





    The emphasis on MCVE isn't minimal (the edit you've provided is good), the emphasis is complete, i.e. previously we couldn't run your code and reproduce the results, now we can :)

    – Wolfie
    Nov 14 '18 at 12:18













0












0








0








I encounter strange results from spap2 on some data:



enter image description here



The actual data is the blue curve, red circles are the knots I am using and yellow curve is the display of the cubic spline curve.



The code is quite simple, I cannot figure out what is the problem:



spgood = spap2(knots_zY, 4, ec, Y);
plot(ec, Y);
hold on;
scatter(knots_zY, Y(ec==knots_zY));
fnplt(spgood)


ec is the vector -4.12:0.02:-0.54.
Y is the following vector:



4.1291 4.0732 4.0173 4.2624 4.3826 4.3267 4.2708 4.4367 4.3808 4.1031 4.1721 3.8152 4.1572
4.1013 4.0454 3.5916 3.8367 3.7808 3.8218 3.6690 3.9141 3.7333 3.8023 3.3204 3.5656 3.4305
3.5787 3.3978 3.3419 3.2860 3.4062 3.4753 3.5706 3.2385 3.1826 3.4947 3.5315 3.1746 3.2089
3.2276 3.1940 2.9162 3.0364 3.0263 2.8155 2.7596 2.9555 2.8996 2.9081 2.7322 2.8524 2.6397
2.7662 2.5279 2.5417 2.2005 2.3409 2.5108 2.5202 2.3359 2.3660 2.3100 2.1682 2.1123 2.2140
2.1288 2.1116 1.9856 2.0089 1.8845 1.9148 1.9308 1.7273 1.7642 1.7326 1.6606 1.7378 1.6570
1.5815 1.5701 1.4630 1.5503 1.5181 1.4385 1.3083 1.3168 1.2991 1.2523 1.1390 0.9988 1.0373
0.9913 1.0113 0.9754 0.8912 0.8790 0.7491 0.7557 0.7544 0.7119 0.7031 0.6843 0.6418 0.5938
0.5193 0.5334 0.4312 0.4839 0.4437 0.3992 0.3689 0.3287 0.3348 0.3076 0.2274 0.2174 0.1970
0.2188 0.1760 0.1384 0.1773 0.1342 0.1388 0.1097 0.0830 0.0782 0.0725 0.0863 0.0581 0.0466
0.0398 0.0431 0.0187 0.0187 0.0176 0.0167 0.0231 0.0033 -0.0117 -0.0016 0.0084 -0.0055 -0.0120
-0.0080 -0.0064 -0.0075 -0.0134 -0.0075 0.0012 -0.0077 -0.0024 0.0006 0.0010 0.0043 0.0016 0.0018
0.0042 0.0030 0.0029 0.0029 0.0021 0.0013 -0.0002 -0.0020 -0.0030 -0.0032 -0.0002 -0.0013 0.0035
0.0028 -0.0000 -0.0057 -0.0032 0.0020 0.0597 0.1835 0.5083 1.0275 1.6448 3.0549


The knots are defined with the following 12 values:



-4.1200 -3.9400 -3.5400 -3.3000 -3.1400 -2.6800 -2.3600 -2.0600 -1.5000 -1.1600 -0.7000 -0.5400


I don't expect a nice fit, but at least the spline fit sticks with the knots ... but here the result is completely erroneous. I am stuck with this, unable to see where is the problem with this data sample.



Note: the knots are computed in a separate algorithm and should be used for the interpolator, getting a good fit is not the question here. The question is why the spline fit does not pass through the knots.










share|improve this question
















I encounter strange results from spap2 on some data:



enter image description here



The actual data is the blue curve, red circles are the knots I am using and yellow curve is the display of the cubic spline curve.



The code is quite simple, I cannot figure out what is the problem:



spgood = spap2(knots_zY, 4, ec, Y);
plot(ec, Y);
hold on;
scatter(knots_zY, Y(ec==knots_zY));
fnplt(spgood)


ec is the vector -4.12:0.02:-0.54.
Y is the following vector:



4.1291 4.0732 4.0173 4.2624 4.3826 4.3267 4.2708 4.4367 4.3808 4.1031 4.1721 3.8152 4.1572
4.1013 4.0454 3.5916 3.8367 3.7808 3.8218 3.6690 3.9141 3.7333 3.8023 3.3204 3.5656 3.4305
3.5787 3.3978 3.3419 3.2860 3.4062 3.4753 3.5706 3.2385 3.1826 3.4947 3.5315 3.1746 3.2089
3.2276 3.1940 2.9162 3.0364 3.0263 2.8155 2.7596 2.9555 2.8996 2.9081 2.7322 2.8524 2.6397
2.7662 2.5279 2.5417 2.2005 2.3409 2.5108 2.5202 2.3359 2.3660 2.3100 2.1682 2.1123 2.2140
2.1288 2.1116 1.9856 2.0089 1.8845 1.9148 1.9308 1.7273 1.7642 1.7326 1.6606 1.7378 1.6570
1.5815 1.5701 1.4630 1.5503 1.5181 1.4385 1.3083 1.3168 1.2991 1.2523 1.1390 0.9988 1.0373
0.9913 1.0113 0.9754 0.8912 0.8790 0.7491 0.7557 0.7544 0.7119 0.7031 0.6843 0.6418 0.5938
0.5193 0.5334 0.4312 0.4839 0.4437 0.3992 0.3689 0.3287 0.3348 0.3076 0.2274 0.2174 0.1970
0.2188 0.1760 0.1384 0.1773 0.1342 0.1388 0.1097 0.0830 0.0782 0.0725 0.0863 0.0581 0.0466
0.0398 0.0431 0.0187 0.0187 0.0176 0.0167 0.0231 0.0033 -0.0117 -0.0016 0.0084 -0.0055 -0.0120
-0.0080 -0.0064 -0.0075 -0.0134 -0.0075 0.0012 -0.0077 -0.0024 0.0006 0.0010 0.0043 0.0016 0.0018
0.0042 0.0030 0.0029 0.0029 0.0021 0.0013 -0.0002 -0.0020 -0.0030 -0.0032 -0.0002 -0.0013 0.0035
0.0028 -0.0000 -0.0057 -0.0032 0.0020 0.0597 0.1835 0.5083 1.0275 1.6448 3.0549


The knots are defined with the following 12 values:



-4.1200 -3.9400 -3.5400 -3.3000 -3.1400 -2.6800 -2.3600 -2.0600 -1.5000 -1.1600 -0.7000 -0.5400


I don't expect a nice fit, but at least the spline fit sticks with the knots ... but here the result is completely erroneous. I am stuck with this, unable to see where is the problem with this data sample.



Note: the knots are computed in a separate algorithm and should be used for the interpolator, getting a good fit is not the question here. The question is why the spline fit does not pass through the knots.







matlab cubic-spline






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 12:13







Bentoy13

















asked Nov 13 '18 at 17:04









Bentoy13Bentoy13

3,79611130




3,79611130







  • 2





    Please post a Minimal, Complete, and Verifiable example

    – Wolfie
    Nov 13 '18 at 17:05











  • uk.mathworks.com/help/curvefit/examples/… may help

    – nkjt
    Nov 13 '18 at 20:02











  • @nkjt The knots are already chosen, your link does not help. My problem is that the spline must pass through the knots, and here it does not. On your link, even if the fit is bad in the first example, the curve passes through the knots.

    – Bentoy13
    Nov 14 '18 at 8:16











  • @Wolfie Indeed. I have added all data values (not so long but still), I don't see how to post a MCVE here with less info.

    – Bentoy13
    Nov 14 '18 at 12:16






  • 1





    The emphasis on MCVE isn't minimal (the edit you've provided is good), the emphasis is complete, i.e. previously we couldn't run your code and reproduce the results, now we can :)

    – Wolfie
    Nov 14 '18 at 12:18












  • 2





    Please post a Minimal, Complete, and Verifiable example

    – Wolfie
    Nov 13 '18 at 17:05











  • uk.mathworks.com/help/curvefit/examples/… may help

    – nkjt
    Nov 13 '18 at 20:02











  • @nkjt The knots are already chosen, your link does not help. My problem is that the spline must pass through the knots, and here it does not. On your link, even if the fit is bad in the first example, the curve passes through the knots.

    – Bentoy13
    Nov 14 '18 at 8:16











  • @Wolfie Indeed. I have added all data values (not so long but still), I don't see how to post a MCVE here with less info.

    – Bentoy13
    Nov 14 '18 at 12:16






  • 1





    The emphasis on MCVE isn't minimal (the edit you've provided is good), the emphasis is complete, i.e. previously we couldn't run your code and reproduce the results, now we can :)

    – Wolfie
    Nov 14 '18 at 12:18







2




2





Please post a Minimal, Complete, and Verifiable example

– Wolfie
Nov 13 '18 at 17:05





Please post a Minimal, Complete, and Verifiable example

– Wolfie
Nov 13 '18 at 17:05













uk.mathworks.com/help/curvefit/examples/… may help

– nkjt
Nov 13 '18 at 20:02





uk.mathworks.com/help/curvefit/examples/… may help

– nkjt
Nov 13 '18 at 20:02













@nkjt The knots are already chosen, your link does not help. My problem is that the spline must pass through the knots, and here it does not. On your link, even if the fit is bad in the first example, the curve passes through the knots.

– Bentoy13
Nov 14 '18 at 8:16





@nkjt The knots are already chosen, your link does not help. My problem is that the spline must pass through the knots, and here it does not. On your link, even if the fit is bad in the first example, the curve passes through the knots.

– Bentoy13
Nov 14 '18 at 8:16













@Wolfie Indeed. I have added all data values (not so long but still), I don't see how to post a MCVE here with less info.

– Bentoy13
Nov 14 '18 at 12:16





@Wolfie Indeed. I have added all data values (not so long but still), I don't see how to post a MCVE here with less info.

– Bentoy13
Nov 14 '18 at 12:16




1




1





The emphasis on MCVE isn't minimal (the edit you've provided is good), the emphasis is complete, i.e. previously we couldn't run your code and reproduce the results, now we can :)

– Wolfie
Nov 14 '18 at 12:18





The emphasis on MCVE isn't minimal (the edit you've provided is good), the emphasis is complete, i.e. previously we couldn't run your code and reproduce the results, now we can :)

– Wolfie
Nov 14 '18 at 12:18












1 Answer
1






active

oldest

votes


















0














I have made several errors.



First, it's a mistake to assume that the result spline will pass through the knots, as it is an approximation (see this answer). The approximation smoothes the whole original data so there is no way to stick on knots.



Second, I have forgot to extend the end knots to impose boundary conditions. The default boundary condition is to have all derivatives (including the 0th-order) to be zero, resulting in this shape. The solution is then to use augknt to get an actual cubic spline with two continuous derivatives:



spgood = spap2(augknt(knots_zY,4), 4, ec, Y);


The resulting fit is:



better fit



which is way better, given the choice of the knot sequence.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286137%2fstrange-result-of-spap2%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I have made several errors.



    First, it's a mistake to assume that the result spline will pass through the knots, as it is an approximation (see this answer). The approximation smoothes the whole original data so there is no way to stick on knots.



    Second, I have forgot to extend the end knots to impose boundary conditions. The default boundary condition is to have all derivatives (including the 0th-order) to be zero, resulting in this shape. The solution is then to use augknt to get an actual cubic spline with two continuous derivatives:



    spgood = spap2(augknt(knots_zY,4), 4, ec, Y);


    The resulting fit is:



    better fit



    which is way better, given the choice of the knot sequence.






    share|improve this answer



























      0














      I have made several errors.



      First, it's a mistake to assume that the result spline will pass through the knots, as it is an approximation (see this answer). The approximation smoothes the whole original data so there is no way to stick on knots.



      Second, I have forgot to extend the end knots to impose boundary conditions. The default boundary condition is to have all derivatives (including the 0th-order) to be zero, resulting in this shape. The solution is then to use augknt to get an actual cubic spline with two continuous derivatives:



      spgood = spap2(augknt(knots_zY,4), 4, ec, Y);


      The resulting fit is:



      better fit



      which is way better, given the choice of the knot sequence.






      share|improve this answer

























        0












        0








        0







        I have made several errors.



        First, it's a mistake to assume that the result spline will pass through the knots, as it is an approximation (see this answer). The approximation smoothes the whole original data so there is no way to stick on knots.



        Second, I have forgot to extend the end knots to impose boundary conditions. The default boundary condition is to have all derivatives (including the 0th-order) to be zero, resulting in this shape. The solution is then to use augknt to get an actual cubic spline with two continuous derivatives:



        spgood = spap2(augknt(knots_zY,4), 4, ec, Y);


        The resulting fit is:



        better fit



        which is way better, given the choice of the knot sequence.






        share|improve this answer













        I have made several errors.



        First, it's a mistake to assume that the result spline will pass through the knots, as it is an approximation (see this answer). The approximation smoothes the whole original data so there is no way to stick on knots.



        Second, I have forgot to extend the end knots to impose boundary conditions. The default boundary condition is to have all derivatives (including the 0th-order) to be zero, resulting in this shape. The solution is then to use augknt to get an actual cubic spline with two continuous derivatives:



        spgood = spap2(augknt(knots_zY,4), 4, ec, Y);


        The resulting fit is:



        better fit



        which is way better, given the choice of the knot sequence.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 15:50









        Bentoy13Bentoy13

        3,79611130




        3,79611130



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286137%2fstrange-result-of-spap2%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Museum of Modern and Contemporary Art of Trento and Rovereto

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully