0

Need an algorithm to solve equation like these:

8*p^2+8*q^2+8*p-16=0
p^2+q^2+12*p+20=0

If anyone can point me to the name of algorithm also it'll be enough. I could have followed any matrix related algorithm but the quadratic inside these linear are causing a problem.

double-beep
  • 5,031
  • 17
  • 33
  • 41

3 Answers3

1

In the case you wrote about, I'd suggest first subtracting 8 times the second equation from the first.

0 = (8*p^2+8*q^2+8*p-16) - 8*(p^2+q^2+12*p+20) = -88*p-176 = 0
p = -2

Then you are left with a simple quadratic equation in q, which you can solve using the common methods for solving quadratic equations in a single variable.

MvG
  • 57,380
  • 22
  • 148
  • 276
  • +1 - a much more rational solution. Why didn't I see it? I'd accept this one. – duffymo Dec 16 '12 at 20:02
  • any algorithm to do the same procedure? i mean how to apply programmatically? –  Dec 17 '12 at 06:26
  • @adi928, take the two coefficients of p^2 and q^2 in either euqation. As long as this 2x2 matrix has two dependent rows, i.e. rank 1, i.e. determinant 0, you can simply cross-multiply them (i.e. second equation multiplied by coefficient of p^2 in first eq. and vice versa) to cancel the quadratic terms. If there is no such linear dependency, this solution method won't apply, and you are back to solutions e.g. like the one provided by duffymo. – MvG Dec 17 '12 at 17:49
1

The Nelder-Mead Algorithm is a very easy-to-program non-linear solver that converges quickly and accurately in low dimensional problems like this one. The pseudo-code is easy to follow on Wikipedia, but you can find some coded implementations online as well.

Your cost function in this case would be the sum of the left hand sides' squares to ensure that lower costs are closer to the correct solutions.

KevinH
  • 413
  • 2
  • 7
0

It's a non-linear problem. You'll need an iterative solution, something like Newton Raphson or BFGS.

Here's a simple approach.

Start by linearizing it:

16*p*dp + 16*q*dq + 8 = 0
2*p*dp + 2*q*dq + 12 = 0

This becomes a linear algebra problem:

| 16*p 16*q |[ dp ]    [  -8 ]
|  2*p  2*q |[ dq ] =  [ -12 ]

You'll start with an initial guess for (p, q):

(p, q) = (p0, q0)

You'll solve for dp and dq, using the usual linear algebra techniques.

You'll update your guesses for p and q:

(p1, q1) = (p0 + dp, q0 + dq)

You'll iterate again until your solution either diverges or converges.

There's no guarantee that it'll converge, depending on your initial guess.

duffymo
  • 305,152
  • 44
  • 369
  • 561