1
$\begingroup$

Assume we want to determine the coefficients (integer) of a quadratic equation by having some information about the input and output values. If I enter the input and output individually before the command Solve, it gives me the correct answer, but I want to give the sequence of out put inside the Solve and get some perhaps different coefficients, it does not work. I give an example:

Clear[a, b, c, x, y, z, d1, d2, d3]

x = 1; d1 = 44; y = 2; d2 = 158; z = 4; d3 = 596;

Solve[{a x^2 + b x + c == d1 && a y^2 + b y + c == d2 && 
a z^2 + b z + c == d3}, {a, b, c}, Integers] // Column

the result is {a -> 35, b -> 9, c -> 0}

Clear[a, b, c, x, y, z, d1, d2, d3]

x = 1; d1 = 37; y = 2; d2 = 132; z = 4; d3 = 568;

Solve[{a x^2 + b x + c == d1 && a y^2 + b y + c == d2 && 
a z^2 + b z + c == d3}, {a, b, c}, Integers] // Column

The result is {a -> 41, b -> -28, c -> 24}.

Now I want to get these two or perhaps more answers with entering a sequence of "d1,d2,d3" ($d1\in \{37, 44\}$, $d2\in \{132, 144, 154, 156, 158\}$, $d3\in \{497, 568, 588, 595, 596\}$) inside the Solve but it does not work. Could you please let me know where is the error:

Clear[a, b, c, x, y, z, d1, d2, d3]

x = 1; y = 3; z = 6; 

Solve[{a x^2 + b x + c == d1 && a y^2 + b y + c == d2 && 
a z^2 + b z + c == d3&&{d1,{37, 44}}&&{d2,{132, 144, 154, 156, 158}}&&{d3,{497, 568, 588, 595, 596}}}, {a, b, c}, Integers] // Column

If I do all the cases individually I get

{a -> 41, b -> -28, c -> 24}, 
{a -> 35, b -> 2, c -> 0}, 
{a -> 30,  b -> 27, c -> -20},
{a -> 29, b -> 32, c -> -24}, 
{a -> 28, b -> 37,  c -> -28}, 
{a -> 48, b -> -56, c -> 52}, 
{a -> 42, b -> -26,  c -> 28}, 
{a -> 37, b -> -1, c -> 8}, 
{a -> 36, b -> 4,  c -> 4}, 
{a -> 35, b -> 9, c -> 0}

But I like to get them once I run the command.

P.S. Note that it is not necessarily always has solution with a given set of "d1,d2,d3", however I need to know this by once not trial and error for all combinations of the elements of $d1,d2,d3$.

