DEV Community

Bob J
Bob J

Posted on

1

Newton Method and recursion ?

So I am working on a small python program to run a Newton method recursion!

hers is my code

def newton(a,b):
return newton(a-((a**2 - b )/2*a))

but when I call it

print(newton( 5,27))

I always get this error

"PS C:\Users\Dell\Desktop\pythonstuff\MathPython> python newton.py
Traceback (most recent call last):
File "newton.py", line 23, in
print(newton( 5,27))
File "newton.py", line 11, in newton
return newton(a-((a**2 - b )/2*a))
TypeError: newton() missing 1 required positional argument: 'b'"

Top comments (2)

Collapse
 
tobiassn profile image
Tobias SN

When calling newton from inside itself, you’re only passing one parameter to it, which is a. So b doesn’t have any value.

Collapse
 
rbjassoc66 profile image
Bob J

thx :-)

current_number = 0 # number of iterations
seed = 5.5 #starting guess
numbertoFindSquareRoot = 37

def newtonMethod():
global current_number
global seed
global numbertoFindSquareRoot
# Base case
if current_number == (6): # max number of iterations
return f'{seed:0.4f}'
# Recursive case
else:
seed = seed - (seed**2 - numbertoFindSquareRoot)/(2*seed)
current_number = current_number + 1
return newtonMethod()

print(newtonMethod())

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay