1\\ adapted from an original idea by Ilya Zakharevich
2
3\\ generate an RGB color triple from a "magnitude" between 0 and 255
4\\ (low = close to a cold blue, high = close to a hot red).
5\\ To generate simple colormaps.
6rgb(mag) =
7{ my(x = mag/255., B, G, R);
8  B = min(max(4*(0.75-x),     0), 1);
9  R = min(max(4*(x-0.25),     0), 1);
10  G = min(max(4*abs(x-0.5)-1, 0), 1);
11  return (floor([R, G, B]*255));
12}
13default(graphcolormap, concat(["white","black","blue"], vector(25,i,rgb(10*i))));
14default(graphcolors, vector(25,i,i+2));
15
16\\ plot Taylor polynomials of f,
17\\ of index  first + i*step <= ordlim, for x in [xmin,xmax].
18plot_taylor(f, xmin=-5, xmax=5, ordlim=16, first=1, step=1) =
19{
20  my(T,s,t,w,h,dw,dh,cw,ch,gh, extrasize = 0.6);
21  my(Taylor_array);
22
23  default(seriesprecision,ordlim+1);
24  T = f('q);
25  ordlim = (ordlim-first)\step + first;
26  Taylor_array = vector(ordlim+1);
27  forstep(i=ordlim+1, 1, -1,
28    T += O('q^(1 + first + (i-1)*step));
29    Taylor_array[i] = truncate(T)
30  );
31
32  t = plothsizes();
33  w=floor(t[1]*0.9)-2; dw=floor(t[1]*0.05)+1; cw=t[5];
34  h=floor(t[2]*0.9)-2; dh=floor(t[2]*0.05)+1; ch=t[6];
35
36  plotinit(2, w+2*dw, h+2*dh);
37  plotinit(3, w, floor(h/1.2));
38  \\ few points (but Recursive!), to determine bounding box
39  s = plotrecth(3, x=xmin,xmax, f(x),
40                "Recursive|no_X_axis|no_Y_axis|no_Frame", 16);
41  gh=s[4]-s[3];
42
43  plotinit(3, w, h);
44  plotscale(3, s[1], s[2], s[3]-gh*extrasize/2, s[4]+gh*extrasize/2);
45  plotrecth(3, x=xmin,xmax, subst(Taylor_array, 'q, x), "no_Rescale");
46  plotclip(3);
47  plotcopy(3, 2, dw, dh);
48
49  plotmove(2, floor(dw+w/2-15*cw), floor(dh/2));
50  plotstring(2, "Multiple Taylor Approximations");
51  plotdraw(2);
52}
53
54\p9
55plot_taylor(sin)
56plot_taylor(exp,-3,3)
57plot_taylor(x->besselk(2,x), 1,5)
58plot_taylor(x->1/(1+x^2), -1.2,1.2)
59