<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238438</link>
    <title>MATLAB Central Newsreader - interpolating/smoothing w/ monotonically increasing</title>
    <description>Feed for thread: interpolating/smoothing w/ monotonically increasing</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Thu, 30 Oct 2008 18:56:03 -0400</pubDate>
      <title>interpolating/smoothing w/ monotonically increasing</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238438#608150</link>
      <author>Pete sherer</author>
      <description>Hi,&lt;br&gt;
&lt;br&gt;
Are there anyway or function that can perform an interpolation or smoothing of data such that the output will be monotonically increasing, even some portion of data don't support so.&lt;br&gt;
&lt;br&gt;
x=[0.1;0.2;0.3;0.4;0.5;0.6;0.7;0.8;0.9;1;];&lt;br&gt;
y=[183.6992;347.6811;184.4308;199.1481;308.3963;453.1903;490.1048;558.374;613.4362;623.4727;];&lt;br&gt;
&lt;br&gt;
I prefer the dipping part to be simply ignore during interpolation if possible.&lt;br&gt;
&lt;br&gt;
Thanks for any suggestions and hints</description>
    </item>
    <item>
      <pubDate>Thu, 30 Oct 2008 19:14:03 -0400</pubDate>
      <title>Re: interpolating/smoothing w/ monotonically increasing</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238438#608155</link>
      <author>Bruno Luong</author>
      <description>&quot;Pete sherer&quot; &amp;lt;tsh@abg.com&amp;gt; wrote in message &amp;lt;ged003$oi7$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Are there anyway or function that can perform an interpolation or smoothing of data such that the output will be monotonically increasing, even some portion of data don't support so.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; x=[0.1;0.2;0.3;0.4;0.5;0.6;0.7;0.8;0.9;1;];&lt;br&gt;
&amp;gt; y=[183.6992;347.6811;184.4308;199.1481;308.3963;453.1903;490.1048;558.374;613.4362;623.4727;];&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I prefer the dipping part to be simply ignore during interpolation if possible.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks for any suggestions and hints&lt;br&gt;
&lt;br&gt;
I believe this kind of problem can be rewritten as least-square minimization (fit) with linear constraints.&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Thu, 30 Oct 2008 19:43:01 -0400</pubDate>
      <title>Re: interpolating/smoothing w/ monotonically increasing</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238438#608162</link>
      <author>John D'Errico</author>
      <description>&quot;Bruno Luong&quot; &amp;lt;b.luong@fogale.findmycountry&amp;gt; wrote in message &amp;lt;ged11r$f59$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Pete sherer&quot; &amp;lt;tsh@abg.com&amp;gt; wrote in message &amp;lt;ged003$oi7$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Hi,&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Are there anyway or function that can perform an interpolation or smoothing of data such that the output will be monotonically increasing, even some portion of data don't support so.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; x=[0.1;0.2;0.3;0.4;0.5;0.6;0.7;0.8;0.9;1;];&lt;br&gt;
