I have a piece of code
clear;x=2;expapprox=0;expapproxarray=zeros(1,12);for i=0:12 expapprox=expapprox+x^i/factorial(i); error=abs(expapprox-exp(x)) for j=1:size(expapproxarray) expapproxarray(j)=error endend plot(expapproxarray)
that compares the exponential function to an approximation of that function using the Taylor series. I want to plot the error found between the two functions for an increasing number of terms in the Taylor expansion. I thought this could be done using the code above but there is a problem with how the second loop works. I think the problem is that the full iteration of the second loop is being done within the first loop but changing the order of the loops gives another wrong answer. How can I get the value of error for each value of I to be outputted into some array that I have called expapproxarray that can then be plotted as a function of i?
Best Answer