Prev: importing geometry to PDEtool
Next: help with the code
From: Alex Pereira on 9 Jul 2010 21:08 I've fit some data to a spline function. Now, I need to find all maximum and minimum points of this function. Does any one knows how to do it? Here follows a simple test code that I'm using. Thank you very much. Alex a=[0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 ]; b=[1:size(a)]; b = b(:); a = a(:); fo_ = fitoptions('method','SmoothingSpline','SmoothingParam',0.10000000000000000555); ft_ = fittype('smoothingspline'); cf_ = fit(b,a,ft_,fo_); h_ = plot(cf_,'fit',0.95); %% this line only plots the result
From: John D'Errico on 9 Jul 2010 23:38 "Alex Pereira" <alexlopespereira(a)gmail.com> wrote in message <i18h5j$smb$1(a)fred.mathworks.com>... > I've fit some data to a spline function. Now, I need to find all maximum and minimum points of this function. Does any one knows how to do it? > Here follows a simple test code that I'm using. > Thank you very much. > Alex > > a=[0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 ]; > b=[1:size(a)]; > b = b(:); > a = a(:); > fo_ = fitoptions('method','SmoothingSpline','SmoothingParam',0.10000000000000000555); > ft_ = fittype('smoothingspline'); > cf_ = fit(b,a,ft_,fo_); > h_ = plot(cf_,'fit',0.95); %% this line only plots the result Shiver. This is the reason why splines are not a good choice for such a fit. Ringing. Ding, dong. ;-) Pchip is a better choice to fit this type of data. But that is probably why you were given this problem. Given a spline model, the extrema must occur at zeros of the second derivative, or at the end points of the spline. So differentiate the curve twice, and look for zero crossings of that piecewise linear function. Then check each end of the spline. John
From: Alex Pereira on 10 Jul 2010 03:36 "John D'Errico" <woodchips(a)rochester.rr.com> wrote in message <i18pur$5nj$1(a)fred.mathworks.com>... > "Alex Pereira" <alexlopespereira(a)gmail.com> wrote in message <i18h5j$smb$1(a)fred.mathworks.com>... > > I've fit some data to a spline function. Now, I need to find all maximum and minimum points of this function. Does any one knows how to do it? > > Here follows a simple test code that I'm using. > > Thank you very much. > > Alex > > > > a=[0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 ]; > > b=[1:size(a)]; > > b = b(:); > > a = a(:); > > fo_ = fitoptions('method','SmoothingSpline','SmoothingParam',0.10000000000000000555); > > ft_ = fittype('smoothingspline'); > > cf_ = fit(b,a,ft_,fo_); > > h_ = plot(cf_,'fit',0.95); %% this line only plots the result > > Shiver. This is the reason why splines are not a good > choice for such a fit. Ringing. Ding, dong. ;-) > > Pchip is a better choice to fit this type of data. But that > is probably why you were given this problem. > > Given a spline model, the extrema must occur at zeros > of the second derivative, or at the end points of the spline. > So differentiate the curve twice, and look for zero crossings > of that piecewise linear function. Then check each end of > the spline. > > John Dear John, Thank you for your relply. I understood the math theory about the second derivative. But I don't know how to differentiate this spline. Do you know it ? Thanks again, Alex
From: John D'Errico on 10 Jul 2010 06:37 "Alex Pereira" <alexlopespereira(a)gmail.com> wrote in message <i197t5$jki$1(a)fred.mathworks.com>... > "John D'Errico" <woodchips(a)rochester.rr.com> wrote in message <i18pur$5nj$1(a)fred.mathworks.com>... > > "Alex Pereira" <alexlopespereira(a)gmail.com> wrote in message <i18h5j$smb$1(a)fred.mathworks.com>... > > > I've fit some data to a spline function. Now, I need to find all maximum and minimum points of this function. Does any one knows how to do it? > > > Here follows a simple test code that I'm using. > > > Thank you very much. > > > Alex > > > > > > a=[0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 ]; > > > b=[1:size(a)]; > > > b = b(:); > > > a = a(:); > > > fo_ = fitoptions('method','SmoothingSpline','SmoothingParam',0.10000000000000000555); > > > ft_ = fittype('smoothingspline'); > > > cf_ = fit(b,a,ft_,fo_); > > > h_ = plot(cf_,'fit',0.95); %% this line only plots the result > > > > Shiver. This is the reason why splines are not a good > > choice for such a fit. Ringing. Ding, dong. ;-) > > > > Pchip is a better choice to fit this type of data. But that > > is probably why you were given this problem. > > > > Given a spline model, the extrema must occur at zeros > > of the second derivative, or at the end points of the spline. > > So differentiate the curve twice, and look for zero crossings > > of that piecewise linear function. Then check each end of > > the spline. > > > > John > Dear John, > Thank you for your relply. I understood the math theory about the second derivative. But I don't know how to differentiate this spline. Do you know it ? > I don't have the curvefitting toolbox, so I don't know what form the spline is returned in. Is it a pp form? A b-spline? Hermite form (unlikely)? If you can extract the spline as a pp form, the differentiation is simple. John
From: Matt J on 10 Jul 2010 13:13
"Alex Pereira" <alexlopespereira(a)gmail.com> wrote in message <i197t5$jki$1(a)fred.mathworks.com>... > > Given a spline model, the extrema must occur at zeros > > of the second derivative, or at the end points of the spline. > > So differentiate the curve twice, and look for zero crossings > > of that piecewise linear function. Then check each end of > > the spline. > > > > John > Dear John, > Thank you for your relply. I understood the math theory about the second derivative. But I don't know how to differentiate this spline. Do you know it ? ============ I guess if more than 1 person has heard of this 2nd derivative result, it must be true in some sense, but I'm not finding it to be true in the obvious sense for cubic B-splines. A cubic B-spline basis function on the interval [-1 1] is f(x) = 2/3 - x.^2 +abs(x).^3/2 which has an extrema (maximum) at x=0, but the 2nd derivative there is -2. |