A very old method to compute squareroot s is the \"divide and average\" method.
ID: 3870260 • Letter: A
Question
A very old method to compute squareroot s is the "divide and average" method. It is an iterative technique that works as follows. Suppose you want to find the squareroot of a. Make a guess, call it x. Then get better and better approximations using the iterative formula: x_new = x_old + a/x_old/2 We continue finding new values of x until it converges to a single number, which should be squareroot a. For example. Suppose you want to find the squareroot of 3, Make a guess, say x = 1.5. Then a better approximation is x_new = 1.5 + 3/1.5/2 = 1.75 Now repeat the process with this new value of x: x_new = 1.75 + 3/1.75/2 = 1.73214 and continue until the numbers don't change too much. The next value we get would be x = 1.73205. Notice that the first four digits have not changed. We can guess that squareroot 3 almostequalto 1.732. In fact, this is the correct value to 4 significant figures. Your Problem: Write a function (or a subroutine) in VBA that evaluates the squareroot of a using the above iterative technique (you can do this by modifying the code in figure 3.4 of your book). Make your first guess a/2 and choose epsilon_s so that the solution is accurate to at least 5 significant figures. Then use your function to approximate squareroot 27.Explanation / Answer
Given below is the code for the question. The questions asks to modify the code in fig 3.4 of book, but the code is not given in the question. So giving you the code to use the formula given in question. Please make any changes to match with the fig 3.4 in book.
Hope the answer helped. If it did, please don't forget to rate the answer. Thank you very much.
Sub Main()
Console.WriteLine("MySqrt(27) = " & MySqrt(27))
End Sub
Public Function MySqrt(a As Double) As Double
Dim xold As Double
Dim xnew As Double
Dim err As Double
Dim diff As Double
xold = a / 2
err = 0.00001
diff = 1 'Initial value keep it high so that loop executes
While diff > err
xnew = (xold + a /xold) / 2
diff = xnew - xold
if(diff < 0)
diff = - diff
end if
xold = xnew
End While
MySqrt = xnew
End Function
output
MySqrt(27) = 5.19615242270663
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.