Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
You may find this discussion relevant. –  Leonid Shifrin Jul 10 '12 at 15:10
add comment

3 Answers

up vote 15 down vote accepted

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

Plot[Map[rp[x, #] &, ratios], {x, 0, 1}, Evaluated -> True]

enter image description here

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.

share|improve this answer
    
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
add comment

This is a direct fulfilling of your attempts :

Plot[ rp[x, #] & /@ ratios, {x, 0, 1}]

enter image description here

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}]

enter image description here

share|improve this answer
    
@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
1  
@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
add comment

Some things to think about:

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

rp[x, #] & /@ ratios

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

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]

Mathematica graphics

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]

Mathematica graphics

Be sure to read this and this for an explanation of Evaluated -> True.

share|improve this answer
1  
+1 Very nice: Plot[Outer[rp, {x}, ratios], {x, 0, 1}] –  Chris Degnen Jul 11 '12 at 10:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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