Coding taylor approximation of natural log

17 vues (au cours des 30 derniers jours)
jLeo
jLeo le 11 Fév 2019
Commenté : jLeo le 11 Fév 2019
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?

Réponses (1)

RAMAKANT SHAKYA
RAMAKANT SHAKYA le 11 Fév 2019
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 commentaire
jLeo
jLeo le 11 Fév 2019
Thanks for the answer. But I was trying to calculater ln(1.9) with ln(1-x). So should my x be -0.9?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Produits


Version

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by