Ask Your Question

Revision history [back]

First, your indentation is messed up, but that's probably just a formatting problem in your question.

The real issue is that when cantor(x,0) is evaluated, x refers to the symbolic variable, so all of your conditions like z < 0 are False (Sage can't prove they are True because it doesn't know anything specific about x) and execution falls through to the default val = 1. That's why you get a horizontal line at $y=1$.

If you replace the expression cantor(x,0) with an anonymous function that delays evaluation of cantor you'll get what you expect:

def cantor(z,iter=0):
    #'z' is the x value
    #'iter' is the number of iterations in the iterative process
    if iter == 0:
        val = z
    elif z<0:
        val = 0
    elif z<(1/3):
        val = (1/2)*cantor(3*z,iter-1)
    elif z<=(2/3):
        val = (1/2)
    elif z<=1:
        val = (1/2)+(1/2)*cantor(3*z-2,iter-1)
    else:
        val = 1
    return val

plot(lambda x: cantor(x,1))

See http://aleph.sagemath.org/?q=79243f15-71df-49d8-82c8-b174ce50101f&lang=sage