<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Interpolating Polynomials</title>
	<link>http://blogs.mathworks.com/loren/2008/07/17/interpolating-polynomials/</link>
	<description>Loren Shure  works on design of the MATLAB language at &#60;a href="http://www.mathworks.com/"&#62;The MathWorks&#60;/a&#62;. She writes here about once a week on MATLAB programming and related topics. &#60;br&#62;&#60;br&#62;&#60;a href="/images/loren-full.jpg"&#62;&#60;img src="/images/loren.jpg"&#62;&#60;/a&#62;</description>
	<pubDate>Sun, 22 Nov 2009 23:06:50 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
		<item>
		<title>By: Zohaib</title>
		<link>http://blogs.mathworks.com/loren/2008/07/17/interpolating-polynomials/#comment-30395</link>
		<dc:creator>Zohaib</dc:creator>
		<pubDate>Wed, 17 Jun 2009 10:53:41 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/07/17/interpolating-polynomials/#comment-30395</guid>
		<description>Thanx John for such a detailed reply....I am grateful for this effort. This helped me a lot.</description>
		<content:encoded><![CDATA[<p>Thanx John for such a detailed reply&#8230;.I am grateful for this effort. This helped me a lot.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John DErrico</title>
		<link>http://blogs.mathworks.com/loren/2008/07/17/interpolating-polynomials/#comment-30385</link>
		<dc:creator>John DErrico</dc:creator>
		<pubDate>Sat, 13 Jun 2009 11:51:04 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/07/17/interpolating-polynomials/#comment-30385</guid>
		<description>Hi Zohaib,

I saw your question on the newsgroup, and decided that the best solution was something I had to finish writing before I could respond. It is now at a realistic stage to reply, and to post on the file exchange. It should appear on Monday.

I've written a set of tools called by the acronym SLM, for "Shape Language Modeling". These represent a way of doing modeling that uses a set of shape primitives, prescribed in advance by the user. The idea is that a scientist/engineer/analyst/modeler often has specific knowledge about their process. This knowledge may come from some basic physical understanding of the system they are modeling, it may come from word of mouth, it may be an observation made from past experience about similar data, or even just from looking at a plot of the data. Regardless, these pieces of information are not always trivial to implement as part of the curve fitting process. 

For example, a frequent piece of information about a curve is that it must be monotone. Perhaps your process is such that physical argument tells you it must always be a monotonic increasing function? Your data may have noise in it so that it is bumpy, but in your head you know that is MUST be monotone and increasing. Usually what happens is the person doing the curve fit will find a nonlinear regression model to fit the data that happens to be monotone and increasing. In fact, I'll argue that this is the single biggest reason why people use these nonlinear regression models. They use various sigmoid functions, erfs, etc. In fact, I list several such different increasing curve shapes in my Nonlinear Regression Shapes submission on the file exchange.

http://www.mathworks.com/matlabcentral/fileexchange/10864

My point is that people often do nonlinear regression not because they want to do so, but because they are forced to do so, because they wish to build some piece of information into their curve to be fit. While monotonicity is the most common shape primitive that people seem to want to use, there are many others. For example, suppose you wished to estimate an MTF (Modulation Transfer Function) curve from some measured data points. Much of the time it is assumed that at a frequency of 0, the modulation is at 100%. So the curve must pass through the point [0,1] in the plot. Furthermore, you might wish to assume other information about the process, perhaps that the modulation will not go above 100%, or that it will never be a negative number.

Some other common shape primitives might be that the function has everywhere a positive (or negative) second derivative, or that the function is locally constant over some internal range. (This last is the question that you have asked about.) You may also wish to employ periodicity in your model, or specify that the function has a single peak in some area, but then drop off away from that point.

Given noisy data however, the data may drive a curve fit by a simple least squares spline or polynomial to do strange things however, that contradict these pieces of intrinsic knowledge that you may have. How do you force the curve to do what you know in your mind must happen, yet still fit the data as best as possible?

The solution is to provide a curve fitting tool that allows you to build any shape primitives into the model itself, in terms of a natural language of shape primitives. I do this using splines. The tool is called slmengine, because it will be the computational engine behind the SLM tools. The slmengine tool allows commandline input to describe the shape of your model. The part of these tools that is not yet fully written is a graphical front end that will allow a direct interface to these tools. In the meantime, I'll post what I have written, as these tools are now at a fully useable and documented stage.

I'll give a few examples of how you might use these tools here. I'll create some data that has an interesting shape.

&lt;pre&gt;
x = (sort(rand(1,100)) - 0.5)*pi;
y = sin(x).^5 + randn(size(x))/25;
plot(x,y,'o')
&lt;/pre&gt;

You can see that the curve has a fair amount of noise in it. Obviously, we know the function must be monotone, given the way I've created the curve itself. Suppose we did not know that underlying model? A common solution here is to employ a nonlinear regression as I said before. A BETTER solution is to use my shape language modeling tools. The slmengine tool has a property/value pair interface. (I'm sorry that I cannot actually show you these plots from this response window, because they are instructive.)

&lt;pre&gt;
slm = slmengine(x,y,'plot','on','knots',10)
slm = 
            form: 'slm'
          degree: 3
           knots: [10x1 double]
            coef: [10x2 double]
    prescription: [1x1 struct]
               x: [100x1 double]
               y: [100x1 double]
&lt;/pre&gt;

This returns the cubic spline in a Hermite form. It has 10 knots/breaks. I could also have specified a pp form as the result.

&lt;pre&gt;
pp = slmengine(x,y,'plot','on','knots',10,'result','pp')
pp = 
            form: 'pp'
          breaks: [-1.5334 -1.1988 -0.86411 -0.52947 -0.19482 0.13983 0.47447 0.80912 1.1438 1.4784]
           coefs: [9x4 double]
          pieces: 9
           order: 4
             dim: 1
    prescription: [1x1 struct]
&lt;/pre&gt;

In either case, the result contains a "prescription" field. This contains all of the information that was indicated when we built the spline. It is useful for future reference. You can also get a hint about the possible things one can specify about the shape of your curve.

&lt;pre&gt;
slm.prescription
ans = 
                    C2: 'on'
           ConcaveDown: 'off'
             ConcaveUp: 'off'
        ConstantRegion: []
            Decreasing: 'off'
                Degree: 3
         EndConditions: 'estimate'
              Envelope: 'off'
              ErrorBar: []
            Increasing: 'off'
              Integral: []
         InteriorKnots: 'fixed'
                 Knots: 10
          LeftMaxSlope: []
          LeftMaxValue: []
          LeftMinSlope: []
          LeftMinValue: []
             LeftSlope: []
             LeftValue: []
          LinearRegion: []
              MaxSlope: []
              MaxValue: []
              MinSlope: []
              MinValue: []
    NegativeInflection: []
                 Order: []
                  Plot: 'on'
    PositiveInflection: []
        Regularization: 0.0001
                Result: 'pp'
         RightMaxSlope: []
         RightMaxValue: []
         RightMinSlope: []
         RightMinValue: []
            RightSlope: []
            RightValue: []
               Scaling: 'on'
            SimplePeak: []
          SimpleValley: []
             Verbosity: 0
               Weights: []
                    XY: []
                   XYP: []
                  XYPP: []
&lt;/pre&gt;

The slm form of our model can be used by some other tools I supply in this toolkit.

&lt;pre&gt;
plotslm(slm)
&lt;/pre&gt;

will plot the spline, giving you a nice graphical interface to look at the derivatives, as well as plot the data on top of it if desired. I also give you tools to evaluate a spline, its derivatives, integrate the spline, etc. All of this is covered in great detail in a tutorial that I've written.

Back to our original curve, the basic call we used before specified that the final result be plotted. Furthermore, the spline will have 10 knots, equally spaced over the span of the data. A better curve fit results from adding more information.

&lt;pre&gt;
slm = slmengine(x,y,'plot','on','knots',10,'increasing','on');
&lt;/pre&gt;

Amazingly, this curve is now quite smooth. Simply providing information about the process and building it into your model is quite simple, as I hope you can see here. Here I've now added two more pieces of information, i.e., that the left and right hand end point slopes are exactly zero.

&lt;pre&gt;
slm = slmengine(x,y,'plot','on','knots',10,'increasing','on','leftslope',0,'rightslope',0);
&lt;/pre&gt;

I can go on further. Suppose I decided that the curve was not quite flat enough in the central region for my purposes. For example, evaluate the second derivative at a few points near zero.

&lt;pre&gt;
slmeval([-.2 0 .2],slm,2)
ans =
    -0.029646  -1.7287e-14     0.035005
&lt;/pre&gt;

Perhaps I want the curve to be constant over that interval? This is what you have asked for, after all.

&lt;pre&gt;
slm = slmengine(x,y,'plot','on','knots',10,'increasing','on','leftslope',0,'rightslope',0,'constantregion',[-.2,.2])
&lt;/pre&gt;

See that the second derivative is indeed flat in the vicinity of x = 0.

&lt;pre&gt;
slmeval([-.2 0 .2],slm,2)
ans =
  -5.7416e-15   1.6138e-15  -1.0432e-15
&lt;/pre&gt;


Alternatively, we can look at the maximum and minimum values of the slope over that interval.

&lt;pre&gt;
slmpar(slm,'maxslope',[-.2,.2])
ans =
   3.1475e-16

slmpar(slm,'minslope',[-.2,.2])
ans =
  -4.1045e-16
&lt;/pre&gt;

I hope that this toolkit is of use to you. I will argue that it will be so for many users of curve fitting, since it allows them to build models for their systems with the intrinsic shapes they know must be present. I am sorry that it took a few days for me to get this up on the file exchange for you. I did want to finish up a few things. As I said above, the slm toolkit should appear on the File Exchange on Monday, 6/15/09. Once it is up, I will plan on making many enhancements over the coming weeks, since I expect to see very much feedback on these tools. I'll also be cleaning up any minor problem that surely will surface.

The SLM toolkit does not require the splines toolbox, although it does use the optimization toolbox because of the class of problems that it forms internally. Really, it mainly uses the function lsqlin, although some rare problems employ other optimizers from that toolbox.

John D'Errico</description>
		<content:encoded><![CDATA[<p>Hi Zohaib,</p>
<p>I saw your question on the newsgroup, and decided that the best solution was something I had to finish writing before I could respond. It is now at a realistic stage to reply, and to post on the file exchange. It should appear on Monday.</p>
<p>I&#8217;ve written a set of tools called by the acronym SLM, for &#8220;Shape Language Modeling&#8221;. These represent a way of doing modeling that uses a set of shape primitives, prescribed in advance by the user. The idea is that a scientist/engineer/analyst/modeler often has specific knowledge about their process. This knowledge may come from some basic physical understanding of the system they are modeling, it may come from word of mouth, it may be an observation made from past experience about similar data, or even just from looking at a plot of the data. Regardless, these pieces of information are not always trivial to implement as part of the curve fitting process. </p>
<p>For example, a frequent piece of information about a curve is that it must be monotone. Perhaps your process is such that physical argument tells you it must always be a monotonic increasing function? Your data may have noise in it so that it is bumpy, but in your head you know that is MUST be monotone and increasing. Usually what happens is the person doing the curve fit will find a nonlinear regression model to fit the data that happens to be monotone and increasing. In fact, I&#8217;ll argue that this is the single biggest reason why people use these nonlinear regression models. They use various sigmoid functions, erfs, etc. In fact, I list several such different increasing curve shapes in my Nonlinear Regression Shapes submission on the file exchange.</p>
<p><a href="http://www.mathworks.com/matlabcentral/fileexchange/10864" rel="nofollow">http://www.mathworks.com/matlabcentral/fileexchange/10864</a></p>
<p>My point is that people often do nonlinear regression not because they want to do so, but because they are forced to do so, because they wish to build some piece of information into their curve to be fit. While monotonicity is the most common shape primitive that people seem to want to use, there are many others. For example, suppose you wished to estimate an MTF (Modulation Transfer Function) curve from some measured data points. Much of the time it is assumed that at a frequency of 0, the modulation is at 100%. So the curve must pass through the point [0,1] in the plot. Furthermore, you might wish to assume other information about the process, perhaps that the modulation will not go above 100%, or that it will never be a negative number.</p>
<p>Some other common shape primitives might be that the function has everywhere a positive (or negative) second derivative, or that the function is locally constant over some internal range. (This last is the question that you have asked about.) You may also wish to employ periodicity in your model, or specify that the function has a single peak in some area, but then drop off away from that point.</p>
<p>Given noisy data however, the data may drive a curve fit by a simple least squares spline or polynomial to do strange things however, that contradict these pieces of intrinsic knowledge that you may have. How do you force the curve to do what you know in your mind must happen, yet still fit the data as best as possible?</p>
<p>The solution is to provide a curve fitting tool that allows you to build any shape primitives into the model itself, in terms of a natural language of shape primitives. I do this using splines. The tool is called slmengine, because it will be the computational engine behind the SLM tools. The slmengine tool allows commandline input to describe the shape of your model. The part of these tools that is not yet fully written is a graphical front end that will allow a direct interface to these tools. In the meantime, I&#8217;ll post what I have written, as these tools are now at a fully useable and documented stage.</p>
<p>I&#8217;ll give a few examples of how you might use these tools here. I&#8217;ll create some data that has an interesting shape.</p>
<pre>
x = (sort(rand(1,100)) - 0.5)*pi;
y = sin(x).^5 + randn(size(x))/25;
plot(x,y,'o')
</pre>
<p>You can see that the curve has a fair amount of noise in it. Obviously, we know the function must be monotone, given the way I&#8217;ve created the curve itself. Suppose we did not know that underlying model? A common solution here is to employ a nonlinear regression as I said before. A BETTER solution is to use my shape language modeling tools. The slmengine tool has a property/value pair interface. (I&#8217;m sorry that I cannot actually show you these plots from this response window, because they are instructive.)</p>
<pre>
slm = slmengine(x,y,'plot','on','knots',10)
slm =
            form: 'slm'
          degree: 3
           knots: [10x1 double]
            coef: [10x2 double]
    prescription: [1x1 struct]
               x: [100x1 double]
               y: [100x1 double]
</pre>
<p>This returns the cubic spline in a Hermite form. It has 10 knots/breaks. I could also have specified a pp form as the result.</p>
<pre>
pp = slmengine(x,y,'plot','on','knots',10,'result','pp')
pp =
            form: 'pp'
          breaks: [-1.5334 -1.1988 -0.86411 -0.52947 -0.19482 0.13983 0.47447 0.80912 1.1438 1.4784]
           coefs: [9x4 double]
          pieces: 9
           order: 4
             dim: 1
    prescription: [1x1 struct]
</pre>
<p>In either case, the result contains a &#8220;prescription&#8221; field. This contains all of the information that was indicated when we built the spline. It is useful for future reference. You can also get a hint about the possible things one can specify about the shape of your curve.</p>
<pre>
slm.prescription
ans =
                    C2: 'on'
           ConcaveDown: 'off'
             ConcaveUp: 'off'
        ConstantRegion: []
            Decreasing: 'off'
                Degree: 3
         EndConditions: 'estimate'
              Envelope: 'off'
              ErrorBar: []
            Increasing: 'off'
              Integral: []
         InteriorKnots: 'fixed'
                 Knots: 10
          LeftMaxSlope: []
          LeftMaxValue: []
          LeftMinSlope: []
          LeftMinValue: []
             LeftSlope: []
             LeftValue: []
          LinearRegion: []
              MaxSlope: []
              MaxValue: []
              MinSlope: []
              MinValue: []
    NegativeInflection: []
                 Order: []
                  Plot: 'on'
    PositiveInflection: []
        Regularization: 0.0001
                Result: 'pp'
         RightMaxSlope: []
         RightMaxValue: []
         RightMinSlope: []
         RightMinValue: []
            RightSlope: []
            RightValue: []
               Scaling: 'on'
            SimplePeak: []
          SimpleValley: []
             Verbosity: 0
               Weights: []
                    XY: []
                   XYP: []
                  XYPP: []
</pre>
<p>The slm form of our model can be used by some other tools I supply in this toolkit.</p>
<pre>
plotslm(slm)
</pre>
<p>will plot the spline, giving you a nice graphical interface to look at the derivatives, as well as plot the data on top of it if desired. I also give you tools to evaluate a spline, its derivatives, integrate the spline, etc. All of this is covered in great detail in a tutorial that I&#8217;ve written.</p>
<p>Back to our original curve, the basic call we used before specified that the final result be plotted. Furthermore, the spline will have 10 knots, equally spaced over the span of the data. A better curve fit results from adding more information.</p>
<pre>
slm = slmengine(x,y,'plot','on','knots',10,'increasing','on');
</pre>
<p>Amazingly, this curve is now quite smooth. Simply providing information about the process and building it into your model is quite simple, as I hope you can see here. Here I&#8217;ve now added two more pieces of information, i.e., that the left and right hand end point slopes are exactly zero.</p>
<pre>
slm = slmengine(x,y,'plot','on','knots',10,'increasing','on','leftslope',0,'rightslope',0);
</pre>
<p>I can go on further. Suppose I decided that the curve was not quite flat enough in the central region for my purposes. For example, evaluate the second derivative at a few points near zero.</p>
<pre>
slmeval([-.2 0 .2],slm,2)
ans =
    -0.029646  -1.7287e-14     0.035005
</pre>
<p>Perhaps I want the curve to be constant over that interval? This is what you have asked for, after all.</p>
<pre>
slm = slmengine(x,y,'plot','on','knots',10,'increasing','on','leftslope',0,'rightslope',0,'constantregion',[-.2,.2])
</pre>
<p>See that the second derivative is indeed flat in the vicinity of x = 0.</p>
<pre>
slmeval([-.2 0 .2],slm,2)
ans =
  -5.7416e-15   1.6138e-15  -1.0432e-15
</pre>
<p>Alternatively, we can look at the maximum and minimum values of the slope over that interval.</p>
<pre>
slmpar(slm,'maxslope',[-.2,.2])
ans =
   3.1475e-16

slmpar(slm,'minslope',[-.2,.2])
ans =
  -4.1045e-16
</pre>
<p>I hope that this toolkit is of use to you. I will argue that it will be so for many users of curve fitting, since it allows them to build models for their systems with the intrinsic shapes they know must be present. I am sorry that it took a few days for me to get this up on the file exchange for you. I did want to finish up a few things. As I said above, the slm toolkit should appear on the File Exchange on Monday, 6/15/09. Once it is up, I will plan on making many enhancements over the coming weeks, since I expect to see very much feedback on these tools. I&#8217;ll also be cleaning up any minor problem that surely will surface.</p>
<p>The SLM toolkit does not require the splines toolbox, although it does use the optimization toolbox because of the class of problems that it forms internally. Really, it mainly uses the function lsqlin, although some rare problems employ other optimizers from that toolbox.</p>
<p>John D&#8217;Errico</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zoh</title>
		<link>http://blogs.mathworks.com/loren/2008/07/17/interpolating-polynomials/#comment-30381</link>
		<dc:creator>Zoh</dc:creator>
		<pubDate>Fri, 12 Jun 2009 12:53:49 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/07/17/interpolating-polynomials/#comment-30381</guid>
		<description>Hello,

I am currently working on the matlab spline toolbox in order to describe a specific movement. I have two question:

Using a cubic spline function, how can we specify the condition at the interior points, (i want to have a straight horizontal line between two of the interior points, i imagine it could be possible by defining zero slopes at these two points or by joining different splines).

can anybody help ???

thanks in advance

Zohaib</description>
		<content:encoded><![CDATA[<p>Hello,</p>
<p>I am currently working on the matlab spline toolbox in order to describe a specific movement. I have two question:</p>
<p>Using a cubic spline function, how can we specify the condition at the interior points, (i want to have a straight horizontal line between two of the interior points, i imagine it could be possible by defining zero slopes at these two points or by joining different splines).</p>
<p>can anybody help ???</p>
<p>thanks in advance</p>
<p>Zohaib</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John DErrico</title>
		<link>http://blogs.mathworks.com/loren/2008/07/17/interpolating-polynomials/#comment-30349</link>
		<dc:creator>John DErrico</dc:creator>
		<pubDate>Wed, 03 Jun 2009 16:29:55 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/07/17/interpolating-polynomials/#comment-30349</guid>
		<description>Vic,

Normally when you have non-distinct data points, the answer is to average them. Interpolating splines simply cannot yield two different values of y (the dependent variable) for the same x (independent variable.)

So the traditional response is to average. the y for replicate x. My consolidator code was written for this purpose (on the File Exchange.)

It sounds like you have a relationship where this fails. This can happen when you try to interpolate data points around the perimeter of a circle, for example. Here we have a perfectly simple relationship, but where the interp1 type of spline interpolant will fail miserably. The trick is to use a parametric interpolant, as you alluded to. This is done in cscvn, from the splines toolbox, but you can easily do it yourself.

Here is an example:

&lt;pre&gt;
xy = randn(20,2);
r = sqrt(sum(xy.^2,2));
x = xy(:,1)./r;
y = xy(:,2)./r;
plot(x,y,'o')
&lt;/pre&gt;


As you can see, these points lie on the circumference of a circle. I'll sort them, based on angle.

&lt;pre&gt;
theta = atan2(y,x);
[theta,tags] = sort(theta);
x = x(tags);
y = y(tags);
&lt;/pre&gt;


Wrap around, since we want our interpolant to be around a full circle.

&lt;pre&gt;
x = [x;x(1)];
y = [y;y(1)];
plot(x,y,'o-')
&lt;/pre&gt;


As you surely see, the piecewise linear interpolant here is poor. There will be some big sectors of the circle that are poorly approximated by only 20 random points. The trick is to use piecewise linear chord length to parameterize on. Yeah, I know that sounds impressive. but can I write it in one line of code? (Yes.) In fact, I've always thought this was kind of pretty.


&lt;pre&gt;
t = cumsum(sqrt(([0;diff(x)].^2 + [0;diff(y)].^2)));
plot(t,x,'r-o',t,y,'b-s')
&lt;/pre&gt;


It should not surprise you that these two curves look vaguely trigonometric. Can we use it though?


&lt;pre&gt;
tinterp = linspace(t(1),t(end),1000);
xinterp = interp1(t,x,tinterp,'spline');
yinterp = interp1(t,y,tinterp,'spline');
plot(xinterp,yinterp,'r-')
hold on
plot(x,y,'bo')
hold off
grid on
axis equal
&lt;/pre&gt;


If I do say so myself, this is not a bad circular interpolant. Ok, there will be someone in the peanut gallery who will point out that this interpolant is not perfect, because it need not be differentiable across the wrap point. Recall that I used the basic spline from interp1, which employed not-a-knot end conditions. Had I used a spline with periodic end conditions, I would have gotten a somewhat better result. A simple trick, if you don't want to go through the effort of building/finding an interpolating spline with periodic end conditions, is to wrap several points around each end of the spline. This will get you closer to periodicity without making any serious mental effort.

These arc length parameterized splines are nice, because they work as well in higher dimensions. 

John</description>
		<content:encoded><![CDATA[<p>Vic,</p>
<p>Normally when you have non-distinct data points, the answer is to average them. Interpolating splines simply cannot yield two different values of y (the dependent variable) for the same x (independent variable.)</p>
<p>So the traditional response is to average. the y for replicate x. My consolidator code was written for this purpose (on the File Exchange.)</p>
<p>It sounds like you have a relationship where this fails. This can happen when you try to interpolate data points around the perimeter of a circle, for example. Here we have a perfectly simple relationship, but where the interp1 type of spline interpolant will fail miserably. The trick is to use a parametric interpolant, as you alluded to. This is done in cscvn, from the splines toolbox, but you can easily do it yourself.</p>
<p>Here is an example:</p>
<pre>
xy = randn(20,2);
r = sqrt(sum(xy.^2,2));
x = xy(:,1)./r;
y = xy(:,2)./r;
plot(x,y,'o')
</pre>
<p>As you can see, these points lie on the circumference of a circle. I&#8217;ll sort them, based on angle.</p>
<pre>
theta = atan2(y,x);
[theta,tags] = sort(theta);
x = x(tags);
y = y(tags);
</pre>
<p>Wrap around, since we want our interpolant to be around a full circle.</p>
<pre>
x = [x;x(1)];
y = [y;y(1)];
plot(x,y,'o-')
</pre>
<p>As you surely see, the piecewise linear interpolant here is poor. There will be some big sectors of the circle that are poorly approximated by only 20 random points. The trick is to use piecewise linear chord length to parameterize on. Yeah, I know that sounds impressive. but can I write it in one line of code? (Yes.) In fact, I&#8217;ve always thought this was kind of pretty.</p>
<pre>
t = cumsum(sqrt(([0;diff(x)].^2 + [0;diff(y)].^2)));
plot(t,x,'r-o',t,y,'b-s')
</pre>
<p>It should not surprise you that these two curves look vaguely trigonometric. Can we use it though?</p>
<pre>
tinterp = linspace(t(1),t(end),1000);
xinterp = interp1(t,x,tinterp,'spline');
yinterp = interp1(t,y,tinterp,'spline');
plot(xinterp,yinterp,'r-')
hold on
plot(x,y,'bo')
hold off
grid on
axis equal
</pre>
<p>If I do say so myself, this is not a bad circular interpolant. Ok, there will be someone in the peanut gallery who will point out that this interpolant is not perfect, because it need not be differentiable across the wrap point. Recall that I used the basic spline from interp1, which employed not-a-knot end conditions. Had I used a spline with periodic end conditions, I would have gotten a somewhat better result. A simple trick, if you don&#8217;t want to go through the effort of building/finding an interpolating spline with periodic end conditions, is to wrap several points around each end of the spline. This will get you closer to periodicity without making any serious mental effort.</p>
<p>These arc length parameterized splines are nice, because they work as well in higher dimensions. </p>
<p>John</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vic</title>
		<link>http://blogs.mathworks.com/loren/2008/07/17/interpolating-polynomials/#comment-30340</link>
		<dc:creator>Vic</dc:creator>
		<pubDate>Tue, 26 May 2009 14:46:58 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/07/17/interpolating-polynomials/#comment-30340</guid>
		<description>Hi,

I'm trying to fit a spline through non distinct data. I know parametrising is the way to go because the repeated values are important. However im not sure how to efficiently parametrise the data points i have in order to fit a cubic spline through it. Any ideas or help would be greatly appreciated.

Vic</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I&#8217;m trying to fit a spline through non distinct data. I know parametrising is the way to go because the repeated values are important. However im not sure how to efficiently parametrise the data points i have in order to fit a cubic spline through it. Any ideas or help would be greatly appreciated.</p>
<p>Vic</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rSekuler</title>
		<link>http://blogs.mathworks.com/loren/2008/07/17/interpolating-polynomials/#comment-29929</link>
		<dc:creator>rSekuler</dc:creator>
		<pubDate>Sun, 14 Dec 2008 18:44:01 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/07/17/interpolating-polynomials/#comment-29929</guid>
		<description>John:  Can you recommend an approach to this possibly-unusal problem?  I have data sets in which y=k over some interval (x&#60;n), after which there is a fairly abrupt change to y=mx.  The value of n at which the change occurs varies over conditions, and I need to estimate best fitting value of n for each data set.</description>
		<content:encoded><![CDATA[<p>John:  Can you recommend an approach to this possibly-unusal problem?  I have data sets in which y=k over some interval (x&lt;n), after which there is a fairly abrupt change to y=mx.  The value of n at which the change occurs varies over conditions, and I need to estimate best fitting value of n for each data set.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sundar</title>
		<link>http://blogs.mathworks.com/loren/2008/07/17/interpolating-polynomials/#comment-29857</link>
		<dc:creator>sundar</dc:creator>
		<pubDate>Mon, 10 Nov 2008 12:32:30 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/07/17/interpolating-polynomials/#comment-29857</guid>
		<description>John DErrico ,
Thanks for that.

I need to generate a closed spline i.e. when I plot theta (independent variable) against radius (dependent variable) in polar graph, I should get a continuous spline curve. 

Using spapi(), if I give 1st data point again as last one to make it a closed one,I am not getting smooth curve at this closing point. Even if I repeat the second point, result is a distorted spline instead of a smooth one.

I need to do this for quintic and cubic splines.

Can you pl. help on this. Thanks in advance.

-Sundar</description>
		<content:encoded><![CDATA[<p>John DErrico ,<br />
Thanks for that.</p>
<p>I need to generate a closed spline i.e. when I plot theta (independent variable) against radius (dependent variable) in polar graph, I should get a continuous spline curve. </p>
<p>Using spapi(), if I give 1st data point again as last one to make it a closed one,I am not getting smooth curve at this closing point. Even if I repeat the second point, result is a distorted spline instead of a smooth one.</p>
<p>I need to do this for quintic and cubic splines.</p>
<p>Can you pl. help on this. Thanks in advance.</p>
<p>-Sundar</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John DErrico</title>
		<link>http://blogs.mathworks.com/loren/2008/07/17/interpolating-polynomials/#comment-29817</link>
		<dc:creator>John DErrico</dc:creator>
		<pubDate>Fri, 17 Oct 2008 02:23:44 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/07/17/interpolating-polynomials/#comment-29817</guid>
		<description>Sundar - the easiest way to convert the result into a pp form is this:

function pp = B2PP(Bform)
% converts spapi created spline form to a pp form
% pp = B2PP(Bform)
breaks = unique(Bform.knots);
x = breaks(1:(end-1))';
n = Bform.order;
coef = zeros(length(x),n);
for i = 1:n
  coef(:,n-i+1) = fnval(Bform,x)/factorial(i-1);
  Bform = fnder(Bform);
end
pp = mkpp(breaks,coef);

John</description>
		<content:encoded><![CDATA[<p>Sundar - the easiest way to convert the result into a pp form is this:</p>
<p>function pp = B2PP(Bform)<br />
% converts spapi created spline form to a pp form<br />
% pp = B2PP(Bform)<br />
breaks = unique(Bform.knots);<br />
x = breaks(1:(end-1))&#8217;;<br />
n = Bform.order;<br />
coef = zeros(length(x),n);<br />
for i = 1:n<br />
  coef(:,n-i+1) = fnval(Bform,x)/factorial(i-1);<br />
  Bform = fnder(Bform);<br />
end<br />
pp = mkpp(breaks,coef);</p>
<p>John</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sundar</title>
		<link>http://blogs.mathworks.com/loren/2008/07/17/interpolating-polynomials/#comment-29816</link>
		<dc:creator>sundar</dc:creator>
		<pubDate>Thu, 16 Oct 2008 07:47:54 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/07/17/interpolating-polynomials/#comment-29816</guid>
		<description>John DErrico ,

Thanks again.

1. In that case, if I need the coefs. of those piecewise quintic curves, how can i get it?. This is reqd. for some manual derivation purposes.
2. How spapi fn. will determine the knot sequence for a given set of data.

thanks,
-sundar</description>
		<content:encoded><![CDATA[<p>John DErrico ,</p>
<p>Thanks again.</p>
<p>1. In that case, if I need the coefs. of those piecewise quintic curves, how can i get it?. This is reqd. for some manual derivation purposes.<br />
2. How spapi fn. will determine the knot sequence for a given set of data.</p>
<p>thanks,<br />
-sundar</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John DErrico</title>
		<link>http://blogs.mathworks.com/loren/2008/07/17/interpolating-polynomials/#comment-29806</link>
		<dc:creator>John DErrico</dc:creator>
		<pubDate>Tue, 14 Oct 2008 02:10:56 +0000</pubDate>
		<guid>http://blogs.mathworks.com/loren/2008/07/17/interpolating-polynomials/#comment-29806</guid>
		<description>Sundar - see the example I gave before.

x = 0:1:20;
y = sin(x/2);
S = spapi(6,x,y)
S = 
      form: 'B-'
     knots: [0 0 0 0 0 0 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 20 20 20 20 20 20]
     coefs: [1x21 double]
    number: 21
     order: 6
       dim: 1

Note that the form is shown as 'B-', as opposed to 'pp'. Plot the 5th derivative of this function.

fnplt(fnder(spl,5))

That derivative plot is piecewise constant. The function is NOT a single polynomial. This is a spline, composed of quintic polynomial segments. The coefficients of the spline are just not represented in the same pp form that spline produces.

spline(x,y)
ans = 
      form: 'pp'
    breaks: [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]
     coefs: [20x4 double]
    pieces: 20
     order: 4
       dim: 1

John</description>
		<content:encoded><![CDATA[<p>Sundar - see the example I gave before.</p>
<p>x = 0:1:20;<br />
y = sin(x/2);<br />
S = spapi(6,x,y)<br />
S =<br />
      form: &#8216;B-&#8217;<br />
     knots: [0 0 0 0 0 0 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 20 20 20 20 20 20]<br />
     coefs: [1&#215;21 double]<br />
    number: 21<br />
     order: 6<br />
       dim: 1</p>
<p>Note that the form is shown as &#8216;B-&#8217;, as opposed to &#8216;pp&#8217;. Plot the 5th derivative of this function.</p>
<p>fnplt(fnder(spl,5))</p>
<p>That derivative plot is piecewise constant. The function is NOT a single polynomial. This is a spline, composed of quintic polynomial segments. The coefficients of the spline are just not represented in the same pp form that spline produces.</p>
<p>spline(x,y)<br />
ans =<br />
      form: &#8216;pp&#8217;<br />
    breaks: [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]<br />
     coefs: [20&#215;4 double]<br />
    pieces: 20<br />
     order: 4<br />
       dim: 1</p>
<p>John</p>
]]></content:encoded>
	</item>
</channel>
</rss>
