0
$\begingroup$

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

enter image description here

And for some reason with

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

Mathematica returns something bizarre...

enter image description here

Can anyone tell me what is going on? How do I get the correct plot?

$\endgroup$
1
$\begingroup$
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]

enter image description here

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}]]

enter image description here

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – Alexander Swanson Dec 2 '17 at 1:00
  • $\begingroup$ 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 $\endgroup$ – Bob Hanlon Dec 2 '17 at 2:19
  • $\begingroup$ Oh, nevermind that comment. I notice now the limit you gave 't' in the new definition. $\endgroup$ – Alexander Swanson Dec 2 '17 at 16:31
1
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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 $\endgroup$ – Alexander Swanson Dec 2 '17 at 16:30
1
$\begingroup$

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}];
pade2 = PadeApproximant[Exp[Sin[x]], {x, 0, 10}];

Here's a comparison:

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

enter image description here

$\endgroup$
  • $\begingroup$ interesting! I had no knowledge of that function. I'll try it out! $\endgroup$ – Alexander Swanson Dec 2 '17 at 16:32

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.