# Periodic function

HI all,

I want to write in sage a 2pi periodic even function defined by f(t) = -t + pi, for t in [0, pi). I already checked Defining a periodic function and defining periodic functions on this plateform, but none of the provided solutions works for me. My main issue is that I need a way to transform any real number x into its unique representative in the interval [-pi, pi). For that I used frac and % but they both raise errors. Could anyone help me out?

Thanks.

edit retag close merge delete

Sort by ยป oldest newest most voted

EDITED

f(t) = -t + pi        # interval  [0, pi]
g(t) = 1/2*f(t*pi)    # transformed to interval  [-1, 1]
# h = lambda x: 2*g(RR(x/pi).frac())   # including retransformation
h = lambda x: (heaviside(x)-1)*pi + 2*g(RR(x/pi).frac())
plot(h,-3*pi,3*pi)


UPDATE 2 --- a symbolic function:

f(x) = 1/2*(sign(sin(2*x))*arccos(cos(2*x-pi)) + pi)
plot(f,-3*pi,3*pi,aspect_ratio=1)

more

Well, frac is a misleading name in this case, since

sage: ( -1.5 ).frac()
-0.500000000000000


Or just plot:

plot(h,-3*pi,3*pi)

( 2017-05-26 17:35:13 +0100 )edit

Thanks for the hint. I tried to improve my answer.

( 2017-05-26 22:39:21 +0100 )edit

thanks, ndomes for your answer, bu the function should be even, yours is not :(. By the way are we obliged to rescale the function from [0, pi) to [0,1)? Is it not possible to solve this question directly (I mean without working on the interval [0,1))?

( 2017-05-27 09:20:35 +0100 )edit

Sorry for my misunderstanding. I ignored the 'even' because the function you provided isn't even. The easiest way to build a periodic function is to start with a periodic function. The start interval depends on the function you use and therefore we may need axis transformations to get the desired result. One more suggestion:

f(x) = abs(arccos(cos(x))-pi)

( 2017-05-28 09:33:39 +0100 )edit

I agree with you, that's why I initially started with this piece of code

v(x) = piecewise([([0, pi], -x + pi), ((pi, 2*pi), x - pi)])


where v is the function I want to duplicate all over the real line. But when I write

f(x) = v(T*RR(abs(x)/T).frac())


it raises an error, and it seems that the issue is coming from T*RR(abs(x)/T).frac().

Even though your answer is correct and defines the function I wanted, I still want to find a way to do the same with the piecewise function I defined earlier, can you help me with it?

( 2017-05-28 18:33:03 +0100 )edit

For mapping one real interval to another interval, you have the numpy function interp() (linear approximation).

Example :

import numpy as np
from numpy import interp
prec = 200
xp = np.linspace(0.0, 99.0, prec)
yp = np.linspace(-1.0, 1.0, prec)
print interp([52.0],xp,yp)


The printed value is near 0.0 (middle of interval [-1,1]) because 52.0 is near 50.0 (middle of interval [0,99])

more

It is, i think, the following function f...

def g(x):    return abs( 2*RR( abs(x)/2 ).frac() - 1 )
def f(x):    return pi * g(x/pi)


In a picture...

plot( g, -3, 3, aspect_ratio=1 )
plot( f, -3*pi, 3*pi, aspect_ratio=1 )

more

... done in the spirit of the previous post.

( 2017-05-26 17:47:09 +0100 )edit

Yeah that's the plot I wanted, but I don't understand the procedure. Can you explain it please?

( 2017-05-27 09:21:57 +0100 )edit

I tried to use the already implemented functions, combined in a suitable way.

First of all, we need an even function, so it is natural to go through $x\to |x|$. This explains the inner most abs. Now we need to construct the shape of the function only for $x\ge 0$. (This already removes the problem in the first answer, i could have also easily fallen in the trap of the implemented frac function, which is not a periodic one, not the one from maths. People that did some computations with the Riemann $\zeta$--function may be highly confused.)

Then we need a periodic function, let us use frac on the positive real halfline. Notation $x\to{x}$. (The last notation is not the one element set, as my teacher joked each time he could...)

It has jumps at integer arguments, but the...

( 2017-05-30 00:42:32 +0100 )edit

...but the slope is almost right, so let us make it continuous somehow. We consider then step by step: $$x\to {x}\ ,$$ $$x\to {x}-\frac 12\ ,$$ $$x\to \left|\ {x}-\frac 12\ \right|\ ,$$ now it is continuous, and it remains a small step, rescaling arguments, and values of the last function.

Note: I have no idea how to markdown + latex {x}. Abve i tried to display the functions: {x}, then {x}-1/2, finally | {x} - 1/2 |

( 2017-05-30 00:43:25 +0100 )edit

OK, thanks for the explanation. I didn't get everything, but I'll take sometime to read and understand each step.

( 2017-06-08 16:23:41 +0100 )edit