How do I use Map for a function with two arguments?

I'm a newbie who tries to be a good boy, and use Map instead of writing out a list of functions.

I have a table I want to Map onto:

ratios = Table[10^(n/10), {n, 0, 10}]

and a function

rp[x_, r_] := 1000 x (r + 1)/(r + x)

which I want to Plot for the 11 ratios, for x in [0, 1]:

Plot[Map[rp, ratios], {x, 0, 1}]

This doesn't work, and I can guess why: rp requires two arguments, and MMA probably doesn't know which one is the ratio. And x also doesn't appear as a parameter. How do I fix this?

You can create a pure function that maps only on ratio as follows:

Plot[Map[rp[x, #] &, ratios], {x, 0, 1}, Evaluated -> True] You will need the Evaluated -> True option in order for Plot to view the functions as several different ones and plot them in different colours.

You can also bypass having to use Map by creating your function with the Listable attribute. For example:

rp2[x_] := Function[{r}, 1000 x (r + 1)/(r + x), Listable]
Plot[rp2[x][ratios], {x, 0, 1}, Evaluated -> True]

This returns the same output as above, but automatically maps (threads) over lists.

• The Evaluated->True works fine for the PlotStyle option but doesn't work for the PlotLegend option, any clue why? Or has any other trick has to be used? – Öskå Jul 22 '13 at 10:32
• @Öskå Do you have an example? This works: Plot[Table[Sin[k x], {k, 1, 3}], {x, 0, 2 π}, PlotLegends -> Automatic, Evaluated -> True] – rm -rf Jul 22 '13 at 15:02
• Using Mathematica 8 PlotLegend -> {"sine", "sine", "sine"}] only shows "sine" one time: imgur.com/1uyciA9 – Öskå Jul 22 '13 at 15:45
• @Öskå Ugh... that's using the old PlotLegends package, which is hideous. I have no idea how to make it fix and I'm not going to touch that with a 10 ft pole :( Perhaps you might be able to use this answer, which is a far better alternative for Mathematica 8. – rm -rf Jul 22 '13 at 17:12
• mhm thanks :-) I'll take a look at that long answer tomorow. Once again, thanks :-) – Öskå Jul 22 '13 at 17:14

This is a direct fulfilling of your attempts :

Plot[ rp[x, #] & /@ ratios, {x, 0, 1}] rp[x, #] & denotes a function depending on the second argument in rp, while /@ is a shorthand for Map, i.e rp[x, #] & /@ ratios means Map[ rp[ x,#] &, ratios ].

Here is another way to plot your functions without Map :

Plot[ Evaluate[ Table[ rp[x, a], {a, ratios}]], {x, 0, 10}]

Evaluate serves here for plotting curves in various colors.

You can plot graphs of the functions as a family of curves in three dimensions using ParametricPlot3D.

ParametricPlot3D[ Evaluate[ Table[{x, a, rp[x, a]}, {a, ratios}]],
{x, 0, 10},  BoxRatios -> {10, 10, 5}] • @stevenvh You've got another way of doing your task in my answer without Map. – Artes Jul 10 '12 at 15:34
• When using Evaluate in this fashion you need to guard against a global value of x. Add something about this to your answer and you'll have my vote. – Mr.Wizard Jul 10 '12 at 16:46
• @Mr.Wizard Do you like Block[{x}, ParametricPlot3D[ Evaluate[Table[{x, a, rp[x, a]}, {a, ratios}]], {x, 0, 10}, BoxRatios -> {10, 10, 5}] ] more ? Otherwise, I'd choose Evaluated->True in ParametricPlot3D. – Artes Jul 10 '12 at 22:28
• Block, Evaluated -> True, or simply the mention of the issue would do. You have my vote now anyway. – Mr.Wizard Jul 11 '12 at 0:15

Some things to think about:

rp[x_, r_] := 1000 x (r + 1)/(r + x)

rp[x, #] & /@ ratios

Outer[rp, {x}, ratios][]

Table[rp[x, i], {i, ratios}]

Block[{rp},
rp[x, ratios] // Thread
]

And Formal Symbols (looks better in the Notebook):

ClearAll[rp]
rp[r_] := 1000 \[FormalX] (r + 1)/(r + \[FormalX])

Plot[rp /@ ratios, {\[FormalX], 0, 1}, Evaluated -> True] And this (see Parameterized function and Currying in Mathematica):

ClearAll[rp]
rp[x_][r_] := 1000 x (r + 1)/(r + x)

Plot[rp[x] /@ ratios, {x, 0, 1}, Evaluated -> True] Be sure to read this and this for an explanation of Evaluated -> True.

• +1 Very nice: Plot[Outer[rp, {x}, ratios], {x, 0, 1}] – Chris Degnen Jul 11 '12 at 10:48

When using Evaluated->True the plot legends to not display or display only one item. The answer by Artes of using the Evaluate function on the first argument to Plot will also fix the issue with plot legends, so their answer is the best from that perspective.

Plot[Evaluate[Map[rp[x, #] &, ratios]], {x, 0, 1},
PlotLegends -> Automatic]

This produces • Welcome to Mma.SE! This looks like an extended comment on the accepted answer; so make sure to reference it (e.g. "Fixing the PlotLegends issue in rm -rf's answer, ... " so this answer can stand on its own. – lirtosiast Jun 30 at 0:59