# PlotLegends to show sum's $n$ unevaluated as $n$

I'd like to show the cosine Taylor series. However, I'd like to to see things like (2n)! evaluated, but instead (2*4)! (e.g. if n=4).

This is what I got:

Manipulate[
Plot[
{Cos[x], Sum[(-1)^n x^(2 n)/(2 n)!, {n, 0, t}]},
{x, -4 Pi, 4 Pi},
PlotLegends -> {
Cos[x],
Sum[(-1)^n x^(2 n)/(2 n)!], {n, 0, t}]
},
PlotRange -> {Automatic, {-2, 2}},
Ticks -> {Range[-4 Pi, 4 Pi, Pi/2], Automatic},
AxesLabel -> {"x", "y"}
],
{t, 0, 16, 1}
]


However, it's evaluating n in the denominator, which makes numbers quickly quite high and not helpful to understand the series in its whole.

I was trying different things, such as Unevaluated[...] but it just ended up me displaying that string Unevaluated[ and ], too. What is the right way to achieve my goal then?

P.S.: I'm a beginner to Mathematica. I just know the basics.

• What does "I'd like to to see things like (2n)! evaluated, but instead (2*4)!" mean? – David G. Stork Jan 31 '18 at 22:41
• I agree with @Kuba, you may try Inactivate[Sum[(-1)^n x^(2 n)/(2 n)!, {n, 0, t}], Factorial] – egwene sedai Jan 31 '18 at 22:56

## 3 Answers

Here is a fleshed out version of @egwenesedai's suggestion:

Manipulate[
Plot[
{Cos[x],Sum[(-1)^n x^(2 n)/(2 n)!,{n,0,t}]},
{x,-4 Pi,4 Pi},
PlotLegends->{
Cos[x],
StandardForm@Inactivate[1+Sum[(-1)^n x^(2 n)/(2n)!,{n,1,t}],Factorial]
},
PlotRange->{Automatic,{-2,2}},
Ticks->{Range[-4 Pi,4 Pi,Pi/2],Automatic},
AxesLabel->{"x","y"}
],
{t,0,16,1}
]


I use 1 + Sum[.., {n, 1, t}] instead of Sum[.., {n, 0, t}] to avoid an unevaluated 0!, and I use StandardForm so that the terms are ordered in ascending powers of x instead of the reverse.

Here is an image showing an example with the desired legend: ## Add

A minimal improvement avoiding the horizontal enlargement when the expansion increases (more readable in vertical), and to show the correct mathematical series expansion:

Manipulate[Labeled[Plot[{Cos[x],
Sum[(-1)^n x^(2 n)/(2 n)!, {n, 0, t}]}, {x, -4 Pi, 4 Pi},
PlotRange -> {Automatic, {-2, 2}}, PlotStyle -> {Red, Green},
Ticks -> {Range[-4 Pi, 4 Pi, Pi/2], Automatic},
AxesLabel -> {"x", "y"}, ImageSize -> Medium],
TraditionalForm@
Grid[{{Red, "f(x)= " Cos[x]}, {Green,
"f(x)\[TildeEqual] " HoldForm[
Sum[(-1)^n x^(2 n)/(2 n)!, {n, 0, k}]] ==
StandardForm@
Inactivate[1 + Sum[(-1)^n x^(2 n)/(2 n)!, {n, 1, t}],
Factorial]}}, ItemSize -> {{3, 12}, {1, Automatic}},
Spacings -> {0, 1},
Alignment -> {{Center, Left}, {Center, Left}}]],
{{t, 0, "k"}, 0, 16, 1, Appearance -> "Labeled"},
ContinuousAction -> False] I'd do it this way (although Carl suggestion looks better):

Manipulate[
Plot[{Cos[x], Sum[(-1)^n x^(2 n)/(2 n)!, {n, 0, t}]}, {x, -4 Pi,
4 Pi}, PlotLegends -> {Cos[x],
With[{t = t}, Defer@Sum[(-1)^n x^(2 n)/(2 n)!, {n, 0, t}]]},
PlotRange -> {Automatic, {-2, 2}},
Ticks -> {Range[-4 Pi, 4 Pi, Pi/2], Automatic},
AxesLabel -> {"x", "y"}
], {t, 0, 16, 1}
] Here is yet another way, which seems to me to be closer to what you asked for than the answers that proceeded this post.

Manipulate[
Column @
{Plot[{Cos[x], Sum[(-1)^n x^(2 n)/(2 n)!, {n, 0, t}]}, {x, -4 Pi, 4 Pi},
PlotRange -> {Automatic, {-2, 2}},
Ticks -> {Range[-4 Pi, 4 Pi, Pi/2], Automatic},
AxesLabel -> {"x", "y"},
AspectRatio -> 1/4,
ImageSize -> 500],
LineLegend[{ColorData}, {Cos[x]}],
LineLegend[
{ColorData},
{Sum[(-1)^n x^(2 n)/With[{n = n}, HoldForm[(2 n)!]], {n, 0, t}]}]},
{t, 0, 10, 1, Appearance -> "Labeled"}] 