&amp;gt; &amp;gt; y=[183.6992;347.6811;184.4308;199.1481;308.3963;453.1903;490.1048;558.374;613.4362;623.4727;];&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I prefer the dipping part to be simply ignore during interpolation if possible.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Thanks for any suggestions and hints&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I believe this kind of problem can be rewritten as least-square minimization (fit) with linear constraints.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Bruno&lt;br&gt;
&lt;br&gt;
Yes, it can, almost. The problem is the least&lt;br&gt;
squares may cause some of the data points to&lt;br&gt;
not get fit exactly, even when the curve and&lt;br&gt;
the data truly is monotone in that vicinity.&lt;br&gt;
&lt;br&gt;
Or, if you use one knot per data point, then&lt;br&gt;
a least squares fit can be trivially written&lt;br&gt;
using lsqlin, but it may be slow if you have a&lt;br&gt;
large number of points, since there will be&lt;br&gt;
one constraint for each pair of consecutive&lt;br&gt;
data points. For example, on the given curve,&lt;br&gt;
try this...&lt;br&gt;
&lt;br&gt;
x=[0.1;0.2;0.3;0.4;0.5;0.6;0.7;0.8;0.9;1;];&lt;br&gt;
y=[183.6992;347.6811;184.4308;199.1481;308.3963;453.1903;490.1048;558.374;613.4362;623.4727;];&lt;br&gt;
&lt;br&gt;
n = length(x);&lt;br&gt;
C = eye(n);&lt;br&gt;
D = y;&lt;br&gt;
A = diag(ones(n,1),0) - diag(ones(n-1,1),1);&lt;br&gt;
A(end,:) = [];&lt;br&gt;
b = zeros(n-1,1);&lt;br&gt;
&lt;br&gt;
opts = optimset('lsqlin');&lt;br&gt;
opts.LargeScale = 'off';&lt;br&gt;
opts.Display = 'none';&lt;br&gt;
yhat = lsqlin(C,D,A,b,[],[],[],[],[],opts);&lt;br&gt;
&lt;br&gt;
plot(x,y,'ro',x,yhat,'b-*')&lt;br&gt;
&lt;br&gt;
If you like, now you could use interp1 (pchip)&lt;br&gt;
to do a montone interpolation for the fitted&lt;br&gt;
results.&lt;br&gt;
&lt;br&gt;
How did it do? &lt;br&gt;
&lt;br&gt;
yhat&lt;br&gt;
yhat =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;183.7&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;243.75&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;243.75&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;243.75&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;308.4&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;453.19&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;490.1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;558.37&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;613.44&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;623.47&lt;br&gt;
&lt;br&gt;
See that it has missed a total of 3 points. All&lt;br&gt;
of the others will have been fit exactly, but&lt;br&gt;
those three were ambiguous. How does lsqlin&lt;br&gt;
know which point was in error? So it has&lt;br&gt;
adjusted 3 consecutive points by the smallest&lt;br&gt;
aggregate amount to yield a monotone curve.&lt;br&gt;
&lt;br&gt;
Yes, usually the human eye is more able to see&lt;br&gt;
that it was actually point #2 here that was in&lt;br&gt;
error, but that will not always be the case.&lt;br&gt;
&lt;br&gt;
HTH,&lt;br&gt;
John</description>
    </item>
    <item>
      <pubDate>Thu, 30 Oct 2008 19:47:01 -0400</pubDate>
      <title>Re: interpolating/smoothing w/ monotonically increasing</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238438#608165</link>
      <author>Pete sherer</author>
      <description>Thanks Bruno.  I would prefer some kinds of interpolation in stead of doing regression which might not fit well at the end point (like left point).  Is it doable?</description>
    </item>
    <item>
      <pubDate>Thu, 30 Oct 2008 20:58:01 -0400</pubDate>
      <title>Re: interpolating/smoothing w/ monotonically increasing</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238438#608189</link>
      <author>John D'Errico</author>
      <description>&quot;Pete sherer&quot; &amp;lt;tsh@abg.com&amp;gt; wrote in message &amp;lt;ged2vl$ep1$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Thanks Bruno.  I would prefer some kinds of interpolation in stead of doing regression which might not fit well at the end point (like left point).  Is it doable?&lt;br&gt;
&lt;br&gt;
No, it is not doable, but it depends upon exactly&lt;br&gt;
what you mean by &quot;doable&quot;. How will you choose&lt;br&gt;
to interpolate non-monotonic data so that the&lt;br&gt;
result is monotone?&lt;br&gt;
&lt;br&gt;
Interpolation means that you will reproduce the&lt;br&gt;
data points exactly. So if your data is not monotone&lt;br&gt;
then an interpolant cannot possibly be monotonic.&lt;br&gt;
&lt;br&gt;
The best you can do is something like what pchip&lt;br&gt;
does. Pchip will not introduce new local extrema&lt;br&gt;
into the curve where the curve was monotone.&lt;br&gt;
It will be as monotone as is the data.&lt;br&gt;
&lt;br&gt;
Or, you can do as I did in my last post, where I&lt;br&gt;
showed how to use a constrained least squares&lt;br&gt;
to find the minimal change to your data points&lt;br&gt;
such that the result is monotone.&lt;br&gt;
&lt;br&gt;
These are the only two alternatives I'd suggest&lt;br&gt;
There are of course others, subtle variations on&lt;br&gt;
the above themes, but not much different.&lt;br&gt;
&lt;br&gt;
John</description>
    </item>
    <item>
      <pubDate>Thu, 30 Oct 2008 21:04:02 -0400</pubDate>
      <title>Re: interpolating/smoothing w/ monotonically increasing</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238438#608191</link>
      <author>Pete sherer</author>
      <description>Wow - Thank you so much John for solving the problem.  Amazing!!</description>
    </item>
    <item>
      <pubDate>Thu, 30 Oct 2008 21:57:01 -0400</pubDate>
      <title>Re: interpolating/smoothing w/ monotonically increasing</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238438#608204</link>
      <author>Bruno Luong</author>
      <description>You could use linprog if you prefer least-absolute or error-sum (linfity or l1) criteria instead of least-square deviation.&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Thu, 30 Oct 2008 23:12:02 -0400</pubDate>
      <title>Re: interpolating/smoothing w/ monotonically increasing</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238438#608215</link>
      <author>Per Sundqvist</author>
      <description>&quot;Pete sherer&quot; &amp;lt;tsh@abg.com&amp;gt; wrote in message &amp;lt;ged7g2$ki3$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Wow - Thank you so much John for solving the problem.  Amazing!!&lt;br&gt;
