Ask Your Question
1

defining a recursive function

asked 2023-02-16 17:47:24 +0200

R. Bhaskar gravatar image

updated 2023-02-22 16:32:24 +0200

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) = x^x$; $w(x,n) = x^{w(x,n-1)}$

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

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-02-22 16:47:33 +0200

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))
edit flag offensive delete link more

Comments

beautiful, thank you

aszanti gravatar imageaszanti ( 2023-02-25 18:29:29 +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

1 follower

Stats

Asked: 2023-02-16 17:47:24 +0200

Seen: 168 times

Last updated: Feb 22 '23