Plotting piecewise constant function in Sage gives "no way to make fast_float from None" error

210 Views Asked by At

I need to plot following piecewise constant function is Sage:

def f(t):
    T = 2.0 * pi

    if t > 0 and t < T / 2.0:
        return 1.0
    elif t == T / 2.0:
        return 0
    elif t > T / 2.0 and t < T:
        return -1.0 

t = var('t')        
p = plot(f(t), (t, 0, 10))
show(p)

But show(p) gives an error:

no way to make fast_float from None

The problem is that there is no default returned value in f(t), so the function returns None if t <= 0 or t >= T.

The original formula is this one and I can't modify it (so I can't add some default value): $$ f(t)=\begin{cases}1, & 0<t<T/2,\\0, & t=T/2,\\-1, & T/2<t<T\end{cases} $$

Can somebody help to plot this function? Seems like I need to draw it only in 0 < t < T interval, but I am out of ideas how to do it.

1

There are 1 best solutions below

0
On BEST ANSWER

I've found a solution here: https://stackoverflow.com/questions/28661269/python-no-way-to-make-fast-float-from-none-error/28661486#28661486

I need to leave the t and plot just f function without a parameter. Also min and max numbers should be in the range of 0 <= t < T:

p = plot(f, (0, 6))
show(p)