Taylor series approximation of e^x at x =-20
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
cee878
el 29 de En. de 2016
Comentada: Matt J
el 1 de Feb. de 2016
I'm trying to evaluate the Taylor polynomials for the function e^x at x = -20. My results do not look right and I don't know what's wrong with my for loop. Also, I can't seem to plot my data correctly with one being the approximate and the actual one on the same graph. Here's my code:
n = 1;
x = -20;
%terms = 0;
terms = zeros(1, n+1);
for j = 0:1:n
terms(j+1)=(x.^j)/factorial(j);
%display(terms)
end;
%display(terms)
termsSum = sum(terms);
display(terms)
display('The approximate estimate at x = -20')
display(termsSum)
%true value at x=-20
display('TRUE VALUE')
display(exp(x))
%absolute error between the approximated and true value
display('The error: ')
t = exp(x)
y = termsSum
h = abs(t-y)
display(h)
%plot the approximation
%plot(-22:0.001:-18,termsSum,'o')
plot(terms,h, '*')
0 comentarios
Respuesta aceptada
Matt J
el 29 de En. de 2016
Editada: Matt J
el 29 de En. de 2016
You're better off approximating exp(+20) with the Taylor series, and then taking the reciprocal of that.
n = 50;
x = -20;
%terms = 0;
terms = zeros(1, n+1);
for j = 0:n
terms(j+1)=(abs(x).^j)/factorial(j);
end;
termsSum = sum(terms);
t = exp(x);
y = termsSum.^sign(x);
h = abs(t-y);
This avoids the numerical instability of summing over large terms with opposite signs. As for plotting, it's not clear to me what you're plotting things as a function of. n? x?
14 comentarios
John D'Errico
el 31 de En. de 2016
Matt points out the problem with computing either exp(-20) or exp(20). Both will be problematic in a series, due to the magnitude of those terms. However, there are nice ways to greatly improve the convergence of a series.
For example, you can use a simple identity or two to fix things. Consider this one:
exp(x) = (exp(x/k))^k
For example, if k = 2, this reduces to
exp(x) = (exp(x/2))^2 = exp(x/2)*exp(x/2)
In the case of x=-20, we can thus compute exp(-20) using k = 32.
exp(-20) = (exp(-20/32))^32
Lets see how this can be used to compute exp(-20) in efficient fashion.
xhat = -20/32;
n = 15; % 16 total terms in the series
ii = (0:n).';
t = xhat.^ii./factorial(ii)
t =
1
-0.625
0.19531
-0.04069
0.0063578
-0.00079473
8.2784e-05
-7.3914e-06
5.7746e-07
-4.0101e-08
2.5063e-09
-1.424e-10
7.4169e-12
-3.5658e-13
1.5919e-14
-6.6329e-16
sum(t)
ans =
0.53526
format long g
sum(t)
ans =
0.53526142851899
So the above is a very good approximation to exp(-20/32). Raise it to the 32nd power...
sum(t)^32
ans =
2.06115362243854e-09
exp(-20)
ans =
2.06115362243856e-09
and we got almost full precision, using only 16 total terms. Had I used a larger value of k, the convergence would have been faster.
There are lots of ways of accelerating convergence of such a series. Generally, if we can make x smaller (closer to zero), then the series will converge with fewer terms.
Matt J
el 1 de Feb. de 2016
Note that my proposed method is a special case of John's method with k=-1. Running that case, you should see that it works, but it took me large n (~100) to get good precision.
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!