Power series in Mathematica

first example

In this example we are defining our own function called f which has one variable x.  The underscore after the x on the right hand side of the equation is a tipoff that x is being used as a variable

f[x_]  =  Sin[x]

Built-in Mathematica functions start with capital leters.  E.g., Sin and Series

Series[f[x], {x, 0, 1}]

The O[x] is a tip-off that Mathematica is returning a complicated object,not just a function

numterms  =  1
apf[x_]  =  Normal[Series[f[x], {x, 0, numterms}]]

Look up the help on Series.  In the "master index" part of the help popup look for "conversion to normal expressions" and you'll see how Normal works.

p1  =  
      Plot[apf[x], {x, 0, 1}]

    p2  =  
      Plot[f[x], {x, 0, 1}, 
        PlotStyle -> {RGBColor[1, 0, 0]}]

    Show[p1, p2]

numterms  =  3
apf[x_]  =  
      Normal[Series[f[x], {x, 0, numterms}]]
p1  =  Plot[apf[x], {x, 0, 1}]
p2  =  
      Plot[f[x], {x, 0, 1}, PlotStyle -> {RGBColor[1, 0, 0]}]
Show[p1, p2]

you see this is MUCH more accurate near 1.  But power series expansions are local.  so as we get farther and farther from the point about which the series is made, the accuracy gets progressively worse.

numterms  =  3
apf[x_]  =  
      Normal[Series[f[x], {x, 0, numterms}]]
p1  =  Plot[apf[x], {x, 0, 3}]
p2  =  
      Plot[f[x], {x, 0, 3}, PlotStyle -> {RGBColor[1, 0, 0]}]
Show[p1, p2]

the only thing we changed in this example is the range over which we plotted Sin and its power series approximation.  Next lets try approximating Sin about  Pi/2  instead of zero.

numterms  =  1
apf[x_]  =  
      Normal[Series[f[x], {x, Pi/2, numterms}]]
p1  =  Plot[apf[x], {x, 0, 3}]
p2  =  
      Plot[f[x], {x, 0, 3}, PlotStyle -> {RGBColor[1, 0, 0]}]
Show[p1, p2]

having only one term makes a terrible approximation, except  at the exact point pi/2.  here is the SAME plot, but zoomed in to this point.

p1  =  Plot[apf[x], {x, 1.5707, 1.5709}]
p2  =  
      Plot[f[x], {x, 1.5707, 1.5709}, 
        PlotStyle -> {RGBColor[1, 0, 0]}]
Show[p1, p2]

something strange?  well if you look on the y-axis of these plots the numbers are all 1.  this is because mathematica trys to automatically zoom in to get something interesting plot.  we have to overriide this and force the plot to have certain limits.  In this case we use the PlotRange to say that the y-axis should be plotted from .9 to 1.1.

p1  =  
      Plot[apf[x], {x, 1.4, 1.7}, 
        PlotRange -> {Automatic, { .9, 1.1}}]
p2  =  
      Plot[f[x], {x, 1.4, 1.7}, PlotStyle -> {RGBColor[1, 0, 0]}, 
        PlotRange -> {Automatic, { .9, 1.1}}]
Show[p1, p2]