Ask Your Question

Periodic function

asked 2017-05-26 14:51:21 +0200

soking gravatar image

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?


edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2017-05-26 16:14:27 +0200

ndomes gravatar image

updated 2017-05-27 06:41:07 +0200


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())

UPDATE 2 --- a symbolic function:

f(x) = 1/2*(sign(sin(2*x))*arccos(cos(2*x-pi)) + pi)
edit flag offensive delete link more


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

sage: ( -1.5 ).frac()

Or just plot:

dan_fulea gravatar imagedan_fulea ( 2017-05-26 17:35:13 +0200 )edit

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

ndomes gravatar imagendomes ( 2017-05-26 22:39:21 +0200 )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))?

soking gravatar imagesoking ( 2017-05-27 09:20:35 +0200 )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)
ndomes gravatar imagendomes ( 2017-05-28 09:33:39 +0200 )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?

soking gravatar imagesoking ( 2017-05-28 18:33:03 +0200 )edit

answered 2017-06-02 21:42:41 +0200

dom gravatar image

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])

edit flag offensive delete link more

answered 2017-05-26 17:46:19 +0200

dan_fulea gravatar image

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 )
edit flag offensive delete link more


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

dan_fulea gravatar imagedan_fulea ( 2017-05-26 17:47:09 +0200 )edit

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

soking gravatar imagesoking ( 2017-05-27 09:21:57 +0200 )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...

dan_fulea gravatar imagedan_fulea ( 2017-05-30 00:42:32 +0200 )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 |

dan_fulea gravatar imagedan_fulea ( 2017-05-30 00:43:25 +0200 )edit

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

soking gravatar imagesoking ( 2017-06-08 16:23:41 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-05-26 14:51:21 +0200

Seen: 1,454 times

Last updated: May 27 '17