Processing math: 100%
Ask Your Question
1

defining a recursive function

asked 2 years ago

R. Bhaskar gravatar image

updated 2 years ago

Max Alekseyev gravatar image

I need to define the function w(x,n). x is real (0<x<1), N is an integer. w(x,1)=xx; w(x,n)=xw(x,n1)

I tried the following, the function definition didn't send an error message but the plot call did and failed:

def w(x,n):
    if n==1:
        return x^x
    else: 
        return x^(w(x,n-1))

plot (w(x,n), (x, 0.01, 0.99) (n, 1, 10))
Preview: (hide)

1 Answer

Sort by » oldest newest most voted
0

answered 2 years ago

Max Alekseyev gravatar image

There are two issues here. First, n cannot have a continuous range as the function defined only for integer n. Second, plugging in a particular value of n needs be done via functools.partial. Here is a working example:

import functools

def w(x,n):
    if n==1:
        return x^x
    else: 
        return x^(w(x,n-1))

plot([functools.partial(w,n=n0) for n0 in (1..10)], (x, 0.01, 0.99))
Preview: (hide)
link

Comments

beautiful, thank you

aszanti gravatar imageaszanti ( 2 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2 years ago

Seen: 229 times

Last updated: Feb 22 '23