# Problem with plotting Taylor polynomials

I'm trying to plot

f1[x_] := Exp[Sin[x]]


And its Taylor polynomials

taylorFunction1[x_, n_] := Normal[Series[f1[x], { x, 0, n} ] ]


for n = 0 to 10, which I've defined as:

h1[x_] := taylorFunction1[x, 10]


When I plot these on my first interval: [-1/2, 1/2] they both work doing

Plot[f1[x], {x, -1/2, 1/2}]

Plot[Evaluate@Normal[{h1[x]}], {x, -1/2, 1/2}]


However, when I try:

Plot[f1[x], {x, 7/2, 9/2}]


I get And for some reason with

Plot[Evaluate@Normal[{h1[x]}], {x, 7/2, 9/2}]


Mathematica returns something bizarre... Can anyone tell me what is going on? How do I get the correct plot?

Clear[f1, h1, taylorFunction1]

f1[x_] := Exp[Sin[x]]

taylorFunction1[x_Symbol, n_Integer?NonNegative] :=
Normal[Series[f1[x], {x, 0, n}]]

h1[x_] := taylorFunction1[t, 10] /. t -> x

h1[x] You need to extend the plot range to see any difference between the plots.

Plot[{f1[x], h1[x]}, {x, -2, 2},
PlotLegends -> Placed["Expressions", {.25, .5}]] • Hey Bob, thanks for the help. I see what you're saying. So is the problem in the input? I come from a programming background, so I figured that that might be it, but I was not sure. Is Mathematica just unwilling to take that literal argument for taylorFunction1? Dec 2, 2017 at 1:00
• To see the problem, change the definition back to taylorFunction1[x_, n_Integer?NonNegative] := Normal[Series[f1[x], {x, 0, n}]]; then do h1[.1] // Trace Dec 2, 2017 at 2:19
• Oh, nevermind that comment. I notice now the limit you gave 't' in the new definition. Dec 2, 2017 at 16:31

I'd say this is a limitation of the mathematical approach, not Mathematica misbehaving particularly.

The Taylor series centered at $x_0=0$ seems to be divergent outside of $-1<x<1$. Note that Taylor series expansions do not need to converge everywhere in a finite number of terms. You can see evidence in favor of this by trying higher order approximations (starting the series at 0 and {x, 7/2, 9/2}, it's obvious that it gets worse).

If you do:

h1[x_] = Normal[Series[f1[x], { x, 8/2, 10} ] ]


You'll find that the graph for the range {x, 7/2, 9/2} is far more accurate (though it will now be equally wrong in the range centered on 0).

One possible solution for using Taylor series approximations for this problem is to manipulate the argument of h1[x] so that x will always be within -1 and 1, such as by using the periodicity of Sin and the double angle rule modified to account for the exponential.

• that is an excellent point. The root problem was that I wasn't understanding why Mathematica could not plot the Taylor series. However, I realize that my bounds were not great for my problem. You're example is much more insightful Dec 2, 2017 at 16:30

You can use a Pade approximant instead of a Taylor series if you want to have a larger radius of convergence:

pade1 = PadeApproximant[Exp[Sin[x]], {x, 0, 5}];

Plot[{pade1, pade2, Exp[Sin[x]]}, {x, -4.5, 4.5}, PlotLegends->"Expressions"] 