Alternatively, if similar behavior is always expected, you could fit a piecewise linear model to each curve and compare the slopes of the second segment to the first to see which is the more pronounced change. If it's known the two curves always have same relative shape then probably that breakpoint for the one is going to be pretty good estimate. I posted code some months ago demonstrating solving the breakpoint altho I don't recall the question subect...
OK, here's the link--search did work one only second try to think of what used as catchword...
ADDENDUM
Seems to work reasonably well...I first plotted
x=1:length(d); x=x(:);
sx=std(x); mx=mean(x);
xpr=(x-mx)/sx;
figure
plotyy(x,d(1:2:end),x,d(2:2:end))
and concluded are similar enough to only deal with one specifically as representative of all.
So,
>> b=nlinfit(xpr,d(:,1),@piecewise,[-1 0 0 5])
b =
-0.1447 0.6965 -0.0294 6.5596
>> yh=piecewise(b,xpr);
>> close
>> plot(d(:,1),'x')
>> hold on
>> plot(yh,'r')
Looks pretty good estimate of where the breakpoint would/should be...so what is it?
>> xBreak=b(3)*sx+mx
xBreak =
48.6640
The piecewise regression function is
>> type piecewise
function y=piecewise(b,x)
ix=x>b(3);
y(ix)=[b(1) + b(3)*(b(2)-b(4))+b(4)*x(ix)];
y(~ix)=[b(1)+b(2)*x(~ix)];
y=y(:);
>>
Given the characteristic shapes are so similar, the "divergence" location is pretty much the location at which the two pieces of the first curve change characteristics. This gives at least a reasonable estimate of where that breakpoint is.
It appears that the second portion is nearly quadratic; I've nor worked on the algebra to see about actually implementing that; you need another condition to tie the coefficients together as above where solve for a2 in terms of a1 and C by equating the two pieces at the intersection point, ie,
a1+b1*C = a2+b2*C --> a2 = a1 + (b1-b2)*C
One possible refinement might be to use the above for an initial estimate the fit the quadratic from floor(xBreak:end) and then look for where that residual peaks to the left of that point. This would rely on the data being quadratic as nearly as seems to be in the sample data, probably.
I just stuck in the actual value...
>> bq=polyfit(x(48:end)',d(48:end,1),2)
bq =
-0.0025 0.6033 -24.5031
>> yhq=polyval(bq,x(48:end)');
>> hold on
>> plot(x(48:end)',yhq,'r')
>> plot(x(48:end)',yhq-d(48:end,1),'g')
>>
Best Answer