Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Plotting a periodic function

I want to plot a function that is abs(x) for -1<= x <= 1 and repeats to create a sawtooth shaped function. With the code

def h(x):
    while x>1:
        x=x-2
    return abs(x)
h(4.3)

The Sage sell server tells me the value is 0.300000000000000, which is what I want as h(4.3)=h(2.3)=h(.3)=abs(.3) However, when I tried to plot h(x) with

def h(x):
    while x>1:
        x=x-2
    return abs(x)
plot(h(x),x,0,5)

The plot looks like abs(x), so that h(4.3) now appears to be 4.3. What has gone wrong? How can I properly plot the h(x) I want?

Plotting a periodic function

I want to plot a function that is abs(x) for -1<= x <= 1 and repeats to create a sawtooth shaped function. With the code

def h(x):
    while x>1:
        x=x-2
    return abs(x)
h(4.3)

The Sage sell server tells me the value is 0.300000000000000, which is what I want as h(4.3)=h(2.3)=h(.3)=abs(.3) However, when I tried to plot h(x) with

def h(x):
    while x>1:
        x=x-2
    return abs(x)
plot(h(x),x,0,5)

The plot looks like abs(x), so that h(4.3) now appears to be 4.3. What has gone wrong? How can I properly plot the h(x) I want?

EDIT: A points plot gives me the output I am expecting, which is different than plot(h(x),x,0,5).

def h(x):
    while x>1:
        x=x-2
    return abs(x)

 points([(x, h(x)) for x in srange(0,5,.01)], pointsize=20)

Plotting a periodic function

I want to plot a function that is abs(x) for -1<= x <= 1 and repeats to create a sawtooth shaped function. With the code

def h(x):
    while x>1:
        x=x-2
    return abs(x)
h(4.3)

The Sage sell server tells me the value is 0.300000000000000, which is what I want as h(4.3)=h(2.3)=h(.3)=abs(.3) However, when I tried to plot h(x) with

def h(x):
    while x>1:
        x=x-2
    return abs(x)
plot(h(x),x,0,5)

The plot looks like abs(x), so that h(4.3) now appears to be 4.3. What has gone wrong? How can I properly plot the h(x) I want?

EDIT: A points plot gives me the output I am expecting, which is different than plot(h(x),x,0,5).

def h(x):
    while x>1:
        x=x-2
    return abs(x)

 points([(x, h(x)) for x in srange(0,5,.01)], pointsize=20)