I'm trying to define (and plot) a piecewise function with the following structure:
$$f(x) = \begin{cases} g(x) & nT \leq x \leq \frac{2n+1}{2}T\\ -g(x) & \frac{2n+1}{2}T \leq x \leq (n+1)T \end{cases}$$
where n
is an integer, n >= 0
and T
is a constant.
The idea is that it cycles between positive and negative after a full period is complete.
My attempt was:
f[x_] := Piecewise[{
{g[x], (n T <= x <= ((2 n + 1)/2) T) && Element[n, Integers] && (n >= 0)},
{-g[x], (((2 n + 1)/2) T <= x <= (n + 1) T) && Element[n, Integers] && (n >= 0)}}]
But when I try a test value for x
in f[x]
it shows n
in the conditions for the output, so it seems to not get past the conditions. I believe plotting fails for this reason too.
I have never tried to use Integers
in Mathematica.
T
? $\endgroup$Floor[x/T]
instead ofn
worked.f[x_] := Piecewise[{{g[x], Floor[x/T] T <= x <= ((2 Floor[x/T] + 1)/2) T}, {-g[x], ((2 Floor[x/T] + 1)/2) T <= x <= (Floor[x/T] + 1) T}}]
I'm not exactly sure how I would simplify the second condition. belisarius: Thank you, I can see that your version works. $\endgroup$