# Parametric plot with piecewise input

I'm trying to plot a parametric function, which is defined piecewise. For some reason, the plot just jumps to the last piece of the parametric function and plots that.

Here's what I want to plot.

t = var('t')
r = 2

def f(x):
if 0 <=x <=r:
return (x, -r)
elif r<x<=r + pi*r:
return (cos(x-r), -(r + sin(x-r)))
elif r + pi*r < x <= 3*r + pi*r:
return (-x + 2*r + pi*r, r)
elif 3*r + pi*r < x <= 3*r + 2*pi*r:
return (cos(x - 3*r), -(r + sin(x-r)))
else:
return (x - 4*r - 2*pi*r, -r)

parametric_plot(f(t), (t, 0, 4*r + 2*pi*r))


It just returns a straight line of length 4r + 2pi*r with y coordinate -r

edit retag close merge delete

Sort by » oldest newest most voted

It's because in parametric_plot(f(t), (t, 0, 4*r + 2*pi*r)), the Python function f is evaluated with the symbolic variable t as an input, prior to giving any concrete value to t:

sage: f(t)
(-4*pi + t - 8, -2)


To get the expected plot, you must split f in two by defining

sage: def f0(x): return f(x)
sage: def f1(x): return f(x)


Then

sage: parametric_plot([f0, f1], (t, 0, 4*r + 2*pi*r))


gives the plot that you want. Note that in the above command, one must write [f0,f1] and not [f0(t),f1(t)]: the latter would reproduce the incorrect behavior, by first evaluating f0 and f1 on the symbolic variable t.

more