Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 2nd, 2012, 09:29 AM
BadutOmpong BadutOmpong is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 BadutOmpong User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #2  
Old November 2nd, 2012, 01:02 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,361 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 10 h 11 sec
Reputation Power: 383
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!

Reply With Quote
  #3  
Old November 2nd, 2012, 11:46 PM
BadutOmpong BadutOmpong is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 BadutOmpong User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 6 sec
Reputation Power: 0
wow Thx ~
it's Work ~

Reply With Quote
  #4  
Old November 3rd, 2012, 12:52 AM
BadutOmpong BadutOmpong is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 BadutOmpong User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 ~

Reply With Quote
  #5  
Old November 3rd, 2012, 07:42 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,361 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 10 h 11 sec
Reputation Power: 383
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.

Reply With Quote
  #6  
Old November 3rd, 2012, 09:38 AM
BadutOmpong BadutOmpong is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 BadutOmpong User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 6 sec
Reputation Power: 0
ahhh you right ~
i just miss typing in mw code ~

thx ~

Reply With Quote
  #7  
Old November 3rd, 2012, 10:07 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,361 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 10 h 11 sec
Reputation Power: 383
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]'

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > I Have Problem Here

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap