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.

  • $\begingroup$ see tutorial: Defining Functions $\endgroup$
    – kglr
    Oct 21 at 8:51
  • $\begingroup$ 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. $\endgroup$
    – Nasser
    Oct 21 at 8:52
  • $\begingroup$ see also: Immediate and delayed definitions $\endgroup$
    – kglr
    Oct 21 at 8:56
  • $\begingroup$ .. and What is the difference between Set and SetDelayed? $\endgroup$
    – kglr
    Oct 21 at 8:57
  • $\begingroup$ @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)? $\endgroup$ 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

data = Flatten[Table[{j, n}, {j, 3}, {n, 3}], 1]

Mathematica graphics

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

Mathematica graphics

Now make the plot for each

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

Mathematica graphics

  • $\begingroup$ Thank you very much! $\endgroup$ Oct 21 at 9:37
  • $\begingroup$ @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. $\endgroup$
    – Nasser
    Oct 21 at 9:43

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.