carminat (48) [Avatar] Offline
#1
I would like to write a gnuplot script which produces an animation showing the convergence of power series.

For instance if f(x)=sin(x), and T_n(x) its taylor-mcLaurin polinomial of degree n, I would like the script to plot a series of frames with f compared to T_n (for n from 1 to 13 say).

I can do it generating data files corresponding to the taylor polynomials with an external program (matlab, maple, etc).
Is there a clever way to do this task just with gnuplot?
philipp.janert (282) [Avatar] Offline
#2
Re: Power series
plot [-8:8][-1.2:1.2] sin(x), x
pause 1
plot [-8:8][-1.2:1.2] sin(x), x-x**3/3!
pause 1
plot [-8:8][-1.2:1.2] sin(x), x-x**3/3!+x**5/5!
pause 1
plot [-8:8][-1.2:1.2] sin(x), x-x**3/3!+x**5/5!-x**7/7!
pause 1
plot [-8:8][-1.2:1.2] sin(x), x-x**3/3!+x**5/5!-x**7/7!+x**9/9!
pause 1

This will plot the first 5 truncated Taylor polynomials, showing each for 1 second.
philipp.janert (282) [Avatar] Offline
#3
Re: Power series
And if you have a very recent version of gnuplot (such as the release candidate
for the new 5.0 version), you can do this nifty bit of code:

do for [n=1:12] {
plot [-8:8][-1.2:1.2] sin(x), sum[k=1:n] (-1)**(k-1)*x**(2*k-1)/(2*k-1)!
pause 1
}
carminat (48) [Avatar] Offline
#4
Re: Power series
That's great, thanks!
I have gnuplot 4.6, and it works fine.

I am looking forward to showing it to my students
(taylor series are next semester's topic).

I just added some improvements, to visualize it better:

##############################################
set terminal wxt enhanced size 2000, 700
set sample 1000

set xzeroaxis
set yzeroaxis
set size ratio -1

do for [n=1:12] {
plot [-11:11][-4:4] sin(x), sum[k=1:n] (-1)**(k-1)*x**(2*k-1)/(2*k-1)! t "n=".n
pause 1
}
##############################################

Message was edited by:
carminat

Message was edited by:
carminat
441392 (1) [Avatar] Offline
#5
I am using GNUPLOT since last 3 months. I used C language to generate data and then plotted it using GNUPLOT. I plotted complex transfer functions, Electric field vectors of Charges etc. For me, still there is much to learn about Gnuplot .The plot of power series is superb. The two demonstrations of Power series are clearly indicating the fact that the good command on GNUPLOT will certainly make our task at hand to be much easier. Thank You.