$\endgroup$
  • $\begingroup$ Perhaps something like With[{x = 1, y = 3, z = 6}, Solve[a x^2 + b x + c == #[[1]] && a y^2 + b y + c == #[[2]] && a z^2 + b z + c == #[[3]], {a, b, c}, Integers] ] & /@ {{37, 321, 1272}, {44, 342, 1314}} $\endgroup$ – LouisB Jun 8 '17 at 9:09
  • $\begingroup$ The solutions you give do not appear to be solutions to the problem as stated, e.g. x = 1; y = 3; z = 6; {a x^2 + b x + c, a y^2 + b y + c, a z^2 + b z + c} /. {a -> 48, b -> -56, c -> 52} returns {44, 316, 1444} and 316 is not in {132, 144, 154, 156, 158} -- do I misunderstand? $\endgroup$ – Mr.Wizard Jun 8 '17 at 16:25
  • $\begingroup$ @Mr.Wizard, I added a note at the end of the question. In fact I need that program does repeating procedure instead of me. As in my question $2\times5\times5$ times trial and error, I need that the program should replace the given numbers in "d1,d2,d3", and if there is solution return $a,b,c$ otherwise nothing. $\endgroup$ – asad Jun 8 '17 at 19:47
2
$\begingroup$

Here is how I would approach this. I modified your acceptable d1 values so that there would be a couple of solutions rather than an empty set.

Clear[a, b, c, x, y, z, d1, d2, d3]

x = 1; y = 3; z = 6;

elem = Or @@@ 
   Thread /@ Thread[
     {d1, d2, d3} ==
       {{2, 6, 37, 44}, {132, 144, 154, 156, 158}, {497, 568, 588, 595, 596}}
    ];

Reduce[{a x^2 + b x + c == d1 && a y^2 + b y + c == d2 && a z^2 + b z + c == d3, 
    Sequence @@ elem}, {a, b, c}, Integers] // ToRules // List
{{d1 -> 2, d2 -> 158, d3 -> 497, a -> 7, b -> 50, c -> -55},
 {d1 -> 6, d2 -> 158, d3 -> 596, a -> 14, b -> 20, c -> -28}}
$\endgroup$
3
$\begingroup$

Solve first and then do the replacements?

soln = 
 First[Solve[{a*x^2 + b*x + c == d1, a*y^2 + b*y + c == d2, 
    a*z^2 + b*z + c == d3, x == 1, y == 3, z == 6}, {a, b, c, x, y, 
    z}]]

(* Out[135]= {a -> 1/30 (3 d1 - 5 d2 + 2 d3), 
 b -> 1/30 (-27 d1 + 35 d2 - 8 d3), c -> 1/5 (9 d1 - 5 d2 + d3), 
 x -> 1, y -> 3, z -> 6} *)

replacements = 
  Map[Thread[{d1, d2, d3} -> #] &, 
   Flatten[Outer[
     List, {37, 44}, {132, 144, 154, 156, 158}, {497, 568, 588, 595, 
      596}], 2]];

{a, b, c} /. soln /. replacements

(* Out[137]= {{89/6, -(71/6), 34}, {587/30, -(923/30), 241/5}, {209/
  10, -(361/10), 261/5}, {641/30, -(1139/30), 268/5}, {643/
  30, -(1147/30), 269/5}, {77/6, 13/6, 22}, {527/30, -(503/30), 181/
  5}, {189/10, -(221/10), 201/5}, {581/30, -(719/30), 208/5}, {583/
  30, -(727/30), 209/5}, {67/6, 83/6, 12}, {159/10, -(51/10), 131/
  5}, {517/30, -(313/30), 151/5}, {177/10, -(123/10), 158/5}, {533/
  30, -(377/30), 159/5}, {65/6, 97/6, 10}, {467/30, -(83/30), 121/
  5}, {169/10, -(81/10), 141/5}, {521/30, -(299/30), 148/5}, {523/
  30, -(307/30), 149/5}, {21/2, 37/2, 8}, {457/30, -(13/30), 111/
  5}, {497/30, -(173/30), 131/5}, {511/30, -(229/30), 138/5}, {171/
  10, -(79/10), 139/5}, {233/15, -(272/15), 233/5}, {304/
  15, -(556/15), 304/5}, {108/5, -(212/5), 324/5}, {331/15, -(664/15),
   331/5}, {332/15, -(668/15), 332/5}, {203/15, -(62/15), 173/
  5}, {274/15, -(346/15), 244/5}, {98/5, -(142/5), 264/5}, {301/
  15, -(454/15), 271/5}, {302/15, -(458/15), 272/5}, {178/15, 113/15, 
  123/5}, {83/5, -(57/5), 194/5}, {269/15, -(251/15), 214/5}, {92/
  5, -(93/5), 221/5}, {277/15, -(283/15), 222/5}, {173/15, 148/15, 
  113/5}, {244/15, -(136/15), 184/5}, {88/5, -(72/5), 204/5}, {271/
  15, -(244/15), 211/5}, {272/15, -(248/15), 212/5}, {56/5, 61/5, 103/
  5}, {239/15, -(101/15), 174/5}, {259/15, -(181/15), 194/5}, {266/
  15, -(209/15), 201/5}, {89/5, -(71/5), 202/5}} *)
$\endgroup$
  • 2
    $\begingroup$ FWIW Tuples[{{37, 44}, {132, 144, 154, 156, 158}, {497, 568, 588, 595, 596}}] is cleaner here. $\endgroup$ – Mr.Wizard Jun 8 '17 at 16:44
  • $\begingroup$ @Mr Wizard Now don't be a show-off... (Okay, yeah, what I did was on the wrong side of ghastly.) $\endgroup$ – Daniel Lichtblau Jun 8 '17 at 16:47
  • $\begingroup$ Never truer was the statement you have forgotten more than I will ever know. I'm just polishing the windshield of the car you built. $\endgroup$ – Mr.Wizard Jun 8 '17 at 16:55
  • $\begingroup$ @DanielLichtblau, Thanks, but I forgot to say that I need only integer solutions $a,b,c$. I added "Integers" t the end of "Solve", but I did not get something correct. $\endgroup$ – asad Jun 8 '17 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.