# function program for $e^x$

I'm Using this approximaion:

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

I'm trying to write a function program to evaluate $e^x$ and have an error that is $\leq$ $10^{-7}$

which is the taylor approximation for $e^x$ up to 10. Is this the correct way to do it? This is a written assignment so I don't have MATLAB confirm if I am doing it correctly over the weekend and feel as though I am missing something. This is what I have, thanks for any and all help.

function px=taylorapprox_my(x,n)

% Input variables:

% x: the value of the interval

% n: the degree of the expansion

% Output:

% px: the value of P(x)

% Evaluate the Taylor approximation at each point n, in the interval [-1,1] up to degree 10, with n the degree of the polynomial.

for n=0:10

px=((x.^n)/factorial(n));


end

end

• The error depends on the value of $x$ – Shakespeare Oct 9 '14 at 19:22

## 1 Answer

The error in the Taylor series for $x \leq 0$ is at most $x^{n+1}/(n+1)!$. You can calculate the value of this as you go, and once it drops below your error tolerance, stop adding new terms and return the result. When $x \geq 0$ things get more complicated since the error is now at most $e^x x^{n+1}/(n+1)!$, and you don't know $e^x$ exactly. However, you can get an almost tight upper bound such as $e^{\lceil x \rceil}$ by multiplying $e$ with itself enough times. Now you can do the same — calculate the error as you go and truncate the series once it drops below your tolerance.

• $e^{\lceil x \rceil}$ may be hard to calculate, if $e$ is not known from the start. You might use $3^{\lceil x \rceil}$ instead. – Joel Oct 9 '14 at 19:39