Coding taylor approximation of natural log

17 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Mathematics 的更多信息

产品


版本

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by