Ask Your Question

Parametric plot with piecewise input

asked 2016-11-03 20:33:56 +0200

jford1906 gravatar image

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)))
        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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-11-03 22:35:00 +0200

eric_g gravatar image

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)[0]
sage: def f1(x): return f(x)[1]


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.

edit flag offensive delete link more


Works, thanks!

jford1906 gravatar imagejford1906 ( 2016-11-12 20:01:37 +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


Asked: 2016-11-03 20:33:56 +0200

Seen: 337 times

Last updated: Nov 03 '16