Coding taylor approximation of natural log

조회 수: 17 (최근 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 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