1

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?

Phab
  • 1,623
  • 9
  • 15
  • @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. – Phab Nov 25 '14 at 10:03
  • 1
    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 ;-) – Yves Klett Nov 25 '14 at 10:11
  • @YvesKlett You see ...there's always a way doing things better, shorter or smarter :-) – Phab Nov 25 '14 at 13:49
  • 1
    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. – Yves Klett Nov 25 '14 at 15:15

1 Answers1

3

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

Yves Klett
  • 15,383
  • 5
  • 57
  • 124
mikuszefski
  • 4,877
  • 1
  • 14
  • 18