I am working on code that uses loops and Heron's method to find an estimate of the square root, while also displaying the iteration and relative change.
I completed a function that uses for loops to achieve this:
def square_root_for(a, x0, max_iter = 10, tol=1e-14):
""" (number, integer, number) -> float
Return an estimate of the square root of a number using the Heron's method.
>>> square_root_for(5, 5)
Iteration | Estimate | Relative Change
-------------------------------------------------
1 | 3.00000000000000 | 0.4000000000000000
2 | 2.33333333333333 | 0.2222222222222222
3 | 2.23809523809524 | 0.0408163265306123
4 | 2.23606889564336 | 0.0009053870529653
5 | 2.23606797749998 | 0.0000004106060359
6 | 2.23606797749979 | 0.0000000000000842
7 | 2.23606797749979 | 0.0000000000000000
2.23606797749979
"""
x = [x0]
x.append(a/x0)
print('Iteration | Estimate | Relative Change')
print('-------------------------------------------------')
for i in range(1,(max_iter 1),1):
change = (abs(x[i] - x[i-1]) / x[i-1])
if change > tol:
x.append(1/2 * (x[i] (a / x[i])))
else:
break
print('{} | {:.14f} | {:.16f}'.format(i, x[i], change))
print(x[i])
Next I need to create a function that uses while loops. So far I have:
def square_root_while(a, x0, tol=1e-14):
""" (number, number, number) -> float
Return an estimate of the square root of a number using the Heron's method.
>>> square_root_while(5, 5)
Iteration | Estimate | Relative Change
-------------------------------------------------
1 | 3.00000000000000 | 0.4000000000000000
2 | 2.33333333333333 | 0.2222222222222222
3 | 2.23809523809524 | 0.0408163265306123
4 | 2.23606889564336 | 0.0009053870529653
5 | 2.23606797749998 | 0.0000004106060359
6 | 2.23606797749979 | 0.0000000000000842
7 | 2.23606797749979 | 0.0000000000000000
2.23606797749979
"""
x = [x0]
x.append(a/x0)
print('Iteration | Estimate | Relative Change')
print('-------------------------------------------------')
i = range(1,(max_iter 1),1)
change = (abs(x[i] - x[i-1]) / x[i-1])
while change < tol:
x.append(1/2 * (x[i] (a / x[i])))
i = i 1
if change > tol:
break
print('{} | {:.14f} | {:.16f}'.format(i, x[i], change))
But this is pretty much completely wrong. I am very confused with how to do this. I don't understand how I could convert this for loop with a range into a while loop. Any tips are appreciated, Thank you!
CodePudding user response:
As in this way? range(start, stop, step)
is just creating a sequence. You can split it into add/increment instructions:
From this:
for i in range(start, stop, step):
...
To this:
i = start
while start < stop:
...
i = step
And applied for your loop:
i = 1
while i < max_iter 1:
change = (abs(x[i] - x[i-1]) / x[i-1])
if change > tol:
x.append(1/2 * (x[i] (a / x[i])))
else:
break
print('{} | {:.14f} | {:.16f}'.format(i, x[i], change))
i = 1
CodePudding user response:
These two loops are equivalent:
for i in range(N):
# do the work
i = 0
while i < N:
# do the work
i = 1
CodePudding user response:
In a for loop you got initial value, max value, step.
for i in range(initial, maxValue, step):
On a while loop you have to define that behavior.
value = 1 # initial value
maxValue= max_iter 1 # max value
while value < maxValue:
#do your thing
value = value 1 //step