Help, secant method by recursion

2 ビュー (過去 30 日間)
Sun Woo Kim
Sun Woo Kim 2017 年 10 月 14 日
回答済み: Kinza Shahzad 2018 年 6 月 1 日
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

採用された回答

Jan
Jan 2017 年 10 月 14 日
All you need is to catch the output:
xi = secantr(n,x0,x1,err);
^^^^
  1 件のコメント
Sun Woo Kim
Sun Woo Kim 2017 年 10 月 15 日
Thanks. I feel so dumb

サインインしてコメントする。

その他の回答 (1 件)

Kinza Shahzad
Kinza Shahzad 2018 年 6 月 1 日
how to run these codes ? :/m having some errors

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by