<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/262969</link>
    <title>MATLAB Central Newsreader - Curve fitting</title>
    <description>Feed for thread: Curve fitting</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>Tue, 13 Oct 2009 05:15:19 -0400</pubDate>
      <title>Curve fitting</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/262969#686594</link>
      <author>Mohammad Monfared</author>
      <description>Hi,&lt;br&gt;
I'd like to fit a function, f(x), to my data (x,y). 'f' has three parameter to be determined p1, p2, p3, and is of form:  &lt;br&gt;
&lt;br&gt;
f_1(x)          0 &amp;lt;= x &amp;gt;= p1  ;&lt;br&gt;
f_2(x)          p1 &amp;lt; x  ;&lt;br&gt;
&lt;br&gt;
How can I do this job while have two equation for f(x) ?&lt;br&gt;
( I have access to the Optimization toolbox)&lt;br&gt;
&lt;br&gt;
thanks a lot,&lt;br&gt;
Mohammad,</description>
    </item>
    <item>
      <pubDate>Wed, 14 Oct 2009 11:35:19 -0400</pubDate>
      <title>Re: Curve fitting</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/262969#686911</link>
      <author>Paul Kerr-Delworth</author>
      <description>Hi Mohammad, &lt;br&gt;
&lt;br&gt;
It is possible to fit piecewise functions to a set of data using either lsqcurvefit from the Optimization Toolbox. &lt;br&gt;
&lt;br&gt;
Firstly, for the example you state, I think you may have your inequalities mis-typed. I think your equation should be&lt;br&gt;
&lt;br&gt;
if 0&amp;lt;= x &amp;lt;= p1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;f(x) = f_1(x, p1, p2, p3)&lt;br&gt;
else&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;f(x) = f_2(x, p1, p2, p3)&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
Now, say you have a MATLAB function, called myFunction which evaluates the equation above&lt;br&gt;
&lt;br&gt;
function y = myFunction(x, p1, p2, p3)&lt;br&gt;
&lt;br&gt;
if 0&amp;lt;= x &amp;lt;= p1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;y = f_1(x, p1, p2, p3)&lt;br&gt;
else&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;y = f_2(x, p1, p2, p3)&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
To fit p1, p2, p3 for this equation using the data (xdata, ydata), you can use lsqcurvefit. First, you need to write an objective function for lsqcurvefit. This function must be able to evaluate your equation at each of the data points, xdata.&lt;br&gt;
&lt;br&gt;
function y = objfun(p, xdata)&lt;br&gt;
&lt;br&gt;
% Assume each row of xdata is a point.&lt;br&gt;
numDataPoints = size(xdata, 1);&lt;br&gt;
y = zeros(numDataPoints, 1);&lt;br&gt;
&lt;br&gt;
% Evaluate myFunction at each point in xdata. Note, there are more efficient ways to do this, just using a for loop for clarity.&lt;br&gt;
for i = 1:numDataPoints&lt;br&gt;
&amp;nbsp;&amp;nbsp;y(i) = myFunction(xdata(i, :), p(1), p(2), p(3));&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
Now call lsqcurvefit:&lt;br&gt;
&lt;br&gt;
estP = lsqcurvefit(@objfun, p0, xdata, ydata);&lt;br&gt;
&lt;br&gt;
where, p0 is a 1-by-3 vector containing an initial guess for the parameter values p(1), p(2) and p(3). The fitted parameters are returned in the variable estP.&lt;br&gt;
&lt;br&gt;
As a cautionary note, fitting piecewise equations using least squares techniques can be tricky. In your case, a good initial guess for p(1) will be very useful.&lt;br&gt;
&lt;br&gt;
To get more help on LSQCURVEFIT, see the Optimization Toolbox documentation. &lt;br&gt;
&lt;br&gt;
Hope this helps.&lt;br&gt;
&lt;br&gt;
Cheers, &lt;br&gt;
&lt;br&gt;
Paul&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&quot;Mohammad Monfared&quot; &amp;lt;gohardoust@gmail.com&amp;gt; wrote in message &amp;lt;hb12d7$jrp$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt; I'd like to fit a function, f(x), to my data (x,y). 'f' has three parameter to be determined p1, p2, p3, and is of form:  &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; f_1(x)          0 &amp;lt;= x &amp;gt;= p1  ;&lt;br&gt;
&amp;gt; f_2(x)          p1 &amp;lt; x  ;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; How can I do this job while have two equation for f(x) ?&lt;br&gt;
&amp;gt; ( I have access to the Optimization toolbox)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; thanks a lot,&lt;br&gt;
&amp;gt; Mohammad,</description>
    </item>
  </channel>
</rss>