&amp;gt; &lt;br&gt;
.&lt;br&gt;
Hi, it looks that your data could be fitted to the monotonic functions atan or erf:&lt;br&gt;
.&lt;br&gt;
%&lt;br&gt;
fun=@(c,x) c(1)+c(2)*erf((x-c(3))/c(4))&lt;br&gt;
%fun=@(c,x) c(1)+c(2)*atan((x-c(3))/c(4))&lt;br&gt;
%&lt;br&gt;
options=optimset('TolX',1e-12,'TolFun',1e-12);&lt;br&gt;
cc0=[391.6603  223.1656    0.5792    0.2257];&lt;br&gt;
cc=lsqcurvefit(fun,cc0,[x(1)' x(3:end)'],[y(1)' y(3:end)'],...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[],[],options)&lt;br&gt;
cc0=cc;&lt;br&gt;
xx=linspace(min(x),max(x),200);&lt;br&gt;
yy=fun(cc,xx);&lt;br&gt;
plot(x,y,'o',xx,yy);&lt;br&gt;
%&lt;br&gt;
%note that I removed point 2&lt;br&gt;
/Per</description>
    </item>
    <item>
      <pubDate>Thu, 30 Oct 2008 23:26:02 -0400</pubDate>
      <title>Re: interpolating/smoothing w/ monotonically increasing</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238438#608217</link>
      <author>Bruno Luong</author>
      <description>&quot;Bruno Luong&quot; &amp;lt;b.luong@fogale.findmycountry&amp;gt; wrote in message &amp;lt;gedajd$mp$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; You could use linprog if you prefer least-absolute or error-sum (linfity or l1) criteria instead of least-square deviation.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Bruno&lt;br&gt;
&lt;br&gt;
Here is the monotonic fit results for three strategies (respectively raw data, L1, Linfty and L2).&lt;br&gt;
&lt;br&gt;
[ydata yl1 yinf yl2]&lt;br&gt;
&lt;br&gt;
ans =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;183.6992  183.6992  184.0256  183.7000&lt;br&gt;
&amp;nbsp;&amp;nbsp;347.6811  197.5389  273.2680  243.7500&lt;br&gt;
&amp;nbsp;&amp;nbsp;184.4308  197.5389  273.2680  243.7500&lt;br&gt;
&amp;nbsp;&amp;nbsp;199.1481  199.1481  275.0011  243.7500&lt;br&gt;
&amp;nbsp;&amp;nbsp;308.3963  308.3963  323.7812  308.4000&lt;br&gt;
&amp;nbsp;&amp;nbsp;453.1903  453.1903  449.2267  453.1900&lt;br&gt;
&amp;nbsp;&amp;nbsp;490.1048  490.1048  499.5431  490.1000&lt;br&gt;
&amp;nbsp;&amp;nbsp;558.3740  558.3740  561.7567  558.3700&lt;br&gt;
&amp;nbsp;&amp;nbsp;613.4362  613.4362  612.5059  613.4400&lt;br&gt;
&amp;nbsp;&amp;nbsp;623.4727  623.4727  647.3502  623.4700&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Bruno (who likes the l1)</description>
    </item>
    <item>
      <pubDate>Thu, 13 Aug 2009 10:01:03 -0400</pubDate>
      <title>Re: interpolating/smoothing w/ monotonically increasing</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238438#672744</link>
      <author>Bruno Luong</author>
      <description>An anonymous reader has solicited me to post the code of monotonic interpolation. As I do not own optimization toolbox I cannot check whereas LINPROG is correctly called and provides good result. But here we go. If you see any issue with linprog call please let me know.&lt;br&gt;
