1
$\begingroup$

This question already has an answer here:

I want to plot multiple solutions of an equation, so that one can differ them in the plot.

The given equation is e.g.

sol = Solve[3 y^2 == x^3 + 7 x^2 + x, {x, y}]

Out: {{y -> -((Sqrt[x] Sqrt[1 + 7 x + x^2])/Sqrt[3])}, {y -> (
       Sqrt[x] Sqrt[1 + 7 x + x^2])/Sqrt[3]}}

Now I have 3 ways to plot the result.

The first one shows them in different colors with legend, but I dont like it, because I have to enter both solutions separately in the plot command:

Plot[{y /. sol[[1]], y /. sol[[2]]}, {x, -10, 10}, 
 PlotLegends -> Automatic]

enter image description here

Second command is very short, but shows both solutions in one color without legend. So it could be there is only one solution:

Plot[y /. # & /@ sol, {x, -10, 10}, PlotLegends -> Automatic]

enter image description here

The third needs no manual manipulation and shows both solution with an own color:

Module[{res = y /. # & /@ sol},
 Plot[res, {x, -10, 10}, PlotLegends -> Automatic]
 ]

enter image description here

But for getting better with Mathematica, is there another/better/smarter way?

$\endgroup$

marked as duplicate by Yves Klett, Kuba, Öskå, Karsten 7., Sjoerd C. de Vries Nov 25 '14 at 14:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ @YvesKlett I agree mikuszefski's answer is a duplicate to this. But I was asking for more/smarter ways doing this. If in the end this should be the best way, yes then it's a duplicate. $\endgroup$ – Phab Nov 25 '14 at 10:03
  • 1
    $\begingroup$ Hmmm, seems pretty neat to me already - what would you be expecting in terms of "smarter"? In fact you can make it even shorter (no need for mapping): Plot[{Evaluate[y /. sol]}, {x, -10, 10}, PlotLegends -> Automatic], but I guess it will not get much better than that ;-) $\endgroup$ – Yves Klett Nov 25 '14 at 10:11
  • $\begingroup$ @YvesKlett You see ...there's always a way doing things better, shorter or smarter :-) $\endgroup$ – Phab Nov 25 '14 at 13:49
  • 1
    $\begingroup$ Phab, glad you like it! I hope you do not mind the closure - in any case you could edit the question to differentiate it a bit more and nominate it for re-opening. $\endgroup$ – Yves Klett Nov 25 '14 at 15:15
3
$\begingroup$

Your second solution can be improved by Evaluate[] on the first plot argument.

Plot[Evaluate[y /. sol], {x, -10, 10}, 
 PlotLegends -> Automatic]

enter image description here

$\endgroup$
  • $\begingroup$ I didnt hit on it ... nice! I added code and resulting plot. $\endgroup$ – Phab Nov 25 '14 at 9:26
  • $\begingroup$ thx, as I do not have access to Mathematica at the moment. $\endgroup$ – mikuszefski Nov 25 '14 at 10:04

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