#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    I Have Problem Here


    def fixedPoint(f, epsilon):
    guess = 1.0
    for i in range(100):
    if f(guess) - guess < epsilon:

    return guess
    else:
    guess = f(guess)
    return guess



    f: a function of one argument that returns a float, epsilon: a small float
    returns the best guess when that guess is less than epsilon
    away from f(guess) or after 100 trials, whichever comes first.

    something wrong in my code?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Using executable Iverson notationwe know that cosine(0.74 radians) is a fixed point.
    Code:
       2 o.^:_]1
    0.739085
    I optimized your python code a little bit to avoid calling the function twice per iteration, and took the absolute value of the difference, inserted my test problem, and now it works. Look up a reference on absolute and relative error.

    Code:
    def fixedPoint(f, epsilon):
        guess = 1.0
        for i in range(100):
            F = f(guess)
            if abs(F - guess) < epsilon:
                return guess
            else:
                guess = F
        return guess
    
    
    import math
    
    print(fixedPoint(math.cos,1e-8))



    Explanation of the j sentence:

    o. are the circle functions, 1&o. is sine --- notice that sine is an odd function and 1 is an odd number, 2&o. is cosine, (yes, 3 o. is tangent) while the power conjunction ^: looks similar to a power operator, but is generalized to evaluating an arbitrary function, and the _ means to repeat infinitely many times, or until convergence.) The ] serves to separate _ from 1 which would other wise be a vector but I'm not going to explain that here. 1 is the starting angle.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    wow Thx ~
    it's Work ~
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    ck, i get a problem again ~

    i want to compute square roots with fixedPoint. the square root is 'a', and the function is = 0.5 * (a/x + x).

    def sqrt(a):
    def tryit(x):
    return 0.5 * (a/x + x)
    return fixedPoint(tryit(a), 0.0001)

    that my code, where's the problem ?

    thx for your help before ~
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Code:
    def sqrt(a):
        def tryit(x):
            return 0.5 * (a/x + x)
        return fixedPoint(tryit, 0.0001)
    Before you had tryit(a). You need to pass the function, not the function result to fixedPoint.

    If your program still fails it because it doesn't know a value for `a' the reason may be that you have an old python version, in which case you'd need another means to pass the value of `a'. Scope rules have changed.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    ahhh you right ~
    i just miss typing in mw code ~

    thx ~
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    What, please, is "mw code"?


    Loving, caring message from emacs:
    `Buffer gs.log has shrunk a lot; auto save disabled in that buffer until next real save'

    Autosave: never lose data.

    Astounding undo facility.

    Built in calculus aware symbolic calculator.

    Integrated with version control---figures out which vcs you're using.
    debugger, language environment

    Extensive help system.

    Intelligent event log: `Making completion list... [2 times]'
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo