Unfortunately I was not able to find a crisp and short solution here.
Sometimes it is helpful to know a bit more about the background of a
problem; sometimes there are problem-specific constraints that can
simplify things. On the other hand, if you just need a formula to plug
into a computer program, then what's below should help you.
If you want I can send you some Java code that computes what I'm
doing, in the form of a big function that takes all the constants as
parameters. For simplicity though, the Java code will probably assume
that
solutions are real. But what's below might be clearer:
Given the two equations below:
(1) c1 = 0.5*c2*[A*(x-D)^2+B*(y-D)^2] + c3/c2*D*[A(x-D)+B(y-D)]
and
(2) A(x-D) = B(y-D)
We assume that c2 is nonzero, since it's a denominator in the
equation.
We divide up the solution into 4 cases, depending on which of A and B
are zero.
We have to do this to make sure we don't divide by zero anywhere.
Case 1: A is nonzero and B is nonzero.
Let
(3) z=A(x-D)
Hence we have,
(3') z=B(y-D)
Then A*(x-D)^2 = A*(x-D)*(x-D) = z*(x-D) = z*z/A=z^2/A.
Similarly, B*(y-D)^2= z^2/B.
Hence, equation (1) becomes:
c1=0.5*c2 * (1/A+1/B))*z^2+c3/c2*D*2*z
Let
(3) a=.5*c2* (1/A+1/B)
and let
(4) b=c3/c2*D*2
Then our equation becomes:
a*z^2 + b*z - c1 = 0
Case 1.1: A is not equal to -B
In this case, a is not equal to 0, so we can write:
(5) z= (-b + sqrt(b^2 + 4a(c1)))/2a
(5') z= (-b - sqrt(b^2 + 4a(c1)))/2a
Now,
x=z/A+D
y=z/B+D
Now you can just compute
(6) x+y=((1/A+1/B)*z + 2*D)
I am not actually writing out x+y in one big equation here, since for
most cases, including input to a computer program, it's easier and
safer to split up the equation using the temporary terms z, a, b and
so on.
Note that there may be two distinct possibilities for z, and thus in
general for x+y, unless 4a(c1)=-b^2, in which case there is only one
solution to the quadratic.
Note that if 4a(c1) is smaller than -b^2 then both solutions will be
complex nonreal. (You hadn't specified whether you want to limit to
real solutions; if so, consider this case as "no solution").
Case 1.2: A=-B
Here, a is zero and we have the linear equation:
b*z - c1 = 0
This has solutions, by inspection, as follows:
Case 1.2.1 If b is 0 and c1 is 0 all z are solutions
Case 1.2.2 If b is 0 and c1 is nonzero, there are no
solutions.
Case 1.2.3 If b is nonzero, then z=(c1)/b .
Just as in case 1.1, you can recover x+y from the value of z using
equation (6).
Case 2: A is nonzero and B is zero
In this case, we must have x=D by equation (2). By inspection of
equation 1,
if c1 is 0 then solutions are x=D and all y; if c1 is nonzero, then
there are no solutions.
Case 3: A is zero and B is nonzero
Similar to case 2, except solutions are y=D if c1 is 0, and no
solutions otherwise.
Case 4: A is zero and B is zero
Here, if c1 is zero, all x,y are solutions; otherwise there are no
solutions. |