Instead of using Show[Plot[f[x]/.{L->1},{x,0,1}],Plot[f[x]/.{L->2},{x,0,1}], Plot[f[x]/.{L->3},{x,0,1}]]
, I was wondering if there is a much simpler way by using some sort of replacing rule. Something like Plot[f[x]/.{L->1,L->2,L->3},{x,0,1}]
, and have the three curves on the same plot. Does Mathematica support something like this?
$\begingroup$
$\endgroup$
1
2 Answers
$\begingroup$
$\endgroup$
1
Let us fix the function f for this example, say, this way:
f[x_, L_] := (x/L)^2;
Then try this:
Plot[f[x, #] & /@ {1, 2, 3}, {x, 0, 1}]
or this:
Plot[Table[f[x, L], {L, {1, 2, 3}}], {x, 0, 1}]
or this:
Plot[Thread[f[x, #] &[{1, 2, 3}]], {x, 0, 1}]
They all return the following plot:
Have fun!
-
2$\begingroup$ Recommend that you also include use of
Evaluate
to distinguish between plots. $\endgroup$ Commented Sep 26, 2018 at 14:34
$\begingroup$
$\endgroup$
Amplifying on Alexei's answer
f[x_, L_] := (x/L)^2;
Either
Plot[Evaluate@Table[f[x, L], {L, {1, 2, 3}}], {x, 0, 1},
PlotLegends -> Placed["Expressions", {.25, .75}]]
or
Plot[Evaluate[f[x, #] & /@ {1, 2, 3}], {x, 0, 1},
PlotLegends -> Placed["Expressions", {.25, .75}]]
produce
{{L -> 1}, {L -> 2}, {L -> 3}}
or take a look at Table etc. Related: 1731 $\endgroup$