Ask Your Question

Revision history [back]

Instead of plot(h(x), x, -5, 5), try plot(h, (-5, 5)).

Note: the function may not be what you want for negative x.

Here is another option defining h as a symbolic function without a while loop.

In this case, both plot(h, (-5, 5)) and plot(h(x), (-5, 5)) give a correct plot.

sage: h(x) = x - 2 * floor((x + 1) / 2)
sage: plot(h, (-5, 5), aspect_ratio=1)

Instead of plot(h(x), x, -5, 5), try plot(h, (-5, 5)).

Note: the function may not be what you want for negative x.

Here is another option defining h as a symbolic function without a while loop.

In this case, both plot(h, (-5, 5)) and plot(h(x), (-5, 5)) give a correct plot.

sage: h(x) = x abs(x - 2 * floor((x + 1) / 2)
2))
sage: plot(h, (-5, 5), aspect_ratio=1)

Instead of plot(h(x), x, -5, 5), try plot(h, (-5, 5)).

Note: the function may not be what you want for negative x.

Here is another option defining One could also define h as a symbolic function without a while loop.

Here are two examples of doing that.

Using the floor function:

sage: h(x) = abs(x - 2 * floor((x + 1) / 2))
sage: plot(h, (-5, 5), aspect_ratio=1)
sage: plot(h(x), (-5, 5), aspect_ratio=1)

Using trigonometric functions:

sage: h(x) = arccos(cos(x*pi))/pi
sage: plot(h, (-5, 5), aspect_ratio=1)
sage: plot(h(x), (-5, 5), aspect_ratio=1)

In this case, both plot(h, (-5, 5)) and plot(h(x), (-5, 5)) give a correct plot.

sage: h(x) = abs(x - 2 * floor((x + 1) / 2))
sage: plot(h, (-5, 5), aspect_ratio=1)