1
$\begingroup$

I'm just starting with Mathematica and I was having a small question: How can you "define" the outcome of a "Solve" in Mathematica? Here is an example from mathematica:

Solve[a x^2 + b x + c == 0, x]

{{x -> (-b - Sqrt[b^2 - 4 a c])/(2 a)}, {x -> (-b + Sqrt[b^2 - 4 a c])/(2 a)}}

I would like to call

x1:= (-b - Sqrt[b^2 - 4 a c])/(2 a)

x2:=(-b + Sqrt[b^2 - 4 a c])/(2 a)

And then make a list {x1, x2} How can you let this happen automatically, because with 2 values it's quite easy but I would like to solve a linear equation with many more solutions.

$\endgroup$
  • $\begingroup$ x /. Solve[a x^2 + b x + c == 0, x] $\endgroup$ – eldo Nov 11 '15 at 10:02
  • $\begingroup$ {x1, x2} = x /. Solve[a x^2 + b x + c == 0, x] $\endgroup$ – Sjoerd C. de Vries Nov 11 '15 at 10:24
1
$\begingroup$

You can have a list like so:

sol = Solve[a x^2 + b x + c == 0, x]

$$\left\{\left\{x\to \frac{-\sqrt{b^2-4 a c}-b}{2 a}\right\},\left\{x\to \frac{\sqrt{b^2-4 a c}-b}{2 a}\right\}\right\}$$

see ReplaceAll for details

list = x /. sol

$$\left\{\frac{-\sqrt{b^2-4 a c}-b}{2 a},\frac{\sqrt{b^2-4 a c}-b}{2 a}\right\}$$

an use parts of your list, see Part:

list[[1]]

(-b - Sqrt[b^2 - 4 a c])/(2 a)

i.e. will be your x1

list[[2]]

(-b + Sqrt[b^2 - 4 a c])/(2 a)

i.e. will be your x2

Assuming:

a = Table[1, {i, 5}]; b = Table[i, {i, -4, 0}]; c = Table[i, {i, 1, 5}];

with list[[1]] and list[[2]] respectively you'll generate

$$\left\{\frac{1}{2} \left(4-2 \sqrt{3}\right),1,\frac{1}{2} \left(2-2 i \sqrt{2}\right),\frac{1}{2} \left(1-i \sqrt{15}\right),-i \sqrt{5}\right\}$$

$$\left\{\frac{1}{2} \left(4+2 \sqrt{3}\right),2,\frac{1}{2} \left(2+2 i \sqrt{2}\right),\frac{1}{2} \left(1+i \sqrt{15}\right),i \sqrt{5}\right\}$$

$\endgroup$

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.