March 28, 2024, 12:54:40 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Square Root by Iteration Method

Started by GWS, December 04, 2019, 01:08:50 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

GWS

Here's another oldie which is probably not really needed these days. :)

We have an inbuilt SQRT() function which is probably faster and more accurate.

However, it's interesting to see how the iteration is used.

Note that it needs Double precision to work correctly ..

' Square Root by Iteration ..
openconsole
cls
' Calculating a Square Root using Iteration ..

def x,y,z:double
def i:int

setprecision 10

print

' Example:
z = 27 :' positive real number whose Square root is required

print
print "Square Root of ",z:print:print STRING$(30, "_" )
print:print:print "Iterations: ":print

x = z/2 :' arbitrary start value for the iteration

do
x = 0.5 * (x + z/x)
print x
until ((x*x - z)/z <= 1.e-11) :' the test for convergence

print
print "The Square Root of ",z, " is  ", x

' Compare with the inbuilt Sqrt() function ..
print:print
print "Using the CB Sqrt() Function, the result is .. ":print
print sqrt(z)
print:print STRING$(30, "_" )

do:until inkey$<>""
closeconsole
end


Best wishes, :)

Graham

Tomorrow may be too late ..