MATLAB: Non-linear piecewise fit using fittype() and fit().

curve fittingnon-linearpiecewise

Hello, I'm trying to fit some data to a rather complicated non-linear model:
y=(1-(x/R)^2)^n
where x is the independent variable with R and n constant. I need to fit this model to my data to find R and n. The problem I have is that this function returns complex values whenever n<1 and x>R. Thus I would like to make it a piecewise function so that c=0 when x>R. I can't figure out how to construct such a piecewise fit object using fittype(). Here's my code thus far for doing the fit:
ft = fittype( '(1-(x/R)^2)^n', 'independent', 'x', 'dependent', 'y' );
[fitresult, gof] = fit( xData, yData, ft)
Any ideas on how to do a non-linear piecewise fit? Thanks in advance for any insight.
Justin

Best Answer

How about replacing
(x/R)^2
by
min(1,(x/R)^2)