&lt;br&gt;
Bruno&lt;br&gt;
&lt;br&gt;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br&gt;
% testmonottonic.m&lt;br&gt;
% Solve the problem submitted in the following thread&lt;br&gt;
% &lt;a href=&quot;http://www.mathworks.com/matlabcentral/newsreader/view_thread/238438&quot;&gt;http://www.mathworks.com/matlabcentral/newsreader/view_thread/238438&lt;/a&gt;&lt;br&gt;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br&gt;
&lt;br&gt;
%% Data&lt;br&gt;
% Pete Shere's (OP) test data&lt;br&gt;
x=[0.1;0.2;0.3;0.4;0.5;0.6;0.7;0.8;0.9;1];&lt;br&gt;
&lt;br&gt;
y=[183.6992;347.6811;184.4308;...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;199.1481;308.3963;453.1903;...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;490.1048;558.374;613.4362;...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;623.4727];&lt;br&gt;
&lt;br&gt;
% L2 solution, using lsqlin, solved by John D'Errico&lt;br&gt;
yl2 = [183.7&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;243.75&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;243.75&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;243.75&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;308.4&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;453.19&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;490.1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;558.37&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;613.44&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;623.47 ];&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br&gt;
%% Engine starts here, basic reshape&lt;br&gt;
x = x(:); % Actually x does not play any role in the problem, it is simply&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% a correspondant abscissa to y data&lt;br&gt;
y = y(:);&lt;br&gt;
n = length(y);&lt;br&gt;
if length(y)~=n&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;error('x and y must have the same length');&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br&gt;
%% L1 fitting, BL's preference&lt;br&gt;
M = eye(n);&lt;br&gt;
&lt;br&gt;
% L1 system with slack variables u and v&lt;br&gt;
%   y(i) = yfit(i) + u(i) - v(i) for all i&lt;br&gt;
%   u&amp;gt;=0&lt;br&gt;
%   v&amp;gt;=0&lt;br&gt;
%   min sum(u+v) = sum|y-yfit|&lt;br&gt;
Aeq = [M eye(n,n) -eye(n,n)];&lt;br&gt;
beq = y(:);&lt;br&gt;
c = [zeros(1,size(M,2)) ones(1,2*n)].';&lt;br&gt;
%&lt;br&gt;
LB = [-inf(1,size(M,2)) zeros(1,2*n)].';&lt;br&gt;
% no upper bounds at all.&lt;br&gt;
UB = inf(size(LB)); % [];&lt;br&gt;
&lt;br&gt;
% Monotonic requirement&lt;br&gt;
A = diag(ones(1,n),0)-diag(ones(1,n-1),1);&lt;br&gt;
A(end,:) = [];&lt;br&gt;
A(end,3*n) = 0;&lt;br&gt;
b = zeros(n-1,1);&lt;br&gt;
&lt;br&gt;
% Solve LP&lt;br&gt;
% Minimizing (for x in R^n): f(x) = c'*x, subject to&lt;br&gt;
%       A*x &amp;lt;= b        (LE)&lt;br&gt;
%       Aeq*x = beq     (EQ)&lt;br&gt;
%       LB &amp;lt;= x &amp;lt;= UB   (BD).&lt;br&gt;
if isempty(which('linprog'))&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% Here is BL Linprog, call Matlab linprog instead to get &quot;SOL&quot;,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% the solution of the above LP problem&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mpsfile = 'L1_1DReg.mps';&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Contain = BuildMPS(A,b,Aeq,beq,c,LB,UB);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OK = SaveMPS(mpsfile,Contain);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if ~OK&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disp('Cannot write mps file');&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[OK outfile] = invokePCx(mpsfile);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if ~OK&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disp('Cannot invoke PCx');&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[OK sol] = readPCxoutput(outfile);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if ~OK&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disp('Cannot read PCx outfile');&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
else % Matlab linprog&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% Adjust optional LP options at your preference, see&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% &lt;a href=&quot;http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/linprog.html&quot;&gt;http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/linprog.html&lt;/a&gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% &lt;a href=&quot;http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/optimset.html&quot;&gt;http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/optimset.html&lt;/a&gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% BL has not checked because he does not have the right toolbox&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;LPoption = {};&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% LPoption = {optimset(...)}    &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;sol = linprog(c,A,b,Aeq,beq,LB,UB,[],LPoption{:});&lt;br&gt;
end&lt;br&gt;
%%% End of linprog solution&lt;br&gt;
&lt;br&gt;
% Discard slack variables&lt;br&gt;
sol = sol(1:size(M,2));&lt;br&gt;
yl1 = M*sol;&lt;br&gt;
&lt;br&gt;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br&gt;
%% Linfinity fitting&lt;br&gt;
M = eye(n);&lt;br&gt;
&lt;br&gt;
% Linfinity system with slack variables u and v&lt;br&gt;
% L1 system with slack variables u and v&lt;br&gt;
%   y(i)+u = yfit(i) OR&lt;br&gt;
%   y(i)-v = yfit(i)&lt;br&gt;
%   u&amp;gt;=0 and v&amp;gt;=0&lt;br&gt;
%   min u+v = max|y-yfit|&lt;br&gt;
LB = [-inf*ones(1,n) 0 0].';&lt;br&gt;
UB = inf(size(LB)); % [];&lt;br&gt;
A_LP=[+M -ones(n,1) zeros(n,1); ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;-M zeros(n,1) -ones(n,1)];&lt;br&gt;
b_LP=[+y;...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;-y];&lt;br&gt;
f = [zeros(1,n) 1 1].';&lt;br&gt;
&lt;br&gt;
% Monotonic requirement&lt;br&gt;
A = diag(ones(1,n),0)-diag(ones(1,n-1),1);&lt;br&gt;
A(end,:) = [];&lt;br&gt;
A(end,n+2)=0;&lt;br&gt;
b = zeros(n-1,1);&lt;br&gt;
&lt;br&gt;
AA = [A_LP; ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;A];&lt;br&gt;
bb = [b_LP; ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b];&lt;br&gt;
% Solve LP&lt;br&gt;
% Minimizing (for x in R^n): f(x) = f'*x, subject to&lt;br&gt;
%       AA*x &amp;lt;= bb        (LE)&lt;br&gt;
%       LB &amp;lt;= x &amp;lt;= UB   (BD).&lt;br&gt;
if isempty(which('linprog'))&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% Here is BL Linprog, call Matlab linprog instead to get &quot;SOL&quot;,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% the solution of the above LP problem    &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mpsfile='Linf_1DReg.mps';&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Contain=BuildMPS(AA,bb,[],[],f,LB,UB,'Linf_1DReg');&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OK=SaveMPS(mpsfile,Contain);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if ~OK&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disp('Cannot write mps file');&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[OK outfile]=invokePCx(mpsfile);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if ~OK&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disp('Cannot invoke PCx');&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;[OK solinf]=readPCxoutput(outfile);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if ~OK&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disp('Cannot read PCx outfile');&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
else % Matlab linprog&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% Adjust optional LP options at your preference, see &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% &lt;a href=&quot;http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/linprog.html&quot;&gt;http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/linprog.html&lt;/a&gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% &lt;a href=&quot;http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/optimset.html&quot;&gt;http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/optimset.html&lt;/a&gt;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% BL has not checked because he does not have the right toolbox&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;LPoption = {};&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;% LPoption = {optimset(...)}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;solinf = linprog(f,AA,bb,[],[],LB,UB,[],LPoption{:});&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
yinf=solinf(1:n);&lt;br&gt;
&lt;br&gt;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%&lt;br&gt;
%% Graphic output&lt;br&gt;
fig = figure(1); clf(fig);&lt;br&gt;
ax = axes('Parent',fig);&lt;br&gt;
plot(ax,x,y,'or',...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;x,yl1,'b',...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;x,yinf,'g', ...&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;x,yl2, 'c');&lt;br&gt;
legend(ax,'data','l_1','l_{inf}','l_2','Location','NorthWest');&lt;br&gt;
&lt;br&gt;
[y yl1 yinf yl2]&lt;br&gt;
%% End of the script</description>
    </item>
    <item>
      <pubDate>Thu, 13 Aug 2009 11:52:02 -0400</pubDate>
      <title>Re: interpolating/smoothing w/ monotonically increasing</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238438#672773</link>
      <author>John D'Errico</author>
      <description>&quot;Bruno Luong&quot; &amp;lt;b.luong@fogale.findmycountry&amp;gt; wrote in message &amp;lt;h60o8v$e66$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; An anonymous reader has solicited me to post the code of monotonic interpolation. As I do not own optimization toolbox I cannot check whereas LINPROG is correctly called and provides good result. But here we go. If you see any issue with linprog call please let me know.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Bruno&lt;br&gt;
