1
$\begingroup$

I have a function that returns multiple values, like this:

f[x_] := ( (*... something ... *); {y1, y2});

They are computed together, so there's absolutely no sense to split the function to two functions.

Now, I want to plot them both:

Plot[f[a], {a,0,1}]

Oops, the two graphs are drawn with the same style. Explicit style decaration (like PlotStyle->{Red,Green}) doesn't change the situation. The Plot routine doesn't recognize f[a] as two values. This is the main trouble. Well, the documentation has a cure for this:

Plot[Evaluate[f[a]], {a,0,1}]

It's OK... until it turns out that 1) numerical computation of f if slow, and 2) symbolic computation of f is extremely slow. In my case, there is a system of 10 linear equations. And this last expression evaluates f symbolically. Well, of course, I can forbid this by declaring f as f[x_?NumericQ], but then the Evaluate operation becomes totally useless.

Why should I evaluate the whole function symbolically only to ensure that it returns two values? Is there another way to explain this fact to the Plot routine, so it would use different styles for the two graphs?

Well, actually, I know the answer:

Plot[{f[a][[1]], f[a][[2]]}, {a,0,1}]

But it is ugly and wasteful. It performs almost the same computations twice. It makes me feel uncomfortable. May be, there is a better solution?

$\endgroup$

marked as duplicate by Mr.Wizard Apr 27 '14 at 19:51

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$ I am certain this is a duplicate. Let me find it. $\endgroup$ – Mr.Wizard Apr 27 '14 at 19:50
  • $\begingroup$ Thank you. Of course, I tried to find someting like this before asking, but I failed. Also, now I see in Related questions an elegant solution with splitstyle here. $\endgroup$ – colt_browning Apr 27 '14 at 19:55
  • $\begingroup$ Also very similar: (8637) $\endgroup$ – Mr.Wizard Apr 27 '14 at 19:55
  • $\begingroup$ You're welcome. Thanks for trying to find a Q&A first. $\endgroup$ – Mr.Wizard Apr 27 '14 at 19:57
  • $\begingroup$ Reviewing the answers, Simon's splitstyle method in the second linked question is probably what you want; it's better than my methods in earlier question. $\endgroup$ – Mr.Wizard Apr 27 '14 at 20:01

Browse other questions tagged or ask your own question.