Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 2nd, 2012, 10:29 AM
Registered User

Join Date: Nov 2012
Posts: 4
Time spent in forums: 35 m 6 sec
Reputation 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
November 2nd, 2012, 02:02 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,169
Time spent in forums: 1 Month 3 Weeks 2 Days 10 h 12 m 25 sec
Reputation Power: 455
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!

#3
November 3rd, 2012, 12:46 AM
Registered User

Join Date: Nov 2012
Posts: 4
Time spent in forums: 35 m 6 sec
Reputation Power: 0
wow Thx ~
it's Work ~

#4
November 3rd, 2012, 01:52 AM
Registered User

Join Date: Nov 2012
Posts: 4
Time spent in forums: 35 m 6 sec
Reputation 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 ~

#5
November 3rd, 2012, 08:42 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,169
Time spent in forums: 1 Month 3 Weeks 2 Days 10 h 12 m 25 sec
Reputation Power: 455
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.

#6
November 3rd, 2012, 10:38 AM
Registered User

Join Date: Nov 2012
Posts: 4
Time spent in forums: 35 m 6 sec
Reputation Power: 0
ahhh you right ~
i just miss typing in mw code ~

thx ~

#7
November 3rd, 2012, 11:07 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,169
Time spent in forums: 1 Month 3 Weeks 2 Days 10 h 12 m 25 sec
Reputation Power: 455

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]'

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > I Have Problem Here