While Loop Problem Displaying NaN

So I am trying to make a while loop which finds the value of log(x+1) using a taylor series. I am having the series run until the answer comes into a certain precision. When I run the function with x=1 I get the correct out for both y and i. However if I try it with any greater number the y value displays as NaN. Can anyone help me figure out how to fix this.
function logoneplusx(x)
format long
y=0;i=1;Err=1;
while Err>0.001
y=((-1)^(i-1))*((x^i)/i)+y;
i=i+1;
Err=abs(y-log(x+1));
end
disp(y)
disp(i)

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 15 日
編集済み: Azzi Abdelmalek 2014 年 7 月 15 日

0 投票

When x is bigger, y becomes infinite, and after you have Err=(inf-inf)=NaN. Matlab or another software has its limits.

1 件のコメント

Dan
Dan 2014 年 7 月 15 日
but ln(5)=1.6... this is not even close to inf

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

James Tursa
James Tursa 2014 年 7 月 15 日

0 投票

If you check this link, you will see that the series you are using only converges for abs(x) <= 1 and x not equal to -1:
If you want to use an x value outside this convergence range, use a different series. On this same link there is another series you can use for other values of x.

カテゴリ

ヘルプ センター および File ExchangeElementary Math についてさらに検索

タグ

質問済み:

Dan
2014 年 7 月 15 日

回答済み:

2014 年 7 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by