Thread Subject: Curve fitting

Subject: Curve fitting

From: Mohammad Monfared

Date: 13 Oct, 2009 05:15:19

Message: 1 of 2

Hi,
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:

f_1(x) 0 <= x >= p1 ;
f_2(x) p1 < x ;

How can I do this job while have two equation for f(x) ?
( I have access to the Optimization toolbox)

thanks a lot,
Mohammad,

Subject: Curve fitting

From: Paul Kerr-Delworth

Date: 14 Oct, 2009 11:35:19

Message: 2 of 2

Hi Mohammad,

It is possible to fit piecewise functions to a set of data using either lsqcurvefit from the Optimization Toolbox.

Firstly, for the example you state, I think you may have your inequalities mis-typed. I think your equation should be

if 0<= x <= p1
   f(x) = f_1(x, p1, p2, p3)
else
   f(x) = f_2(x, p1, p2, p3)
end

Now, say you have a MATLAB function, called myFunction which evaluates the equation above

function y = myFunction(x, p1, p2, p3)

if 0<= x <= p1
   y = f_1(x, p1, p2, p3)
else
   y = f_2(x, p1, p2, p3)
end

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.

function y = objfun(p, xdata)

% Assume each row of xdata is a point.
numDataPoints = size(xdata, 1);
y = zeros(numDataPoints, 1);

% Evaluate myFunction at each point in xdata. Note, there are more efficient ways to do this, just using a for loop for clarity.
for i = 1:numDataPoints
  y(i) = myFunction(xdata(i, :), p(1), p(2), p(3));
end

Now call lsqcurvefit:

estP = lsqcurvefit(@objfun, p0, xdata, ydata);

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.

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.

To get more help on LSQCURVEFIT, see the Optimization Toolbox documentation.

Hope this helps.

Cheers,

Paul


"Mohammad Monfared" <gohardoust@gmail.com> wrote in message <hb12d7$jrp$1@fred.mathworks.com>...
> Hi,
> 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:
>
> f_1(x) 0 <= x >= p1 ;
> f_2(x) p1 < x ;
>
> How can I do this job while have two equation for f(x) ?
> ( I have access to the Optimization toolbox)
>
> thanks a lot,
> Mohammad,

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com