Re: solution to nonlinear eq



"Roger Stafford" <ellieandrogerxyzzy@xxxxxxxxxxxxxxxxxxxxxx> wrote in
message <g6d4oe$6he$1@xxxxxxxxxxxxxxxxxx>...
"Andrew Palmer" <apalmer28@xxxxxxxxxxxxxx> wrote in message
<g6c35q
$mi1$1@xxxxxxxxxxxxxxxxxx>...
Given an equation of the form:

A = b(1+b) + g(1+g)

where; A, b and g are restricted to integers
A is known, b and g are unknown

What is the best method to determine the possible values
for b and g?

Thanks,

The following finds all possible non-negative integer solutions. (Any
negative solutions can be determined from these in a trivial manner.) I
have
not been able to think of any more efficient method.

function [B,G] = andrew(A)
b = []; g = [];
a = A/2;
if round(a)~=a | A<0, return, end
for b = 0:floor((sqrt(1+4*a)-1)/2)
a = a-b;
g = (sqrt(1+8*a)-1)/2;
if round(g)==g
B = [B;b]; G = [G;g];
end
end
return

Arrays B and G will have all possible non-negative b and g solution pairs, if
any, (other than reversing their order.)

I have assumed here that the 'sqrt' function gives an exact answer for the
square root of any integer squared. Otherwise the two 'sqrt' calls above
would have to be amended to allow a small tolerance for rounding errors.

Why a for loop? Vectorize it. For example,

A = 42;

b = 0:(sqrt(A/2)+1);
r = b.*(1+b) - A;
g = (sqrt(1 - 4*r) - 1)/2;
k = (g == round(g));
bg = unique(sort([b(k);g(k)]',2),'rows')

bg =
0 6
3 5

Finding both distinct solutions as the rows
of bg.

John
.



Relevant Pages

  • Re: solution to nonlinear eq
    ... b and g are unknown ... The following finds all possible non-negative integer solutions. ... I have assumed here that the 'sqrt' function gives an exact answer for the ... square root of any integer squared. ...
    (comp.soft-sys.matlab)
  • Re: Forth PARANOIA
    ... Addition/Subtraction neither rounds nor chops. ... Test for sqrt monotonicity. ... "Square root is neither chopped nor correctly rounded", ...
    (comp.lang.forth)
  • Re: Riemann surface
    ... function is the projection taking the x value. ... So, the sqrt function *is* ... globally defined analytic square root on C or on C without the origin. ... instead of just Z the complex plane that represents the y-component only. ...
    (sci.math)
  • Re: read string to float
    ... It can represent square root of 2 and any other real number. ... doesn't use (sqrt 2) as representation though. ... digit, untested) ... That's something like that reallib does. ...
    (comp.lang.lisp)
  • Re: the number of divisors, number theory
    ... sqrt is the square root of n. ... is always smaller than 2 time the sqrt of n). ... and that tau is a multiplicative function. ...
    (sci.math)