views:

60

answers:

1

Hi,

I need some help in Mathematica. I'm trying to plot functions that are stored in lists like:

list = {{3x,1,5},{2x^2,0,4}}

I need get an output similar to if I inputted:

Show[Plot[3x,{x,1,5}],Plot[2x^2,{x,0,4}]]

But I am not quite sure how this is achieved?

Thanks in advance

+3  A: 

Of the many possible ways to do this, here's a perhaps cryptic and terse one-liner, followed by explanation:

Show[Plot[#, {x, ##2}] & @@@ list]

First, # is the operator form of Slot and ## is the operator form of SlotSequence, and f @@@ expr is the infix operator for Apply[f,expr,{1}]], so this could be more verbosely expressed as:

Show[Apply[Plot[#, {x, ##2}] &, list, {1}]]

Thus, for each sub-list of your list, the elements are passed as arguments to the pure function. In the pure function, # is the first argument (first sub-element, e.g. the function, 3x) and ##2 is the rest of the arguments (starting with the second one as a Sequence, e.g. Sequence[0, 4]). For the first element then, the command evaluated would be Plot[3x, {x,0,4}].

If the above is too cryptic, you could always define a function and use Map:

plotter[{func_, interval__}] := Plot[func, {x, interval}]
Show[plotter /@ list]

Hope that helps!

Michael Pilat
Wow absolutely fantastic :D Thank you so much - I've been trying to get my head around this for hours and it turns out to be that simple!
Matt Immer
@Michael: I always forget about using `SlotSequences`, and this is a very good use of them.
rcollyer