Root finder matlab code is not displaying result

I wrote this code below. It works without errors. However I can't see the result.
It finds the nth root of w, starting from guess g.
w=9;
g=5;
n=2;
function result=findRoot(w,g,n)
if (abs(newGuess-g)<(g*0.0001))
result = newGuess;
disp(result);
else
newGuess = (g - g^n - w) / (n*g^(n-1));
result = findRoot(w, newGuess,n);
disp(result);
end
end
I appreciate any help. Thank you.

2 Kommentare

One problem is that you are calling your function from within the function:
result = findRoot(w, newGuess,n);
This will lead to ‘infinite recursion’, and so no output.
Ceyda Elcin Kaya
Ceyda Elcin Kaya am 10 Feb. 2019
Bearbeitet: Ceyda Elcin Kaya am 10 Feb. 2019
Out of memory. The likely cause is an infinite recursion within the program.
yep the exact problem I'm facing with right now. I will try to find a solution.
edit: i made it i guess. only problem left is that now it displays infinetely
w=9;
g=5;
n=2;
findRoot(w,g,n);
function [result]=findRoot(w,g,n)
newGuess=0;
while(abs(newGuess-g)>g*0.0001)
newGuess = (g - g^n - w) / (n*g^(n-1));
%result = findRoot(w, newGuess,n);
disp(newGuess)
end
result = newGuess;
disp(newGuess);
end

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Anand Shirke
Anand Shirke am 10 Feb. 2019

0 Stimmen

Maybe the problem is while declaring the function, I would suggest you to declare the function in this way
function [result]=findRoot(w,g,n)
..
.....
end
After saving the code, type in command window :
findRoot(9,5,2)

1 Kommentar

I did what you say. Now I'm getting a new error.
Function with duplicate name "findRoot" cannot be defined.
But my function is defined.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2018b

Gefragt:

am 10 Feb. 2019

Bearbeitet:

am 10 Feb. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by