MATLAB: How to fit a piecewise nonlinear function to the data

nonlinear fitpiecewise fit

I have eyeposition data from a saccade and I would like to fit a piecewise function that has a linear fit before the onset of the saccade and an exponential fit after. I can clearly define the onset. How do I best do this? I was told to use the nlinfit function for that, but I'm not sure how to define the inputs well.

Best Answer

I wouldn't recommend fitting a piecewise model to the complete data set. Rather, divide the data into a linear and an exponential part and fit each one separately. You can use polyfit() to fit the linear part. For the exponential part, you could use nlinfit as long as there aren't any bound constraints that you would like to put on the exponential parameters. If you wish to apply bounds, I would recommend either fit() or lsqcurvefit(), depending on which Toolboxes you have.
Related Question