# How would I write iterative code to try multiple coefficients for an equation?

So, I'm trying to solve a class of equations, and get all the solutions in a list. The general form looks like this:

c == a*Sqrt[1-n] PlusMinus[b * Sqrt[n]]


Please forgive if that's wrong, I'm really new to Mathematica.

The twist to this? c, a, and b all need to iterate separately through the integer values from -3 to 3. I need to get all solutions of n, positive and negative, for all combinations of these integer values. How would I write code to do that iteration for me, and get all the possible values of n in a list as output?

EDIT: I tried this in response to that first answer after reading Solve pretty carefully. I know this is horrendously wrong- what, though?

ClearAll[a, b, c, domainstate, domainstatea, domainstateb, \
domainstatec]
domainstatea = a \[Element] Integers && a >= -3 && a <= 3
domainstateb = b \[Element] Integers && b >= -3 && b <= 3
domainstatec = c \[Element] Integers && c >= -3 && c <= 3
domainstate = domainstatea && domainstateb && domainstatec
Solve[a*Sqrt[1 - n] \[PlusMinus] b * Sqrt[n] == c && domainstate, n]

• The code is, indeed not right, since the PlusMinus is only for representation, not for calculations. Your problem looks trivial, but without a correct equation there is no sense doing anything. Anyway, check Menu/Help/WolframDocumentation/Solve. Commented Sep 28, 2015 at 14:06
• Hey, I updated it, Would you mind taking another look? Thanks! Commented Sep 28, 2015 at 14:16
• Now I'm getting this error that it isn't solvable with the methods available to solve? I know for a fact that there are solutions to this in this space. Commented Sep 28, 2015 at 14:19
• I guess that you mean c == a*Sqrt[1-n]+b * Sqrt[n]  and c == a*Sqrt[1-n]-b * Sqrt[n] . Is it right? Or you mean a multiplication like a*Sqrt[1-n] *b * Sqrt[n]  ?? Commented Sep 28, 2015 at 14:27
• It was the first one. Commented Sep 28, 2015 at 17:40

Here is the solution of a possible interpretation of your question.

Clear[a, b, c, n, sol]


First solve the equation with unspecified parameters a, b, and c: (The plus minus in the OP sign is incorporated in the parameter b):

sol = Solve[c == a Sqrt[1 - n] + b Sqrt[n], n]

(*
Out[919]= {{n -> (a^4 + a^2 b^2 - a^2 c^2 + b^2 c^2 -
2 Sqrt[a^4 b^2 c^2 + a^2 b^4 c^2 - a^2 b^2 c^4])/(
a^4 + 2 a^2 b^2 + b^4)}, {n -> (
a^4 + a^2 b^2 - a^2 c^2 + b^2 c^2 +
2 Sqrt[a^4 b^2 c^2 + a^2 b^4 c^2 - a^2 b^2 c^4])/(a^4 + 2 a^2 b^2 + b^4)}}
*)


There are two solutions n1 and n2.

The we calculate the solutions when each of the parameters take the integer values from -3 to +3.

The format is {{a,b,c},{n1,n2}}

Flatten[Table[{{a, b, c}, n /. sol}, {a, -3, 3}, {b, -3, 3}, {c, -3, 3}] //
Quiet, 2]
(* we shall show only a few lines *)

(*
{
{{-3, -3, -3}, {0, 1}},
{{-3, -3, -2}, {1/324 (162 - 36 Sqrt[14]), 1/324 (162 + 36 Sqrt[14])}},
{{-3, -3, -1}, {1/324 (162 - 18 Sqrt[17]), 1/324 (162 + 18 Sqrt[17])}},
{{-3, -3,  0}, {1/2, 1/2}}, {{-3, -3, 1},
{1/324 (162 - 18 Sqrt[17]), 1/324 (162 + 18 Sqrt[17])}},
{{-3, -3, 2}, {1/324 (162 - 36 Sqrt[14]), 1/324 (162 + 36 Sqrt[14])}},
{{-3, -3, 3}, {0, 1}},
{{-3, -2, -3}, {0, 144/169}},
{{-3, -2, -2}, {25/169, 1}},
{{-3, -2, -1}, {1/169 (112 - 24 Sqrt[3]), 1/169 (112 + 24 Sqrt[3])}},
{{-3, -2, 0}, {9/13, 9/13}},
{{-3, -2, 1}, {1/169 (112 - 24 Sqrt[3]), 1/169 (112 + 24 Sqrt[3])}},
...
}
*)

Off[Solve::ifun]

max = 3;

Join[{{"a", "b", "c", "sign", "n"}},
Table[{a, b, c, sign, Solve[c == a*Sqrt[1 - n] + sign*b*Sqrt[n], n]},
{a, -max, max}, {b, -max, max}, {c, -max, max}, {sign, {-1, 1}}] //
Flatten[#, 3] &] // Grid[#, Frame -> All] &


< deleted section >

To get Table sorted by largest absolute value of n

Join[{{"a", "b", "c", "sign", "n", "N[|n|]"}},
SortBy[
Select[ (* delete cases without solution *)
Table[
{a, b, c, sign,
sol = n /. Solve[
c == a*Sqrt[1 - n] + sign*b*Sqrt[n], n],
Abs[sol] // Union // N},
{a, -max, max},
{b, -max, max},
{c, -max, max},
{sign, {-1, 1}}] // Flatten[#, 3] &,
FreeQ[#, n] &],
Max[Abs[#[[-1]]]] &] // Reverse] //
Grid[#, Frame -> All] &

• @ Bob Hanlon: nice table, Bob. Commented Sep 28, 2015 at 14:30
• Gorgeous! This is exactly what I need. Is there a way I could get exact forms and absolute values of all the solutions and sort them in numerical order? Commented Sep 28, 2015 at 14:32
Flatten@(Solve[#, n] & /@
Flatten@Table[{a Sqrt[1 - n] + b*Sqrt[n] == c,
a Sqrt[1 - n] - b Sqrt[n] == c},
{a, -3, 3},
{b, -3, 3},
{c, -3, 3}]) // Quiet
`