Re: solution to nonlinear eq
- From: "John D'Errico" <woodchips@xxxxxxxxxxxxxxxx>
- Date: Fri, 25 Jul 2008 18:50:21 +0000 (UTC)
"Roger Stafford" <ellieandrogerxyzzy@xxxxxxxxxxxxxxxxxxxxxx> wrote in
message <g6d4oe$6he$1@xxxxxxxxxxxxxxxxxx>...
"Andrew Palmer" <apalmer28@xxxxxxxxxxxxxx> wrote in message<g6c35q
$mi1$1@xxxxxxxxxxxxxxxxxx>...have
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
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
.
- Follow-Ups:
- Re: solution to nonlinear eq
- From: Roger Stafford
- Re: solution to nonlinear eq
- References:
- solution to nonlinear eq
- From: Andrew Palmer
- Re: solution to nonlinear eq
- From: Roger Stafford
- solution to nonlinear eq
- Prev by Date: Printing Plots in PDF format
- Next by Date: Re: Single precision random
- Previous by thread: Re: solution to nonlinear eq
- Next by thread: Re: solution to nonlinear eq
- Index(es):
Relevant Pages
|