Coding taylor approximation of natural log

5 ビュー (過去 30 日間)
jLeo
jLeo 2019 年 2 月 11 日
コメント済み: jLeo 2019 年 2 月 11 日
Hi all,
I need to approximate ln(1.9) using a taylor series ln(1-x)=Sum(-(x^k)/k,k,1,inf). I have to code a script to figure out 1) how many terms I need, 2) the value from the series, and 3) the error. Here is what I have so far. I tried to run it, but it seems to be not ending.
x=0.9;
target_equation = log(1-x);
series_sum = 0;
difference = abs(target_equation - series_sum);
threshold = 1*10^-10;
count = 0;
while difference > threshold;
count=count+1;
series_sum=series_sum + ((x^count)/count);
difference = abs(target_equation - series_sum);
end
disp(series_sum);
I'm not sure what is wrong with my code. Any suggestion?

回答 (1 件)

RAMAKANT SHAKYA
RAMAKANT SHAKYA 2019 年 2 月 11 日
if you want to calculate log(1.9) and x=0.9 then you have apply taylor series log(1+x) see formula form google and change in to the code is
function series_sum=talor(x) %give x=0.9 as input
target_equation = log(1+x); % for calculating log(1.9)
series_sum = 0;
difference = abs(target_equation - series_sum);
threshold = 1*10^-10;
count = 0;
while difference > threshold;
count=count+1;
series_sum=series_sum + ((-1)^(count+1)*(x^count)/count); %as per formula of taylor series the even term cofficients are negative
difference = abs(target_equation - series_sum);
end
disp(series_sum);
  1 件のコメント
jLeo
jLeo 2019 年 2 月 11 日
Thanks for the answer. But I was trying to calculater ln(1.9) with ln(1-x). So should my x be -0.9?

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

カテゴリ

Help Center および File ExchangeMathematics and Optimization についてさらに検索

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by