I'm trying to evaluate f(x) at x=-20 and plotting the Taylor estimation with the actual value. My results do not match with my hand calculations and I can't find the problem in my recursive for-loop.
%Using Taylor polynomials to evaluate f(x)=e^x at x=-20
n = 10;x = -20;%terms = 0;
terms = zeros(1, n+1);for j = 0:1:n terms(j+1)=(x.^j)/factorial(j);end;%display(terms)
termsSum = sum(terms); %holds the estimate
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.^sign(x)h = abs(t-y)display(h)%plot the approximation and exact graph
plot(-22:0.001:-18,termsSum,'o')plot(x,exp(x),'*')
Best Answer