0

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?

2 Answers2

2

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;
Emily
  • 35,688
  • 6
  • 93
  • 141
2

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

$$f(x)=P_k(x)+R_k(x)=1+x+\frac{x^2}{2}+\frac{x^3}{6}+\cdots+\frac{x^k}{k!}+R_k(x)$$

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

$$R_k(x)=\frac{e^\xi}{\left(k+1\right)!}x^{k+1}$$

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

$$e^x=P_1(x)+R_1(x)=1+x+\frac{e^\xi}{2}x^2$$

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

$$e^x=1+x+\frac{e^\xi}{2}x^2<1+x+\frac{e^x}{2}x^2$$

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

$$e^x<2\frac{x+1}{2-x^2}$$

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

Therefore

$$R_k(x)=\frac{e^\xi}{\left(k+1\right)!}x^{k+1}<\frac{4x^{k+1}}{\left(k+1\right)!}$$

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

$$R_k(x)<\frac{4}{\left(k+1\right)!}<10^{-7}$$

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

$$e^x<\frac{6+6x+3x^2}{6-x^3}$$

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.

BeaumontTaz
  • 2,795