[metapost] Function defined by a sum

Mojca Miklavec mojca.miklavec.lists at gmail.com
Fri Nov 2 11:21:19 CET 2007


On 10/31/07, Nicolas FRANCOIS wrote:
> Hi.
>
> I'd like to show the Gibbs phenomenon on an example. For this I'd like to
> draw the partial sums of the Fourier serie :
>
>   sum(sin(n*x)/n,n=1..infty)

There is no "sum" (unless you define one), but oyu can use "for" loop:

vardef sin(expr xx) = sind(xx*180/3.14) enddef;

vardef f(expr xx,n) =
   save sum;
   sum:=0;
   for i=1 upto n:
      sum := sum+sin(i*(xx))/i;
   endfor;

   sum
enddef;

beginfig(1);
   path p;
   p := (0,0)
   for j=1 upto 700:
      --(j/100,f(j/100,3))
   endfor;
   draw p scaled 1cm;
endfig;
end.

> For the first five partial sums, I can of course tpe them by hand. But
> what about the sum number 20 ? And 50 ? Is there a "sum" construction
> available in Metapost ? Or some for loop ?

See the metapost manual for using "for" loops.
    http://tug.ctan.org/graphics/metapost/mpman.pdf

Mojca


More information about the metapost mailing list