Taylor series for ln(x)?

조회 수: 13 (최근 30일)
Karan Sandhu
Karan Sandhu 2016년 2월 7일
댓글: Walter Roberson 2017년 2월 4일
Hi, I am trying to use the formula for the Taylor series of ln(x) in order to solve for the value of 'e'. The answer is supposed to be one, but I am getting a huge number instead. I am having no luck in debugging this - perhaps my formula for the taylor series of ln(x) is wrong? Here is my code:
SumN=0; % initialize SumN
sign=-1; % variable that assigns a sign to a term
x= exp(1);
for N=1:1000
sign=-1*sign;
SumN=SumN + ((sign)^N*(x-1)^N)/(N);
end

채택된 답변

John D'Errico
John D'Errico 2016년 2월 7일
편집: John D'Errico 2016년 2월 7일
As a hint, I might wonder if your instructor expected this problem, perhaps this is why the problem was assigned?
Your formula for the series seems at a quick glance correct. However, you might consider if that series is convergent for x = 2.71828...
That is, will terms that look like
(1.718...)^n/n
ever approach zero? Or, is it possible that those terms do blow up, as you have seen? What does that tell you about the values of x for which this series will be convergent?
Note that there are ways to repair this problem, most notably via transformations. For example, a simple idea is if x > 1, then what is the value of -ln(x)? Consider the transformation
u = 1/x
ln(x) = -ln(u)
Now the terms in your series will look like
(-0.63212)^n/n
Clearly they will approach zero. I'm too lazy to write a loop, so consider this:
N = (1:25)';
S = cumsum((mod(N,2)*2-1).*(u-1).^N./N)
S =
-0.63212
-0.83191
-0.9161
-0.95602
-0.9762
-0.98684
-0.9926
-0.99578
-0.99757
-0.99859
-0.99918
-0.99952
-0.99971
-0.99983
-0.9999
-0.99994
-0.99996
-0.99998
-0.99999
-0.99999
-1
-1
-1
-1
-1
When you negate the above result, that simply recovers our hoped for value for log(exp(1)).
So when you see a problem with a series like this, consider not only if your formula is correct, but if there are other considerations one must satisfy for that formula to be of any use. Here, convergence radius is what matters.
  댓글 수: 1
Karan Sandhu
Karan Sandhu 2016년 2월 7일
편집: Karan Sandhu 2016년 3월 3일
Thank you sir, the program worked like a charm.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bijaya Nepal
Bijaya Nepal 2017년 2월 4일
편집: Walter Roberson 2017년 2월 4일
The value of ln (x) for any x in the range of 0 < x <= 2 can be estimated using the Taylor series as shown below. As more terms are added the results should get better.
lnx=(x-1)-(x-1)^2/2+(x-1)^3/3-(x-1)^4/4....
Write a script file that takes as user inputs the value of x (between 0 and 2) and the number of terms to use N. The program should then compute the estimate of ln(x) using the Taylor series. The program should output the estimate, the actual value, and the percent difference (in absolute terms

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by