November 2nd, 2012, 09:29 AM

I Have Problem Here
def fixedPoint(f, epsilon):
guess = 1.0
for i in range(100):
if f(guess)  guess < epsilon:
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?
November 2nd, 2012, 01:02 PM

Using executable Iverson notationwe know that cosine(0.74 radians) is a fixed point.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,1e8))
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!
November 2nd, 2012, 11:46 PM

November 3rd, 2012, 12:52 AM

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 fixedPoint(tryit(a), 0.0001)
that my code, where's the problem ?
thx for your help before ~
November 3rd, 2012, 07:42 AM

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!
November 3rd, 2012, 09:38 AM

ahhh you right ~
i just miss typing in mw code ~
thx ~
November 3rd, 2012, 10:07 AM

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 controlfigures 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!