# Plotting a sequence of functions with cases

How can I plot functions like this $$f_{2^n+j}(x)=\begin{cases}0, x\in [\frac{j}{2^n},\frac{j+1}{2^n}]\\1, x\in x<\frac{j}{2^n} \text{ or } x>\frac{j+1}{2^n}\end{cases}$$?

I tried something like this:

f[j_, n_, x_] = Piecewise[
{
{0, j/2^n <= x && x <= (j + 1)/2^n},
{1, x < j/2^n || x > (j + 1)/2^n}
}
]


But it did not help. It is my first time I use this software.

• – kglr
Oct 21 at 8:51
• The above is just the definition of the function. It does nothing if you do not call it. A function has to be called to do its work. Did you actually call it? Can you show how? btw, better to use := for function definition. This works Plot[f[1, 2, x], {x, 0, 1}] for example. Oct 21 at 8:52
– kglr
Oct 21 at 8:56
• – kglr
Oct 21 at 8:57
• @Nasser : Yes. Thanks! It works if it do as you write. My attempt was "Plot[f[j,n,x], {j, 0, 10}, {n, 0, 10}, {x, 0, 10}]". What can I do to have a sequences of $j$ and $n$ in Plot function without specifying paticular numbers (like 1 and 2 in your code)? Oct 21 at 9:02

What can I do to have a sequences of j and n in Plot function without specifying paticular numbers

If I understand you right, you can first generate the j,n data first

Clear["Global*"]
data = Flatten[Table[{j, n}, {j, 3}, {n, 3}], 1]


Now generate the functions for each j,n above

f[j_, n_, x_] :=
Piecewise[{{0, j/2^n <= x && x <= (j + 1)/2^n}, {1,
x < j/2^n || x > (j + 1)/2^n}}]

p = f[Sequence @@ #, x] & /@ data


Now make the plot for each

    Grid[Partition[Plot[#, {x, 0, 10}] & /@ p, 3], Frame -> All,
Spacings -> {3, 3}]
`

• Thank you very much! Oct 21 at 9:37
• @Mokhmad-SalekhKhekhaev I did not put labels on the plots to indicate "j","n" values. I assumed you know how to do and add this. This will help know for which value of "j,n" each plot is associated with. Oct 21 at 9:43