From: Alexandra on
I want to fit two functions to two data sets
y1 = ax^2 + bx + c = fit for data set 1
y2 = dx^2 + ex + f = fit for data set 2
I know that the curves must intersect at two points given by
y1=y2 at [x=A,y1]
y1=y2 at [x=B,y2]
I would like to calculate the intersection points from the data also but if this is not possible I can estimate them.
Using this information, how do I find the values for a,b,c,d,e,f?
From: Matt J on


Apply polyfit() to each data set.
From: John D'Errico on
"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <hodcmk$fqj$1(a)fred.mathworks.com>...
>
>
> Apply polyfit() to each data set.

NO. This is not what was asked. The curves had specific
requirements.
From: John D'Errico on
"Alexandra " <klanga_uk(a)yahoo.co.uk> wrote in message <hodaql$c8a$1(a)fred.mathworks.com>...
> I want to fit two functions to two data sets
> y1 = ax^2 + bx + c = fit for data set 1
> y2 = dx^2 + ex + f = fit for data set 2
> I know that the curves must intersect at two points given by
> y1=y2 at [x=A,y1]
> y1=y2 at [x=B,y2]
> I would like to calculate the intersection points from the data also but if this is not possible I can estimate them.
> Using this information, how do I find the values for a,b,c,d,e,f?

You have two quadratic polynomials, with unknown
coefficients. You do have data points that can be
used to fit the curves, but an additional piece of
information is that the curves MUST cross at two
specific locations in x.

If you do not know the locations of the intersection
points, then you might choose to use polyfit on each
curve independently, then compute the intersections.
You would do that by applying roots to the difference
of the two functions. Thus

roots(y1 - y2)

will yield two locations. They are the locations in x
where the curves cross.

If you decide that these locations are in fact already
known, then the estimation process is different.

Thus, at x = A, y1(A) = y2(A), and at x = B, we
have y1(B) = y2(B). The function values at those
points are not known here, only that the curves must
cross at those points. This problem is solved using
a linear least squares with equality constraints. lsqlin
from the optimization toolbox will do it, or if you do
not have that TB, then download lse from the FEX.

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

Assume that we have curve 1 defined at a set of points
with (x1,y1), where x1 and y1 are vectors. We also
have (x2,y1), vectors that contain data for the second
curve. Then estimate the coefficients as:

x1 = x1(:);
x2 = x2(:);
n1 = numel(x1);
n2 = numel(x2);
M = [[x1.^2, x2, ones(n1,1),zeros(n1,3)]; ...
[zeros(n2,3),x2.^2, x2, ones(n2,1)]];

Aeq= [A.^2,A,1,-A.^2,-A,-1;B.^2,B,1,-B.^2,-B,-1];
coef = lsqlin(M,[y1(:);y2(:)],[],[],Aeq,[0;0]);
p1 = coef(1:3);
p2 = coef(4:6);

HTH,
John
From: Matt J on
"John D'Errico" <woodchips(a)rochester.rr.com> wrote in message <hodets$n80$1(a)fred.mathworks.com>...
> "Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <hodcmk$fqj$1(a)fred.mathworks.com>...
> >
> >
> > Apply polyfit() to each data set.
>
> NO. This is not what was asked. The curves had specific
> requirements.
===============

They had requirements, but not terribly specific ones. Without knowing from the OP what a priori info there is on the locations of the intersections, doing polyfit and deriving an intersection post facto is about as good a solution as can be suggested.

If we had, as a priori info, even a conservative lower bound on the distance between the intersection points, an alternative would be to do a least squares fit subject to the requirement that the discrimimant of the difference polynomial is >0. This would ensure that the curves have two intersection points without knowing exactly where.

Namely, we pose the problem as follows

min.

(y1 - (ax^2 + bx + c))^2 + (y2 - (dx^2 + ex + f))^2

subject to

Discriminant=(b-e)^2-4*(a-d)*(c-f)>=a^2*MinDist^2

where MinDist>0 would be the a priori lower bound on the separation distance between the intersection points.

This is a more complicated quadratic constraint, but since there's only the one, should be doable by applying Lagrange multiplier methods.