1
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ Either {{L -> 1}, {L -> 2}, {L -> 3}} or take a look at Table etc. Related: 1731 $\endgroup$ – Kuba Sep 26 '18 at 11:59
2
$\begingroup$

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:

enter image description here

Have fun!

$\endgroup$
  • 2
    $\begingroup$ Recommend that you also include use of Evaluate to distinguish between plots. $\endgroup$ – Bob Hanlon Sep 26 '18 at 14:34
3
$\begingroup$

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

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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