Reputation:
Is there a quick way to plot the Maclaurin/Taylor series using Gnuplot. For eg: http://en.wikipedia.org/wiki/Power_series I'm trying to plot each term of the series NOT the series f(x) vs x. So, for sin(x) I'd like to plot on the x-axis, x, -x^3/3!, x^5/5! etc and the sum itself all on one axis without having to type each term in the sequence manually.
Upvotes: 0
Views: 1072
Reputation: 7905
You can plot functions in gnuplot. In this case, you would have to define your series, for example:
gnuplot> a0 = 1
gnuplot> a1 = 0.5
gnuplot> a2 = 0.1
gnuplot> f(x) = a0 + a1*x**2 + a2*x**3
gnuplot> plot f(x)
This will give you:
Edit
Based on the comment to this answer, I'm proposing this (which should work for gnuplot 4.4 and higher):
First, define your factorial:
gnuplot> fac(n) = (n==0) ? 1 : n * fac(n-1)
Second, iterate over as many terms as you like (in this case 10
). We're only interested in the uneven exponents, hence the upper bound is 20
. Furthermore, every other "term" has to be multiplied by -1
, thus two commands, and an increment of 4
:
plot for [a=3:21:4] -1*x**a/fac(a), for [a=1:21:4] x**a/fac(a)
This will give you a plot like this:
Upvotes: 1