<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Piecewise Linear Interpolation</title>
	<atom:link href="http://blogs.mathworks.com/loren/2008/08/25/piecewise-linear-interpolation/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2008/08/25/piecewise-linear-interpolation/</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Wed, 15 Feb 2012 15:23:22 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Jose Albornoz</title>
		<link>http://blogs.mathworks.com/loren/2008/08/25/piecewise-linear-interpolation/#comment-32801</link>
		<dc:creator>Jose Albornoz</dc:creator>
		<pubDate>Tue, 06 Dec 2011 14:40:43 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/08/25/piecewise-linear-interpolation/#comment-32801</guid>
		<description>Thank you, nice article!</description>
		<content:encoded><![CDATA[<p>Thank you, nice article!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2008/08/25/piecewise-linear-interpolation/#comment-32031</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Mon, 14 Feb 2011 12:19:25 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/08/25/piecewise-linear-interpolation/#comment-32031</guid>
		<description>Jeff-

Alas, nothing else from John.  You may want to see the recent post on data driven fitting: http://blogs.mathworks.com/loren/2011/01/13/data-driven-fitting/

--Loren</description>
		<content:encoded><![CDATA[<p>Jeff-</p>
<p>Alas, nothing else from John.  You may want to see the recent post on data driven fitting: <a href="http://blogs.mathworks.com/loren/2011/01/13/data-driven-fitting/" rel="nofollow">http://blogs.mathworks.com/loren/2011/01/13/data-driven-fitting/</a></p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeff</title>
		<link>http://blogs.mathworks.com/loren/2008/08/25/piecewise-linear-interpolation/#comment-32030</link>
		<dc:creator>Jeff</dc:creator>
		<pubDate>Sun, 13 Feb 2011 07:40:20 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/08/25/piecewise-linear-interpolation/#comment-32030</guid>
		<description>Was this series ever continued? I cannot find the next article.</description>
		<content:encoded><![CDATA[<p>Was this series ever continued? I cannot find the next article.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John D'Errico</title>
		<link>http://blogs.mathworks.com/loren/2008/08/25/piecewise-linear-interpolation/#comment-31336</link>
		<dc:creator>John D'Errico</dc:creator>
		<pubDate>Wed, 12 May 2010 15:13:17 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/08/25/piecewise-linear-interpolation/#comment-31336</guid>
		<description>I probably need more information to answer more precisely. Is this a piecewise linear question in 1 or 2 or more independent variables?

Piecewise linear interpolation is easy. Use interp1q, interp1, or interp2, interpn, etc., in higher dimensions. Or griddata for scattered data.

You did say piecewise linear APPROXIMATION though. This takes slightly more effort, but it still depends upon several issues. Is this a 1-d problem, i.e, only one independent variable? If so, then you can use my SLM tools on the file exchange to do it. Here is a link to it:

http://www.mathworks.com/matlabcentral/fileexchange/24443-slm-shape-language-modeling

Do you know where the breaks occur? If so, then specify them as knots to slmengine. If not, then as long as you know how many pieces there are, you can specify the interior knots to be free. slmengine will place them for you. If you have no idea how many breaks there are, then this becomes a more difficult problem. I have never seen an implementation of MARS in MATLAB, but that code could solve for a piecewise linear regression spline with an indeterminate number of breaks.

If this is a case of higher dimensional piecewise linear approximation, then my gridfit does this for two independent variables. Find it here:

http://www.mathworks.com/matlabcentral/fileexchange/8998-surface-fitting-using-gridfit

Remember that piecewise linear is something that must be carefully defined in higher dimensions, as even a tensor product linear interpolant (for example, what interp2 will give you if you specify &#039;linear&#039;) is not truly a linear interpolant by some definitions of what linear means.

Even radial basis function interpolation can be implemented in a &quot;linear&quot; form, although calling a linear RBF interpolant piecewise linear is a stretch. Regardless, you can find such a tool on the FEX. Look here:

http://www.mathworks.com/matlabcentral/fileexchange/10056-scattered-data-interpolation-and-approximation-using-radial-base-functions

So it very much depends on what exactly is your problem. Ask again if I have missed the mark, or if you need clarification.</description>
		<content:encoded><![CDATA[<p>I probably need more information to answer more precisely. Is this a piecewise linear question in 1 or 2 or more independent variables?</p>
<p>Piecewise linear interpolation is easy. Use interp1q, interp1, or interp2, interpn, etc., in higher dimensions. Or griddata for scattered data.</p>
<p>You did say piecewise linear APPROXIMATION though. This takes slightly more effort, but it still depends upon several issues. Is this a 1-d problem, i.e, only one independent variable? If so, then you can use my SLM tools on the file exchange to do it. Here is a link to it:</p>
<p><a href="http://www.mathworks.com/matlabcentral/fileexchange/24443-slm-shape-language-modeling" rel="nofollow">http://www.mathworks.com/matlabcentral/fileexchange/24443-slm-shape-language-modeling</a></p>
<p>Do you know where the breaks occur? If so, then specify them as knots to slmengine. If not, then as long as you know how many pieces there are, you can specify the interior knots to be free. slmengine will place them for you. If you have no idea how many breaks there are, then this becomes a more difficult problem. I have never seen an implementation of MARS in MATLAB, but that code could solve for a piecewise linear regression spline with an indeterminate number of breaks.</p>
<p>If this is a case of higher dimensional piecewise linear approximation, then my gridfit does this for two independent variables. Find it here:</p>
<p><a href="http://www.mathworks.com/matlabcentral/fileexchange/8998-surface-fitting-using-gridfit" rel="nofollow">http://www.mathworks.com/matlabcentral/fileexchange/8998-surface-fitting-using-gridfit</a></p>
<p>Remember that piecewise linear is something that must be carefully defined in higher dimensions, as even a tensor product linear interpolant (for example, what interp2 will give you if you specify &#8216;linear&#8217;) is not truly a linear interpolant by some definitions of what linear means.</p>
<p>Even radial basis function interpolation can be implemented in a &#8220;linear&#8221; form, although calling a linear RBF interpolant piecewise linear is a stretch. Regardless, you can find such a tool on the FEX. Look here:</p>
<p><a href="http://www.mathworks.com/matlabcentral/fileexchange/10056-scattered-data-interpolation-and-approximation-using-radial-base-functions" rel="nofollow">http://www.mathworks.com/matlabcentral/fileexchange/10056-scattered-data-interpolation-and-approximation-using-radial-base-functions</a></p>
<p>So it very much depends on what exactly is your problem. Ask again if I have missed the mark, or if you need clarification.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dipesh</title>
		<link>http://blogs.mathworks.com/loren/2008/08/25/piecewise-linear-interpolation/#comment-31335</link>
		<dc:creator>dipesh</dc:creator>
		<pubDate>Wed, 12 May 2010 12:32:26 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/08/25/piecewise-linear-interpolation/#comment-31335</guid>
		<description>Hi all,
would anyone tell me how to do piecewise linear approximation using matlab?</description>
		<content:encoded><![CDATA[<p>Hi all,<br />
would anyone tell me how to do piecewise linear approximation using matlab?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John D'Errico</title>
		<link>http://blogs.mathworks.com/loren/2008/08/25/piecewise-linear-interpolation/#comment-31277</link>
		<dc:creator>John D'Errico</dc:creator>
		<pubDate>Mon, 19 Apr 2010 01:32:24 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/08/25/piecewise-linear-interpolation/#comment-31277</guid>
		<description>This is a complicated question, depending on more than the relative number of breaks there are in the &quot;spline&quot;, and the number of data points to be interpolated. By the way, a piecewise linear interpolation is indeed a spline, only a piecewise linear spline, but yet a spline.

If you ask about general the complexity of linear interpolation, we must define exactly how that interpolation is to be accomplished. It sounds like you are asking for the complexity of interp1q, which is not representative of ALL linear interpolation schemes.

There are two main parts of any such interpolation. First, you must determine which knot interval contains each data point. Then, for each data point, you retrieve the values of the function at each end of the respective interval, and perform the linear interpolation. At that point, the linear interpolation is little more than an add or two, a divide, and a multiply.

The dominant factor in such a scheme is often the determination of which knot interval a given point falls in. That determination is a function of how many breaks there are, and how many data points there are. In fact, interp1q uses a sort to do this operation. A sort is a reasonable choice, depending upon the relative number of breaks and the number of points to interpolate. However, if you had only a single data point, yet n (the number of knots) is in the millions, a simple bisection scheme will take O(log(n)) tests to determine which knot interval the point falls in. This would be more efficient than a full sort. (I&#039;ll note that for scalar values to interpolate, interp1q actually uses a find. This may be faster in terms of MATLAB code than a MATLAB implementation of a bisection scheme.)

Even better, suppose you knew that the breaks of your spline were equally spaced? You could then determine which knot interval a point lies inside using only a single subtract and a divide.

Back to the complexity of specifically interp1q, we can break it down to a sort of (n+m) numbers, where n is the number of breaks, m the number of data points. This should indeed be roughly O((n+m)*log(n+m)), at least assuming MATLAB does the sort in an efficient manner.

To that, we must now add in the second part of the operation, the interpolation itself. This is clearly an O(m) operation. While those are the main operations that will require cpu time in the interp1q operation, other aspects of the code, as well as function call overhead in MATLAB must be considered.

In the end I&#039;ll suggest that these O estimates are all somewhat suspect. We must consider how MATLAB chooses to implement any accelerations (code optimizations) for you. We must recognize that the CPU in your computer is a complex beast that has various levels of cache. These caches heavily impact any operation counts one might make, to the extent that your count may be in fact only barely valid. A not insignificant factor in the problem may even be the time required to load the interpolation data in from memory, in the event that your spline is longer than the available cache memory.

My point is to be careful not to put too much weight on any such computation.

John</description>
		<content:encoded><![CDATA[<p>This is a complicated question, depending on more than the relative number of breaks there are in the &#8220;spline&#8221;, and the number of data points to be interpolated. By the way, a piecewise linear interpolation is indeed a spline, only a piecewise linear spline, but yet a spline.</p>
<p>If you ask about general the complexity of linear interpolation, we must define exactly how that interpolation is to be accomplished. It sounds like you are asking for the complexity of interp1q, which is not representative of ALL linear interpolation schemes.</p>
<p>There are two main parts of any such interpolation. First, you must determine which knot interval contains each data point. Then, for each data point, you retrieve the values of the function at each end of the respective interval, and perform the linear interpolation. At that point, the linear interpolation is little more than an add or two, a divide, and a multiply.</p>
<p>The dominant factor in such a scheme is often the determination of which knot interval a given point falls in. That determination is a function of how many breaks there are, and how many data points there are. In fact, interp1q uses a sort to do this operation. A sort is a reasonable choice, depending upon the relative number of breaks and the number of points to interpolate. However, if you had only a single data point, yet n (the number of knots) is in the millions, a simple bisection scheme will take O(log(n)) tests to determine which knot interval the point falls in. This would be more efficient than a full sort. (I&#8217;ll note that for scalar values to interpolate, interp1q actually uses a find. This may be faster in terms of MATLAB code than a MATLAB implementation of a bisection scheme.)</p>
<p>Even better, suppose you knew that the breaks of your spline were equally spaced? You could then determine which knot interval a point lies inside using only a single subtract and a divide.</p>
<p>Back to the complexity of specifically interp1q, we can break it down to a sort of (n+m) numbers, where n is the number of breaks, m the number of data points. This should indeed be roughly O((n+m)*log(n+m)), at least assuming MATLAB does the sort in an efficient manner.</p>
<p>To that, we must now add in the second part of the operation, the interpolation itself. This is clearly an O(m) operation. While those are the main operations that will require cpu time in the interp1q operation, other aspects of the code, as well as function call overhead in MATLAB must be considered.</p>
<p>In the end I&#8217;ll suggest that these O estimates are all somewhat suspect. We must consider how MATLAB chooses to implement any accelerations (code optimizations) for you. We must recognize that the CPU in your computer is a complex beast that has various levels of cache. These caches heavily impact any operation counts one might make, to the extent that your count may be in fact only barely valid. A not insignificant factor in the problem may even be the time required to load the interpolation data in from memory, in the event that your spline is longer than the available cache memory.</p>
<p>My point is to be careful not to put too much weight on any such computation.</p>
<p>John</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Martin H. Skjelvareid</title>
		<link>http://blogs.mathworks.com/loren/2008/08/25/piecewise-linear-interpolation/#comment-31275</link>
		<dc:creator>Martin H. Skjelvareid</dc:creator>
		<pubDate>Thu, 15 Apr 2010 22:02:52 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/08/25/piecewise-linear-interpolation/#comment-31275</guid>
		<description>Hello, 

I can see that some time has passed since the last post, but I&#039;ll try positng my question anyway: 

What is the computational complexity of linear interpolation, in Big O notation? I have been looking into the code for the interp1q function, and it seems to me that the most computationally demanding step is a sort of the the stacked x and xi values, sort([x;xi]). Defining N = length(x) + length(xi), can I assume that the sorting algorithm, and thus also interp1q, is Theta(N log N)?</description>
		<content:encoded><![CDATA[<p>Hello, </p>
<p>I can see that some time has passed since the last post, but I&#8217;ll try positng my question anyway: </p>
<p>What is the computational complexity of linear interpolation, in Big O notation? I have been looking into the code for the interp1q function, and it seems to me that the most computationally demanding step is a sort of the the stacked x and xi values, sort([x;xi]). Defining N = length(x) + length(xi), can I assume that the sorting algorithm, and thus also interp1q, is Theta(N log N)?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John DErrico</title>
		<link>http://blogs.mathworks.com/loren/2008/08/25/piecewise-linear-interpolation/#comment-30795</link>
		<dc:creator>John DErrico</dc:creator>
		<pubDate>Mon, 16 Nov 2009 14:30:05 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/08/25/piecewise-linear-interpolation/#comment-30795</guid>
		<description>Tina,

If I understand your question correctly, you are trying to solve an interpolation problem where the two dependent variables are given, and you wish to recover the values of three independent variables. If a solution exists, then it will generally be infinitely many solutions, i.e., a 1-manifold of solutions that lie in the R^3 domain space of this problem. Think of this as an iso-line. Is any point on this locus of solutions a valid solution? 

As is often the case, my approach would depend on my goals. If a piecewise linear approximation to the 1-manifold was acceptable, then I might start with a triangulation of the R^3 domain set. Then slice through this twice to reduce that triangulation to a piecewise linear 1-manifold. Now I can do anything I want with that curve. It is quite nice for visualization purposes, and sometimes for other end uses. I would use a set of custom made tools for these slices that I have never gotten around to putting on the File Exchange.

If my goal is otherwise, I might apply fmincon (rather than Newton-Raphson, which cannot employ constraints) to solve for the closest point to my starting point, such that the dependent variables are properly interpolated (as a pair of nonlinear equality constraints.) Here my interpolation might use griddata or interp2, with a smooth method employed.

Let me know if I can be of further help, or if my understanding of your question was incorrect.

John</description>
		<content:encoded><![CDATA[<p>Tina,</p>
<p>If I understand your question correctly, you are trying to solve an interpolation problem where the two dependent variables are given, and you wish to recover the values of three independent variables. If a solution exists, then it will generally be infinitely many solutions, i.e., a 1-manifold of solutions that lie in the R^3 domain space of this problem. Think of this as an iso-line. Is any point on this locus of solutions a valid solution? </p>
<p>As is often the case, my approach would depend on my goals. If a piecewise linear approximation to the 1-manifold was acceptable, then I might start with a triangulation of the R^3 domain set. Then slice through this twice to reduce that triangulation to a piecewise linear 1-manifold. Now I can do anything I want with that curve. It is quite nice for visualization purposes, and sometimes for other end uses. I would use a set of custom made tools for these slices that I have never gotten around to putting on the File Exchange.</p>
<p>If my goal is otherwise, I might apply fmincon (rather than Newton-Raphson, which cannot employ constraints) to solve for the closest point to my starting point, such that the dependent variables are properly interpolated (as a pair of nonlinear equality constraints.) Here my interpolation might use griddata or interp2, with a smooth method employed.</p>
<p>Let me know if I can be of further help, or if my understanding of your question was incorrect.</p>
<p>John</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tina</title>
		<link>http://blogs.mathworks.com/loren/2008/08/25/piecewise-linear-interpolation/#comment-30789</link>
		<dc:creator>Tina</dc:creator>
		<pubDate>Sat, 14 Nov 2009 05:13:56 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/08/25/piecewise-linear-interpolation/#comment-30789</guid>
		<description>Do you know of an interpolation scheme for nonmonotonic functiona?  I have three independent variables and two dependent variables.  I am using the interpolated variables to solve an energy equation.  The process is iterative and I need an algorithm that will not shoot off into space.  The code I use now uses Newton-Raphson to find a solution and it is restricted to monotonic data.  Any thoughts?</description>
		<content:encoded><![CDATA[<p>Do you know of an interpolation scheme for nonmonotonic functiona?  I have three independent variables and two dependent variables.  I am using the interpolated variables to solve an energy equation.  The process is iterative and I need an algorithm that will not shoot off into space.  The code I use now uses Newton-Raphson to find a solution and it is restricted to monotonic data.  Any thoughts?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John DErrico</title>
		<link>http://blogs.mathworks.com/loren/2008/08/25/piecewise-linear-interpolation/#comment-30511</link>
		<dc:creator>John DErrico</dc:creator>
		<pubDate>Tue, 04 Aug 2009 19:54:10 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/08/25/piecewise-linear-interpolation/#comment-30511</guid>
		<description>Hi Ahmed,

Is this a Simulink question I think? Your question is very confusing, so I am not sure how to respond. You need to work out what is your question. What are you trying to do here? I have said this many times before, but the most important part of solving a difficult problem is in simply stating the problem accurately.

Regardless of the environment you will use it in, let me talk briefly about lookup tables. A lookup table generally is defined by a list of points, an input and an output for each input. I&#039;ll call the input x (the independent variable) and the output variable y. y is dependent upon x. Now, suppose we will use the lookup table. For a new value, call it X, we wish to determine an appropriate output value Y. 

With one dimensional input, we must identify the pair of points in x that bound the new value X. Then use some scheme to predict a value for Y at that location. Some lookup tables will merely use nearest neighbor interpolation or a variant of it, or linear interpolation, or perhaps a cubic spline interplant. The choice of method would be up to you. But regardless, the table itself is fixed, dependent upon what data you supply. So if your data points represent a decreasing relationship, then so would your lookup table. (Note that some choice of interpolants will not always produce a monotone interpolant even for monotone data. This is why PCHIP was invented, and provided as part of interp1.)

So please try restating your problem.

Regards,
John</description>
		<content:encoded><![CDATA[<p>Hi Ahmed,</p>
<p>Is this a Simulink question I think? Your question is very confusing, so I am not sure how to respond. You need to work out what is your question. What are you trying to do here? I have said this many times before, but the most important part of solving a difficult problem is in simply stating the problem accurately.</p>
<p>Regardless of the environment you will use it in, let me talk briefly about lookup tables. A lookup table generally is defined by a list of points, an input and an output for each input. I&#8217;ll call the input x (the independent variable) and the output variable y. y is dependent upon x. Now, suppose we will use the lookup table. For a new value, call it X, we wish to determine an appropriate output value Y. </p>
<p>With one dimensional input, we must identify the pair of points in x that bound the new value X. Then use some scheme to predict a value for Y at that location. Some lookup tables will merely use nearest neighbor interpolation or a variant of it, or linear interpolation, or perhaps a cubic spline interplant. The choice of method would be up to you. But regardless, the table itself is fixed, dependent upon what data you supply. So if your data points represent a decreasing relationship, then so would your lookup table. (Note that some choice of interpolants will not always produce a monotone interpolant even for monotone data. This is why PCHIP was invented, and provided as part of interp1.)</p>
<p>So please try restating your problem.</p>
<p>Regards,<br />
John</p>
]]></content:encoded>
	</item>
</channel>
</rss>