&lt;br&gt;
There is an IMPORTANT caveat there.&lt;br&gt;
&lt;br&gt;
Bruno has written a piecewise linear approximant. This&lt;br&gt;
is NOT interpolation, because interpolation reproduces&lt;br&gt;
the data exactly. When your data is not monotone, then&lt;br&gt;
a monotone function cannot interpolate the data.&lt;br&gt;
&lt;br&gt;
You will see different results from a monotone cubic&lt;br&gt;
approximant too. See my SLM tools for both a linear&lt;br&gt;
and cubic approximant. (It looks like Bruno has used&lt;br&gt;
a solution that I posted some time ago, before I had&lt;br&gt;
chosen to post SLM.&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://www.mathworks.com/matlabcentral/fileexchange/24443&quot;&gt;http://www.mathworks.com/matlabcentral/fileexchange/24443&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
Note that the constraints for a monotone cubic are&lt;br&gt;
a bit nastier than just those one would employ for a&lt;br&gt;
monotone linear approximant. In fact, there are several&lt;br&gt;
ways one can implement a class of constraints for a&lt;br&gt;
monotone cubic. (Outside the scope of this discussion.)&lt;br&gt;
&lt;br&gt;
While in the past, I have offered an L1 or Linfinity solver&lt;br&gt;
when I've written a tool like SLM, I've rarely found it to&lt;br&gt;
be in demand. It is not difficult to add to SLM though&lt;br&gt;
if users wanted it.&lt;br&gt;
&lt;br&gt;
Now, why did Bruno see different results from the&lt;br&gt;
various choices of approximant used? &lt;br&gt;
&lt;br&gt;
An infinity norm solver will minimize the MAXIMUM&lt;br&gt;
residual. This is a terrible choice for problems where&lt;br&gt;
you have an outlier or outliers in the data, since all&lt;br&gt;
that matters is a reduction of the MAXIMUM residual.&lt;br&gt;
Worse, if you use an infinity norm solver to solve this&lt;br&gt;
problem, then it need not reproduce the data points&lt;br&gt;
even when the data is truly monotone! See that the&lt;br&gt;
L1 and L2 solvers will at least reproduce the data&lt;br&gt;
points in those parts of the curve where the data was&lt;br&gt;
monotone.&lt;br&gt;
&lt;br&gt;
At the other extreme is an L1 solver. Here the sum of&lt;br&gt;
absolute residuals is minimized. So a single outlier may&lt;br&gt;
often be ignored when it is inconsistent with the&lt;br&gt;
remainder of the data. There is no assurance that this&lt;br&gt;
will always yield a better result however. In fact, I can&lt;br&gt;
give an example where the L1 solver need not generate&lt;br&gt;
as good a solution as the L2 (lsqlin) solver.&lt;br&gt;
&lt;br&gt;
I'll argue that the best solution would use my SLM tools&lt;br&gt;
to yield a result that is both smooth and has all the&lt;br&gt;
properties that you know must exist in the underlying&lt;br&gt;
relationship.&lt;br&gt;
&lt;br&gt;
Again, I'll consider adding an L1 solver to SLM if&lt;br&gt;
there are requests. This is easy enough to do.&lt;br&gt;
&lt;br&gt;
John</description>
    </item>
    <item>
      <pubDate>Thu, 13 Aug 2009 12:22:01 -0400</pubDate>
      <title>Re: interpolating/smoothing w/ monotonically increasing</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238438#672787</link>
      <author>Bruno Luong</author>
      <description>&quot;John D'Errico&quot; &amp;lt;woodchips@rochester.rr.com&amp;gt; wrote in message &amp;lt;h60up2$dpn$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Bruno Luong&quot; &amp;lt;b.luong@fogale.findmycountry&amp;gt; wrote in message &amp;lt;h60o8v$e66$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; An anonymous reader has solicited me to post the code of monotonic interpolation. As I do not own optimization toolbox I cannot check whereas LINPROG is correctly called and provides good result. But here we go. If you see any issue with linprog call please let me know.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Bruno&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; There is an IMPORTANT caveat there.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Bruno has written a piecewise linear approximant. This&lt;br&gt;
&amp;gt; is NOT interpolation, because interpolation reproduces&lt;br&gt;
&amp;gt; the data exactly. When your data is not monotone, then&lt;br&gt;
&amp;gt; a monotone function cannot interpolate the data.&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Yes you are right, I should call it &quot;monotonic regression&quot;, &quot;interpolation when-you-can&quot;, or whatever the process that corrects the data &quot;as little as possible&quot; to make them monotonic.&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Thu, 13 Aug 2009 17:51:05 -0400</pubDate>
      <title>Re: interpolating/smoothing w/ monotonically increasing</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238438#672975</link>
      <author>Bruno Luong</author>
      <description>A remark that might be relevant: once the points are adjusted monotonically, a Hermit cublic interpolation (PCHIP) can be used for higher order interpolation properly. The monotonic will be prevailed.&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Thu, 13 Aug 2009 18:43:20 -0400</pubDate>
      <title>Re: interpolating/smoothing w/ monotonically increasing</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238438#672984</link>
      <author>John D'Errico</author>
      <description>&quot;Bruno Luong&quot; &amp;lt;b.luong@fogale.findmycountry&amp;gt; wrote in message &amp;lt;h61jq9$5hq$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; A remark that might be relevant: once the points are adjusted monotonically, a Hermit cublic interpolation (PCHIP) can be used for higher order interpolation properly. The monotonic will be prevailed.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Bruno&lt;br&gt;
&lt;br&gt;
To some extent this is true, at least if you will set the&lt;br&gt;
knots of the spline as the same as the data points.&lt;br&gt;
&lt;br&gt;
However it is arguably better to use a monotone cubic&lt;br&gt;
model in the first place. Even if the knots are still&lt;br&gt;
coincident with all of the data points, this will still&lt;br&gt;
result in a C2 cubic. Thus you will get a monotone&lt;br&gt;
approximant that both fits the data as well as&lt;br&gt;
possible and is C2. Since pchip is only C1, this may&lt;br&gt;
be an issue. And application of pchip to a general&lt;br&gt;
set of monotone points will not result in a C2 curve.&lt;br&gt;
It will still be only C1.&lt;br&gt;
&lt;br&gt;
I once did a minor survey, and I found that people&lt;br&gt;
could indeed see the difference between a C1 and a&lt;br&gt;
C2 cubic fit. Sometimes that difference is subtle, but&lt;br&gt;
it is very much visible.&lt;br&gt;
&lt;br&gt;
And it is not adequate to make the data points&lt;br&gt;
monotone and then to run a standard interpolating&lt;br&gt;
spline through them, since there is no way to&lt;br&gt;
assure that the spline will still be truly monotone.&lt;br&gt;
&lt;br&gt;
Finally, use of a tool like SLM allows you to build&lt;br&gt;
many other constraints into the curve fit. Must it&lt;br&gt;
pass through a specific point? Or perhaps it must&lt;br&gt;
have a zero slope at one end of the curve? You&lt;br&gt;
can also control the smoothness of the curve by a&lt;br&gt;
judicious choice of the knots with SLM. &lt;br&gt;
&lt;br&gt;
John</description>
    </item>
    <item>
      <pubDate>Fri, 14 Aug 2009 00:07:02 -0400</pubDate>
      <title>Re: interpolating/smoothing w/ monotonically increasing</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/238438#673084</link>
      <author>Pete sherer</author>
      <description>Hi John,&lt;br&gt;
&lt;br&gt;
Would it be possible if you can write out the functional form of the linear and cubic hermite polynomial.  I want to see how the slm.coef is used to evaluate the f(x).&lt;br&gt;
&lt;br&gt;
Thanks a lot in advanced,&lt;br&gt;
Pete</description>
    </item>
  </channel>
</rss>

