4
$\begingroup$

The MWE below

Clear[p, x]
p = -Sqrt[2];
{x1, x2} = x /. Solve[p x^2 + (1 + p^2) x + p == 0, x]

solves the equation for the specific parameter $p=-\sqrt2$. How can I specify a list of $p$ values, e.g.

pvalues = {-Sqrt[2], Sqrt[2], -1/Sqrt[2], 1/Sqrt[2]}

and let Mathematica calculate the solutions for each $p$? TIA.

$\endgroup$

4 Answers 4

3
$\begingroup$

one of many ways

ClearAll[p, x];
pvalues = {-Sqrt[2], Sqrt[2], -1/Sqrt[2], 1/Sqrt[2]};
res = ( x /. Solve[# x^2 + (1 + #^2) x + # == 0, x]) & /@ pvalues

Mathematica graphics

Update

if I want to use the output and calculate x1/x2 for each of the list pairs, how is this easily calculated?

There are many ways to do this. Here are some but I am sure more can be found. Assuming res is the above from the above, then any of these will work

(#[[1]]/#[[2]]) & /@ res
(First@#/Last@#) & /@ res
Map[#[[1]]/#[[2]] &, res]
Function[{x1, x2}, x1/x2] @@@ res
Apply[Function[{x1, x2}, x1/x2], res, {1}]

Each of the above commands gives

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ Follow up; if I want to use the output and calculate x1/x2 for each of the list pairs, how is this easily calculated? $\endgroup$
    – mf67
    Commented Sep 24, 2022 at 21:11
  • 1
    $\begingroup$ @mf67 fyi, updated to answer your comment $\endgroup$
    – Nasser
    Commented Sep 24, 2022 at 23:43
3
$\begingroup$

Table is convenient.

Clear[sol];
pvalues = {-Sqrt[2], Sqrt[2], -1/Sqrt[2], 1/Sqrt[2]};
sol = Solve[p x^2 + (1 + p^2) x + p == 0, x]
x/.Table[sol, {p, pvalues}]
Table[(x /. sol[[1]])/(x /. sol[[2]]), {p, pvalues}]

enter image description here

$\endgroup$
2
$\begingroup$

Using SolveValues:

Map[Composition[SolveValues[#, x] &, # == 0 &, Function[{p}, p x^2 + (1 + p^2) x + p]], pvalues]

enter image description here

Or using SolveValues and Thread:

SolveValues[#, x] & /@ Thread[Function[{p}, p x^2 + (1 + p^2) x + p] /@ pvalues == 0]

enter image description here

Or more compact:

SolveValues[#, x] & /@ Function[{p}, p x^2 + (1 + p^2) x + p == 0] /@ pvalues

enter image description here

$\endgroup$
1
$\begingroup$
List /@ Thread[Rule[p, pvalues]]

generates the following rules:

$\left\{\left\{p\to -\sqrt{2}\right\},\left\{p\to \sqrt{2}\right\},\left\{p\to -\frac{1}{\sqrt{2}}\right\},\left\{p\to \frac{1}{\sqrt{2}}\right\}\right\}$

Each of these values for p can be applied to the equation:

Clear[x, p];
sol = x /. Solve[p x^2 + (1 + p^2) x + p == 0, x] /. 
  List /@ Thread[Rule[p, pvalues]]

$\left\{\frac{1}{\sqrt{2}},\sqrt{2}\right\},\left\{-\frac{1}{\sqrt{2}},-\sqrt{2}\right\},\left\{\sqrt{2},\frac{1}{\sqrt{2}}\right\},\left\{-\sqrt{2},-\frac{1}{\sqrt{2}}\right\}$

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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