17

Suppose I have a function that I want to plot for several values of a parameter all in the same window. For instance the function $f$ defined as

f[a_,b_,c_,d_]:= a+Sin[b]*Exp[c]-d^4

If I now want to plot f[a,1,2,3] with respect to a from 0 to 1 I simply write

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

But suppose I want to instead plot f[a,b,2,3] same as before for a from 0 to 1 but now the parameter b takes on several values, say b = 0,1,2,3,4 and so on. How can I do this all in the same plot (preferably with different colors on each curve corr. to the various b-values)?

Physics_maths
  • 1,329
  • 1
  • 10
  • 26

1 Answers1

17

You can use Table for this:

Plot[Evaluate@Table[f[a, b, 2, 3], {b, 0, 5, 1}], {a, 0, 1}]

And you can also put there multiple parameters, so Table will use all of them.

enter image description here


You can use PlotLegend to specify the legend:

Plot[Evaluate@Table[f[a, b, 2, 3], {b, 0, 5, 1}], {a, 0, 1}, 
 PlotLegends -> LineLegend[Table[b, {b, 0, 5, 1}], LegendLabel -> x]]

enter image description here

m0nhawk
  • 3,867
  • 1
  • 20
  • 35