4

I'd like to know how to plot power series (whose variable is x), but I don't even know where to start with. I know it might not be possible plot infinite series, but it'd do as well plotting the sum of the first n terms.

2 Answers2

10

Gnuplot has a sum function, which can be used inside the using statement to sum up several columns or terms. Together with the special file name + you can implement power series.

Consider the exponention function, which has a power series

\sum_{n=0}^\infty x^n/n!

So, we define a term as

term(x, n) = x**n/n!

Now we can plot the power series up to the n=5 term with

set xrange [0:4]
term(x, n) = x**n/n!
set samples 20
plot '+' using 1:(sum [n=0:5] term($1, n))

enter image description here

To plot the results when using 2 to 7 terms and compare it with the actual exp function, use

term(x, n) = x**n/n!
set xrange [-2:2]
set samples 41
set key left
plot exp(x), for [i=1:6] '+' using 1:(sum[t=0:i] term($1, t)) title sprintf('%d terms', i)

enter image description here

Christoph
  • 47,569
  • 8
  • 87
  • 187
0

The easiest way that I can think of is to generate a file that has a column of x-values and a column of f(x) values, then just plot the table like you would any other data. A power series is continuous, so you can just connect the dots and have a fairly accurate representation (provided your dots are close enough together). Also, when evaluating f(x), you just sum up the first N terms (where N is big enough). Big enough means that the sum of the rest of the terms is smaller than whatever error you allow. (*If you want 3 good digits, then N needs to be large enough that the remaining sum is smaller than .001.)

You can pull out a calc II textbook to determine how to bound the error on the tail of the sum. A lot of calc classes briefly cover it, but students tend to feel like the error estimates are pointless (I know because I've taught the course a few times.) As an example, if you have an alternating series (whose terms are decreasing in absolute value), then the absolute value of the first term you omit (don't sum) is an upperbound on your error.

*This statement is not 100% true, it is slightly over simplified, but is correct for most practical purposes.

TravisJ
  • 1,592
  • 1
  • 21
  • 37