Smile_SafeLY
Smile_SafeLY

Reputation: 67

How to input function so it can be graphed correctly

Currently I am trying to graph the sin(x) function and a function named myPolys which is the taylor polynomial of sin(x) as it is equal to

myPolys = 
Table[Table[(-1)^((j - 1)/2) x^j/Factorial[j], {j, 1, h, 2}], {h, 1, 
      19, 2}];

How can I use manipulate to graph both functions so that each part of myPolys is graphed

My graphing code so far:

Manipulate[Plot[{Sin[x], myPolys[[n]]}, {x, -10, 10}, 
PlotRange -> {-5, 5}], {n, 1, Length[myPolys], 1}];

currently for each iteration of n, myPolys is graphed as separate x and then x & -(x^3)/3! and then x & -(x^3)/3! & x^5/5! (all are plotted separate o the same graph )

The graph I'm trying to achieve is that for n=1 sin(x) should be plotted and x from myPoly should be plotted, for n=2 it continues and graphs x-(x^3/3!) (instead of plotting, for n=2, x and -x^3/3! seperately) and so on and so forth until n reaches 10.

My graph at the moment:

enter image description here

The graph I'm hoping for: enter image description here

Upvotes: 2

Views: 64

Answers (2)

agentp
agentp

Reputation: 6999

I suppose you know there is the built in Series you can use..

Manipulate[m = n;
 Show[{
   Plot[Sin[x], {x, -10, 10}, PlotRange -> {-5, 5}, 
    PlotStyle -> {Thick, Red}],
   Plot[Evaluate[
     Accumulate[List @@ Normal@Series[Sin[x], {x, 0, m}]]], {x, -10, 10}]}],
    {{n, 3}, 3, 25, 2}]

Upvotes: 1

Chris Degnen
Chris Degnen

Reputation: 8680

myPolys = Table[Sum[(-1)^((j - 1)/2) x^j/Factorial[j],
    {j, 1, h, 2}], {h, 1, 19, 2}];

Manipulate[Plot[{Sin[x], Evaluate@Take[myPolys, n]},
  {x, -10, 10}, PlotRange -> {-5, 5}], {n, 1, Length[myPolys], 1}]

enter image description here

Or, in more functional style.

Clear[myPolys]

myPolys[n_] := Table[Sum[(-1)^((j - 1)/2) x^j/Factorial[j],
   {j, 1, h, 2}], {h, 1, 2 n - 1, 2}]

Manipulate[Plot[{Sin[x], Evaluate@myPolys[n]},
  {x, -10, 10}, PlotRange -> {-5, 5}], {n, 1, 10, 1}]

And with legend.

myLabels[n_] := Table[Sum[(-1)^((j - 1)/2) x^j/ToString[j] <> "!",
   {j, 1, h, 2}], {h, 1, 2 n - 1, 2}] 

Manipulate[Plot[{Sin[x], Evaluate@myPolys[n]},
  {x, -10, 10}, PlotRange -> {-5, 5},
  PlotLegends -> Placed[PointLegend[
     Rest@Array[ColorData[97], n + 1], HoldForm /@ myLabels[n],
     LegendMarkers -> {"\[Tilde]", 30}], Left]], {n, 1, 10, 1}]

enter image description here

Upvotes: 2

Related Questions