The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
I Have Problem Here
Discuss I Have Problem Here in the Python Programming forum on Dev Shed. I Have Problem Here Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 2nd, 2012, 09: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?
|

November 2nd, 2012, 01:02 PM
|
 |
Contributing User
|
|
|
|
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,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!
|

November 2nd, 2012, 11:46 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 4
Time spent in forums: 35 m 6 sec
Reputation Power: 0
|
|
|
wow Thx ~
it's Work ~
|

November 3rd, 2012, 12: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 ~
|

November 3rd, 2012, 07:42 AM
|
 |
Contributing User
|
|
|
|
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.
|

November 3rd, 2012, 09: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 ~
|

November 3rd, 2012, 10:07 AM
|
 |
Contributing User
|
|
|
|
|
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]'
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|