Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Is it possible to set the following function f[a], where a can be any real number?

$$ f(\alpha) = \begin{cases} 1 & \text{if } 2\pi n < \alpha < \pi (2n+1) \\ 2 & \text{if } \pi (2n+1) \le \alpha \le 2\pi (n+1) \end{cases}, \ n \in \mathbb{Z}\ $$

The issue here is n which I want to be substituted consecutively by an integer in order to produce propriate continuous conditions. I guess, it is not the case where Assumptions should be used as my attempt underneath doesn't work.

f[a_] := \[Piecewise] {
   {1, 2 \[Pi]n < a && a < \[Pi] (2 n + 1)},
   {2, \[Pi] (2 n + 1) <= a && a <= 2 \[Pi] (n + 1)}
  }, Assumptions -> n \[Element] Integers

Thanks.

share|improve this question

5 Answers 5

up vote 1 down vote accepted

Here's one way to make your function periodic:

f[t_] := Which[0 <= t < Pi, 1, Pi <= t < 2 Pi, 2, t < 0, f[t + 2 Pi],  t > 2 Pi, f[t - 2 Pi]]

For example:

Plot[f[t], {t, -10, 10}]

enter image description here

This method works well for any function you care to use (not just square waves). For instance, f[t] can be linear in one half and quadratic in the second half:

f[t_] := Which[0 <= t < Pi, t, Pi <= t < 2 Pi, t^2, t < 0, f[t + 2 Pi], t > 2 Pi, f[t - 2 Pi]];
Plot[f[t], {t, -10, 10}]

enter image description here

share|improve this answer
    
Lovely! Thanks. –  mikeonly Aug 17 at 20:04

You can also use the built-in function SquareWave:

ClearAll[f];
f[x_] := SquareWave[{2, 1}, x/(2 Pi)];

Plot[f[x], {x, 0, 10 Pi}, Ticks -> {Pi Range[10], Automatic},
    ExclusionsStyle -> Automatic, PlotRange -> {0, 3}, ImageSize -> 400]

enter image description here

Update: another alternative: Use ListInterpolation with options InterpolationOrder->0 and PeriodicInterpolation -> True

ClearAll[f2];
f2[x_] := ListInterpolation[{2, 1, 2}, {{0, 2 Pi}}, InterpolationOrder -> 0, 
            PeriodicInterpolation -> True][x];
Plot[f2[x], {x, 0, 10 Pi}, Ticks -> {Pi Range[10], Automatic}, 
     Exclusions -> None, PlotRange -> {0, 3}]

enter image description here

share|improve this answer

You can use a combination of Floor,

Floor[x, a] gives the greatest multiple of a less than or equal to x

and Mod,

Mod[m, n] gives the remainder on division of m by n

to get

f[x_] := 1 + Floor[Mod[x, 2 π]/π]

Mathematica graphics

share|improve this answer

Here is a method that uses UnitStep and Sin to generate the square wave. Sin produces the periodicity and UnitStep maps the sinusoid into a square-wave. This method will be faster than Which.

f[x_] := 1 + UnitStep[Sin[x + Pi]]
Plot[f[x], {x, -10, 10}, Exclusions -> None, PlotStyle -> Thick]

sq-wave

share|improve this answer

You can use Mod with a non-integer second argument:

f[a_] := Ceiling[Mod[a, 2 \[Pi]]/\[Pi]]

To generalize you can make a Periodic operator as follows:

Periodic[T_, offset_: 0][f_] := f@*((Mod[#, T] - offset) &)
f2pi = Periodic[2 \[Pi]][Ceiling[#/\[Pi]] &]
f3pi = Periodic[3 \[Pi], 0.1][Ceiling[#/\[Pi]] &]
quadratic[x_] := (x/\[Pi])^2;
parabola = Periodic[2 \[Pi], \[Pi]][quadratic]

We offset f3pi so that the lines will show up in the plot:

Plot[{f2pi[x], f3pi[x], parabola[x]}, {x, -10, 10}, Exclusions -> None]

gives something akin to

plot

To get bill s' piecewise function, you can simply do:

bills[t_]:=bills[t_] := Which[t < \[Pi], t, True, t^2]

Plot[Periodic[2\[Pi]][bills][t], {t,-10,10}]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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