Function return value of x instead of y
<p>f(x) represents the function of a triangular waveform. In which you input the value x and it returns you the associated y value. However my function returns x every time instead of y. For example f(1) should give 2/pi instead of 1.</p>

<pre><code>def f(x):

    y=x

    if x in arange(-math.pi,-math.pi/2):
        y=(-2/math.pi)*x-2


    elif x in arange(-math.pi/2,math.pi/2):
        y=(2/math.pi)*x


    elif x in arange(math.pi/2,math.pi):
        y=(-2/math.pi)*x+2

    return y
</code></pre>

<p><a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html" rel="nofollow"><code>numpy.arange</code></a> returns an array of non-consecutive numbers. <code>in</code> operation against it will return <code>True</code> only if the left-hand operand belong to those numbers.</p>

<p>You'd better to use <code>&lt;=</code> / <code>&lt;</code> pair to avoid such problem. In addition to be correct, it also save cost of creating arrays.</p>

<pre><code>def f(x):
    y = x

    if -math.pi &lt;= x &lt; -math.pi/2:
        y = (-2/math.pi)*x-2
    elif -math.pi/2 &lt;= x &lt; math.pi/2:
        y = (2/math.pi)*x
    elif math.pi/2 &lt;= x &lt; math.pi:
        y = (-2/math.pi)*x+2

    return y
</code></pre>

python function