For $f(x)=e^x$, find a Taylor approximation that is in error by at most $10^-7$ on [-1,1]. Using this approximation, write a function program to evaluate $e^x$. Compare it to the standard value of $e^x$ obtained from the MATLAB function exp(x); calculate the difference between your approximation and exp(x) at 21 evenly spaced points in [-1,1].

So I am stuck on this problem. I know for the first part, I needed to find n smallest such that $e/(n+1)!$ $\leq$ $10^-7$, so I found n=10 to satisfy that part.

So do I need to find a polynomial of degree 10 using a nested loop that evaluates $e^x$? What is my next movie in this problem?

  • 1
    $\begingroup$ Yes, a loop to evaluate $1+x+x^2/2+...$ up to 10. $\endgroup$ – lemon Oct 7 '14 at 18:49
  • $\begingroup$ Thank you! and then I just compare my loop with the MATLAB function given and see where they are different? $\endgroup$ – user141745 Oct 7 '14 at 18:52
  • 2
    $\begingroup$ Very relevant wiki example. $\endgroup$ – BeaumontTaz Oct 7 '14 at 18:59
  • 1
    $\begingroup$ @user141745 Yes - the purpose of comparing is to make sure that your routine is accurate to within $10^{-7}$. $\endgroup$ – lemon Oct 7 '14 at 19:02

I posted the link in the comments, but I'll flush out the example on the wiki here with some added commentary.

We have $f(x)=e^x$ and we know by Taylor'r theorem that


where $R_k(x)$ is our error term and $P_k(x)$ is the Taylor polynomial. We want to find the $k$ such that $R_k(x)<10^{-7}$ for $x\in[-1,1]$.

We know that the remainder term $R_k(x)$ can be expressed as


for some $\xi\in[-1,1]$. We need to find the value of $\xi$ though. And since we don't know $e^x$ (otherwise we'd just use that function) we again approximate it using a lower order Taylor expansion


Since $e^x$ is an increasing function, we know that $e^\xi<e^x$ when $-1<\xi<x$. So we can say


Which we can solve for $e^x$ and get


Some basic calculus gives us that the maximum of this expression for $x\in[-1,1]$ is at $x=1$ and $e^x<4$.



And since $x^{k+1}$ is a maximum on $[-1,1]$ at $x=1$ and we want $R_k(x)$ to be less than $10^{-7}$ we have


So basically, we just need to find $k$ such that $(k+1)!>4\cdot10^7$. And $11!=39916800=3.99\cdot10^7$ which is reallllly close. But, really we need $(k+1)!=12!$ so $k=11$.

Now... this is a very modest upper bound. If we really think about it (and this is what you did), the upper bound on the interval $[-1,1]$ is $e$. So if we did this with $e^x<e$ on $x\in[-1,1]$ instead, then we'd end up with

$$\frac{e}{\left(k+1\right)!}<10^{-7}\implies (k+1)!>2.718\cdot10^7\implies k=10$$

Same as you got. However, this relies on the fact that we know $e$ already.

If we don't know $e$ we can use a higher order approximation than $2$. So we find that


Which means it's either $e^x<\frac{15}{5}=3$ if it's at $x=1$ or it's at the maximum of the interval which involves find the roots of a fourth order polynomial $3x^4+12x^3+18x^2+36x+36$ whose critical points turn out not to be on our bound. So we can use this higher order approximation the same way we did before and get that

$$\frac{3}{\left(k+1\right)!}<10^{-7}\implies (k+1)!>3\cdot10^7\implies k=10$$

From there, you can either hard code the approximation

$$e^x\approx \text{myFunc}(x)=1+x+\frac{x^2}{2}+\frac{x^3}{3!}+\frac{x^4}{4!}+\frac{x^5}{5!}+\frac{x^6}{6!}+\frac{x^7}{7!}+\frac{x^8}{8!}+\frac{x^9}{9!}+\frac{x^{10}}{10!}$$

or do a sum like this in a for loop

$$e^x\approx \text{myFunc}(x)=\sum_{i=0}^{10}\frac{x^i}{i!}$$

It appears as though Arkamis gave you some MATLAB code that'll do this for you, though.


Just for fun, here's a matlab script that computes the answer in one line -- in MATLAB we almost never need to loop!

The function takes three parameters: n, the maximum order of the polynomial to try; m, the number of data points in the interval we wish to evaluate; and tol, the tolerance we wish to achieve.

function Order = TaylorExp(n,m,tol)
Order = min(find(max(abs(tril(kron(ones(n,1),1./factorial(0:(n-1))))*power(kron(ones(n,1),linspace(-1,1,m)),kron(ones(1,m),(0:(n-1))')) - kron(ones(n,1),exp(linspace(-1,1,m)))),[],2) < tol)) - 1;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.