MATLAB: Taylor series approximation of e^x at x =-20

taylor series

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, '*')

Best Answer

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?