Processing math: 100%

First time here? Check out the FAQ!

Ask Your Question
0

Periodic function

asked 7 years ago

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?

Thanks.

Preview: (hide)

3 Answers

Sort by » oldest newest most voted
0

answered 7 years ago

ndomes gravatar image

updated 7 years ago

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)
Preview: (hide)
link

Comments

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)
dan_fulea gravatar imagedan_fulea ( 7 years ago )

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

ndomes gravatar imagendomes ( 7 years ago )

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

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

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 ( 7 years ago )
0

answered 7 years ago

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

Preview: (hide)
link
0

answered 7 years ago

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 )
Preview: (hide)
link

Comments

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

dan_fulea gravatar imagedan_fulea ( 7 years ago )

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

soking gravatar imagesoking ( 7 years ago )

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|x|. This explains the inner most abs. Now we need to construct the shape of the function only for x0. (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 ζ--function may be highly confused.)

Then we need a periodic function, let us use frac on the positive real halfline. Notation xx. (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 ( 7 years ago )

...but the slope is almost right, so let us make it continuous somehow. We consider then step by step: xx , xx12 , x| x12 | , 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 ( 7 years ago )

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

soking gravatar imagesoking ( 7 years ago )

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

Stats

Asked: 7 years ago

Seen: 1,828 times

Last updated: May 27 '17