Taylor series for ln(x)?
10 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 个)
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 Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!