My loop has an infinite recursion, and I'm not sure how to fix it?

I'm trying to implement the Babylonian method through a while loop. When I call my function in another script file, it says I have an infinite recursion. I think it has something to do with my boundary variable. I'm not quite sure how to fix it.
function y = divide_and_average(initialGuess,a,limit)
y = (initialGuess + a/(initialGuess))/2; %calculation for square
boundary = (abs(y-initialGuess)/y); % compare the limit to
while boundary > limit %will continue until false
y = initialGuess;
divide_and_average(y,a,limit);
boundary = (abs(y-initialGuess)/y);
end

Respuestas (1)

You always set y to be the initial guess and call divide_and_average with y which is the initial guess.
while boundary > limit %will continue until false
y = initialGuess; % y is set to your initialGuess
% Now we call this function with the exact inputs it was originally called with. This happens infinitely.
divide_and_average(y,a,limit);
boundary = (abs(y-initialGuess)/y);
end

5 comentarios

It seems you likely want to remove the line,
y = initialGuess
and rather you want the next call to happen with:
y = (initialGuess + a/(initialGuess))/2;
So I made some modifications to my code:
function y = divide_and_average(initialGuess,a,limit)
y = (initialGuess + a/(initialGuess))/2; %calculation for square
boundary = (abs(y-initialGuess)/y); % compare the limit to
square = 0;
while boundary > limit %will continue until false
square = divide_and_average(y,a,limit);
y = square; %%replacing y value with square to converge to root
boundary = (abs(y-initialGuess)/y); %%using changed y value
end
display(square);
however, the while loop doesn't stop. I think this has something to do with my boundary variable? Does it not change in the loop?
So I actually fixed my boundary. However, my condition isn't fulfilled.
function y = divide_and_average(initialGuess,a)
y = (initialGuess + a/(initialGuess))/2; %calculation for square
boundary = (abs(y-initialGuess)/y); % compare the limit to
square = 1;
while boundary >= 10^-6 %will continue until false
square = y; %%calls function with y as initial guess
boundary = (abs(square-initialGuess)/square); %%changes boundary until it is less than the limit
initialGuess = square; %%replaces initialGuess with value calculated above to converge to root
end
display(square);
the answer I get when I call my function divide_and_average(3.9,16) is 4.0013, which isn't within 10^-6 of the actual square root of 16
In your while loop square takes on the value of y on every iteration but y does not change. Also, your calculation of the boundary is not correct.
function [y,error] = divide_and_average(initialGuess,a,limit)
% Calculate square based on initial guess, this is our new guess
y = (initialGuess + a/(initialGuess))/2;
error = (a - y^2)/(2*y); % This is the approximate error in the answer
while abs(error) >= limit %will continue until false
% Call the function with our new "initialGuess" and get the
% corresponding error.
[y,error] = divide_and_average(y,a,limit);
end
This is now within the appropriate tolerance:
format long
[square,err] = divide_and_average(3.9,16,1e-6)
square =
4.000000205391106
err =
-2.053911006834815e-07
I highly suggest taking a look at the MATLAB Debugging options which will allow you to determine where error in your code are.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 2 de Feb. de 2016

Comentada:

el 2 de Feb. de 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by