Help, secant method by recursion
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Sun Woo Kim
am 14 Okt. 2017
Beantwortet: Kinza Shahzad
am 1 Jun. 2018
I'm trying to write a function that uses the secant method to find a root of the function.
I'm required to write both the iterative and recursive method.
So far, I finished writing the iterative but cannot finish the recursive.
The iterative is
function root = secanti(n, x0, x1, err)
while true
xi = (x0 * feval(n,x1) - x1 * feval(n, x0))/(feval(n,x1) - feval(n,x0));
if abs((xi-x1)/xi) < err
root = xi;
break
else
x0 = x1;
x1 = xi;
end
end
end
The one I have for recursive so far is
function xi = secantr(n, x0, x1, err)
xi = (x0 * feval(n,x1) - x1 * feval(n, x0))/(feval(n,x1) - feval(n,x0));
if abs((xi-x1)/xi) < err
return
end
x0 = x1;
x1 = xi;
secantr(n,x0,x1,err)
end
The recursive function keeps giving a different value from the iterative.
Is there any way to improve my recursive function? Thank you
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Software Development Tools finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!