3
$\begingroup$

I would like to plot a sequence of functions in one plot.

My function is defined piecewise:

f[x_] := Piecewise[{{-1, -1 <= x < -(1/n)}, {n x, -(1/n) <= x <= 1/n}, {1, 
 1/n < x <= 1}}];

I would like to plot the functions for let's say

n = {1,2,3}

in one plot.

How could I do that?

My ideas were to use Map, Apply or MapApply but all of them did not work.

$\endgroup$
1
  • $\begingroup$ Using Map: Plot @@ {f[x] /. n -> # & /@ {1,2,3}, {x, -5, 5}} $\endgroup$ Commented Oct 22, 2022 at 8:47

2 Answers 2

3
$\begingroup$
Clear[f, n, x]
f[x_] := Piecewise[{{-1, -1 <= x < -(1/n)}, {n x, -(1/n) <= x <= 
      1/n}, {1, 1/n < x <= 1}}];

Plot[Evaluate[f[x] /. {{n -> 1}, {n -> 2}, {n -> 3}}], {x, -2, 2}, 
 Exclusions -> None, PlotLegends -> "Expressions"]
$\endgroup$
3
$\begingroup$
f[n_][x_] = 
  Piecewise[{{-1, -1 <= x < -(1/n)}, {n x, -(1/n) <= x <= 1/n}, {1, 
     1/n < x <= 1}}];
Plot[Table[f[n][x], {n, 3}] // Evaluate, {x, -5, 5}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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