Find the root of f(x)=x^2 – 50 using a while loop

91 views Asked by At

I have this homework. But i don't know how to do it.

We did it with a for loop.

f=lambda x: (x**2)-27
fd=lambda x:x*2

a=50
for i in range(1,100):
    a = a - (f(a)/fd(a))
    print(i, ". root in step - ", a)

I think we are asked to turn this into a while loop and do it.

1

There are 1 answers

0
blhsing On

The while loop should keep going until the delta between each step is smaller than the precision of a floating point number:

a = 50
while a > (a := a - f(a) / fd(a)):
    print(a)

This outputs:

25.27
13.169230312623665
7.609732014284865
5.578910083156939
5.2092825345429645
5.196168970075266
5.19615242273298
5.196152422706632

Demo: https://ideone.com/nAmHYw