def sqrt(a):
def tryit(x):
return 0.5 * (a/x + x)
return fixedPoint(tryit(x), 0.0001)
Asked
Active
Viewed 930 times
-3

Tim Pietzcker
- 328,213
- 58
- 503
- 561

Rachit Upmanyu
- 1
- 1
-
def sqrt(a): def tryit(x): return 0.5 * (a/x + x) return fixedPoint(tryit(x), 0.0001) – Rachit Upmanyu Nov 03 '12 at 06:49
-
no the code has a little bug in itt. i can not add any extra line in it instead i am allowed to alter ant line in it.. the problem is in return statement.. please specify.. – Rachit Upmanyu Nov 03 '12 at 06:50
-
Is this an assignment you've got that you're now asking us to solve for you? Or is this your code and it's not working as expected? – Tim Pietzcker Nov 03 '12 at 06:54
2 Answers
0
There are a couple of problems:
- You're calling
fixedPoint()
- what's that? Is this defined somewhere else? - You're using
x
in the function without having defined it. - You're calling the function
sqrt()
, but the logic isn't anywhere near something that would calculate the square root of anything.
There is at least one inelegance:
- You're defining a nested function (
tryit()
). This is not a problem per se, but it doesn't make much sense, and you're relying on a local variablea
being defined here instead of passing that variable to a stand-alone function defined on the module level. It's hard to tell what that function is supposed to be doing, though. tryit()
isn't a good function name.

Tim Pietzcker
- 328,213
- 58
- 503
- 561
0
See Solve this equation with fixed point iteration for working examples of fixed point iteration.
The code in the present question misspells fixed_point
, a function that needs to be imported from scipy.optimize
. The required arguments of fixed_point
include a function and an initial value; for example:
scipy.optimize.fixed_point(tryit, a/2)

Community
- 1
- 1

James Waldby - jwpat7
- 8,593
- 2
- 22
- 37