kal
kal

Reputation: 37

Solving for the square root by Newton's Method

enter image description here


yinitial = x

y_n approaches sqrt(x) as n->infinity

If theres an x input and tol input. Aslong as the |y^2-x| > tol is true compute the following equation of y=0.5*(y + x/y). How would I create a while loop that will stop when |y^2-x| <= tol. So every time through the loop the y value changes. In order to get this answer--->

>>sqrtx = sqRoot(25,100)

sqrtx =
 7.4615

I wrote this so far:

function [sqrtx] = sqrRoot(x,tol)

n = 0;
x=0;%initialized variables

if x >=tol %skips all remaining code

   return

end

while x <=tol

    %code repeated during each loop

   x = x+1  %counting code

end

Upvotes: 0

Views: 4798

Answers (3)

kal
kal

Reputation: 37

The correct way to solve this..

function [sqrtx] = sqRoot(x,tol)

sqrtx = x;%output = x

while abs((sqrtx.^2) - x) > tol %logic expression to test when it should

end

sqrtx = 0.5*((sqrtx) + (x/sqrtx)); %while condition prove true calculate

end

end

Upvotes: -1

Wauzl
Wauzl

Reputation: 971

Yes, that is exactly what you are supposed to do: Iterate using the equation for y_{n+1} over and over again.

In your code you should have a loop like

while abs(y^2 - x) > tol
   %// Calculate new y from the formula
end

Also note that tol should be small, as told in the other answer. The parameter tol actually tells you how inaccurate you want your solution to be. Normally you want more or less accurate solutions, so you set tol to a value near zero.

Upvotes: 2

rayryeng
rayryeng

Reputation: 104464

That formula is using a modified version of Newton's method to determine the square root. y_n is the previous iteration and y_{n+1} is the current iteration. You just need to keep two variables for each, then when the criteria of tolerance is satisfied, you return the current iteration's output. You also are incrementing the wrong value. It should be n, not x. You also aren't computing the tolerance properly... read the question more carefully. You take the current iteration's output, square it, subtract with the desired value x, take the absolute value and see if the output is less than the tolerance.

Also, you need to make sure the tolerance is small. Specifying the tolerance to be 100 will probably not allow the algorithm to iterate and give you the right answer. It may also be useful to see how long it took to converge to the right answer. As such, return n as a second output to your function:

function [sqrtx,n] = sqrRoot(x,tol) %// Change

%// Counts total number of iterations
n = 0;    

%// Initialize the previous and current value to the input
sqrtx = x;
sqrtx_prev = x;

%// Until the tolerance has been met...
while abs(sqrtx^2 - x) > tol

    %// Compute the next guess of the square root
    sqrtx = 0.5*(sqrtx_prev + (x/sqrtx_prev));

    %// Increment the counter
    n = n + 1;

    %// Set for next iteration
    sqrtx_prev = sqrtx;    
end

Now, when I run this code with x=25 and tol=1e-10, I get this:

>> [sqrtx, n] = sqrRoot(25, 1e-10)

sqrtx =

     5

n =

     7

The square root of 25 is 5... at least that's what I remember from maths class back in the day. It also took 7 iterations to converge. Not bad.

Upvotes: 4

Related Questions