Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Plotting a recursive function

I'm trying to plot different iterations of the Cantor function using the iterative definition:

Let ƒ0(x) = x.

Then,

Let ƒn+1(x) = 1/2 × ƒn(3x), when 0 ? x < 1/3?;

Let ƒn+1(x) = 1/2, when 1/3 ? x < 2/3?;

Let ƒn+1(x) = 1/2 + 1/2 × ƒn(3?x ? 2), when 2/3 ? x ? 1.

I extended it to be 0 when x<0 and 1 when x>1.

The function I have defined returns the correct values for a specific iteration and a specific x, but it is not plotting how I would like it to. When I plot it, I just get a horizontal line at 1.

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(cantor(x,0)) #this works
plot(cantor(x,1)) #this doesn't work

Thanks in advance for any help.

Plotting a recursive function

I'm trying to plot different iterations of the Cantor function using the iterative definition:

Let ƒ0(x) = x.

Then,

Let ƒn+1(x) = 1/2 × ƒn(3x), when 0 ? <= x < 1/3?;

Let ƒn+1(x) = 1/2, when 1/3 ? <= x < 2/3?;

Let ƒn+1(x) = 1/2 + 1/2 × ƒn(3?x ? ƒn(3x - 2), when 2/3 ? x ? <= x <= 1.

I extended it to be 0 when x<0 and 1 when x>1.

The function I have defined returns the correct values for a specific iteration and a specific x, but it is not plotting how I would like it to. When I plot it, I just get a horizontal line at 1.

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(cantor(x,0)) #this works
plot(cantor(x,1)) #this doesn't work

Thanks in advance for any help.

Plotting a recursive function

I'm trying to plot different iterations of the Cantor function using the iterative definition:

Let ƒ0(x) = x.

Then,

Let ƒn+1(x) = 1/2 × ƒn(3x), 1/2*ƒn(3x), when 0 <= x < 1/3?;1/3;

Let ƒn+1(x) = 1/2, when 1/3 <= x < 2/3?;2/3;

Let ƒn+1(x) = 1/2 + 1/2 × ƒn(3x 1/2*ƒn(3x - 2), when 2/3 <= x <= 1.

I extended it to be 0 when x<0 and 1 when x>1.

The function I have defined returns the correct values for a specific iteration and a specific x, but it is not plotting how I would like it to. When I plot it, I just get a horizontal line at 1.

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(cantor(x,0)) #this works
plot(cantor(x,1)) #this doesn't work

Thanks in advance for any help.

Plotting a recursive function

I'm trying to plot different iterations of the Cantor function using the iterative definition:

Let ƒ0(x) = x.

Then,

Let ƒn+1(x) = 1/2*ƒn(3x), when 0 <= x < 1/3;

Let ƒn+1(x) = 1/2, when 1/3 <= x < 2/3;

Let ƒn+1(x) = 1/2 + 1/2*ƒn(3x - 2), when 2/3 <= x <= 1.

I extended it to be 0 when x<0 and 1 when x>1.

The function I have defined returns the correct values for a specific iteration and a specific x, but it is not plotting how I would like it to. When I plot it, I just get a horizontal line at 1.

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(cantor(x,0)) #this works
plot(cantor(x,1)) #this doesn't work

Thanks in advance for any help.