From: Christopher Deen on
I have a set of test data (from material stress rupture tests) which when plotted gives a scatter graph with a strong positive correlation. It is quite obvious that there is a line of best fit, which is easily found using linear regression, for examply using the R2 value in Excel. However, in actual fact, 2 lines of best fit would be more accurate, as this would incorporate a yield point.

My question is this: How do I write a MatLab program that examines all the data points, and tests all of them, then instead of simply plotting one line of best fit, it plots 2. Essentialy 1 line with a sudden change in gradient. This means the 2 lines of best fit must connect at some value, and combined the values of divergance must be at an overall minimum.

I have some reasonable skill with MatLab, but I'm not even sure if I got my point accross, never mind starting it myself. I will try and sketch something up in paint or such which represents my point more accurately.

Thanks

Chris
From: John D'Errico on
"Christopher Deen" <deeny34(a)hotmail.com> wrote in message <i1k642$38v$1(a)fred.mathworks.com>...
> I have a set of test data (from material stress rupture tests) which when plotted gives a scatter graph with a strong positive correlation. It is quite obvious that there is a line of best fit, which is easily found using linear regression, for examply using the R2 value in Excel. However, in actual fact, 2 lines of best fit would be more accurate, as this would incorporate a yield point.
>
> My question is this: How do I write a MatLab program that examines all the data points, and tests all of them, then instead of simply plotting one line of best fit, it plots 2. Essentialy 1 line with a sudden change in gradient. This means the 2 lines of best fit must connect at some value, and combined the values of divergance must be at an overall minimum.
>
> I have some reasonable skill with MatLab, but I'm not even sure if I got my point accross, never mind starting it myself. I will try and sketch something up in paint or such which represents my point more accurately.
>

I would describe this as a least squares (linear) spline,
with one free internal knot or break point, thus two
segments in the curve.

You can fit it easily enough using my SLM tools, found
here:

http://www.mathworks.com/matlabcentral/fileexchange/24443

That will require the optimization toolbox, but it allows
you to do many other things too. However, without
that TB, you can use my fminspleas to do the estimation.

http://www.mathworks.com/matlabcentral/fileexchange/10093-fminspleas

Even simpler, but with a bit more effort, you can
actually use fminbnd for the estimation since there is
only one nonlinear parameter of interest in this problem.
(That parameter is the location of the break point.)

John
From: Matt J on

The fminbnd approach was actually pretty easy...


function yfit=FittingRoutine(x,y)
%input data is x,y
%ouput yit is the fit to y with 2 linear pieces.

yieldPt=fminbnd(@cost,x(1),x(end));

[R,yfit]=cost(yieldPt);

function [R,yfit]=cost(yield)

lo=(x<=yield);
hi=~lo;

[plo,slo]=polyfit(x(lo),y(lo),1);
[phi,shi]=polyfit(x(hi),y(hi),1);

R=slo.normr+shi.normr;

if nargout>1,
yfit=[polyval(plo,x(lo)), polyval(phi,x(hi))];
end

end

end