Gerben, Again, thanks for all you do. Here's a Fourier approximation of a square wave. Again, I compiled this in Texshop so I know it works.

%verbatimtex
% \input mtplain
% \MTMI{8pt}{6pt}{5pt}
% \MTSY{8pt}{6pt}{5pt}
% \MTEX{8pt}
% \MathRoman{tir}{8pt}{6pt}{5pt}
% \MathBold{tib}{8pt}{6pt}{5pt}
%etex

%Input Context macros
input mp-tool

%define ytick
 vardef ytick(expr pos)=
  path p;
  p:=(-2,0)--(2,0);
  draw p shifted pos;
 enddef;

%define xtick
 vardef xtick(expr pos)=
  path p;
  p:=(0,-2)--(0,2);
  draw p shifted pos;
 enddef;

%define pi
 pi:=3.14159;

%define cosine in radians
 vardef cos(expr x)=
  cosd(x*180/pi)
 enddef;

%define sine in radians
 vardef sin(expr x)=
  sind(x*180/pi)
 enddef;

%hyperbolic sine
 vardef sinh(expr x)=
  (exp(x)-exp(-x))/2
 enddef;

beginfig(1);

%enter number of terms
 numeric N;
 N=6;

%define L
 numeric L;
 L:=pi;

%define a_0
 ao:=1;

%define a_n
 vardef a(expr n)=
  2*sin(n*pi/2)/(n*pi)
 enddef;

%define b_n
 vardef b(expr n)=
  0
 enddef;

%initialize scale
 numeric ux, uy;
 pi*ux=2in; 1.5*uy=2in;

%draw axes
 drawarrow (0,0)--(3.5ux,0);
 drawarrow (0,0)--(0,1.5uy);

%label axes
 label.rt(btex $x$ etex, (3.5ux,0));

%tick marks
 xtick((pi*ux,0));
 label.bot(btex $\pi$ etex, (pi*ux,0));
 ytick((0,1*uy));
 label.lft(btex $1$ etex, (0,1*uy));

%draw the function in black
 draw (0,1uy)--((pi/2)*ux,1uy);

%draw Fourier approximation in cyan
 path p;
 numeric x, y;
 x:=0;
 y:=ao/2;
 for k=1 step 1 until N:
  y:=y+a(k)*cos(k*pi*x/L)+b(k)*sin(k*pi*x/L);
 endfor;
 p:=(x,y);
 for x=0 step .1 until pi:
  y:=ao/2;
  for k=1 step 1 until N:
   y:=y+a(k)*cos(k*pi*x/L)+b(k)*sin(k*pi*x/L);
  endfor;
  p:=p--(x,y);
 endfor;
 x:=pi;
 y:=ao/2;
 for k=1 step 1 until N:
  y:=y+a(k)*cos(k*pi*x/L)+b(k)*sin(k*pi*x/L);
 endfor;
 p:=p--(x,y);
 p:=p xyscaled(ux,uy);
 draw p withcolor cyan;

endfig;


beginfig(0);

%enter number of terms
 numeric N;
 N=6;

%define L
 numeric L;
 L:=pi;

%define a_0
 ao:=1;

%define a_n
 vardef a(expr n)=
  2*sin(n*pi/2)/(n*pi)
 enddef;

%define b_n
 vardef b(expr n)=
  0
 enddef;

%initialize scale
 numeric ux, uy;
 6*pi*ux=2in; 1.5*uy=2in;

%draw axes
 drawdblarrow (-3.5*pi*ux,0)--(3.5*pi*ux,0);
 drawarrow (0,0)--(0,1.5uy);

%label axes
 label.rt(btex $x$ etex, (3.5*pi*ux,0));

%tick marks
 xtick((pi*ux,0));
 xtick((2*pi*ux,0));
 xtick((3*pi*ux,0));
 label.bot(btex $3\pi$ etex, (3*pi*ux,0));
 xtick((-pi*ux,0));
 xtick((-2*pi*ux,0));
 xtick((-3*pi*ux,0));
 label.bot(btex $-3\pi$ etex, (-3*pi*ux,0));
 ytick((0,1*uy));
 label.lft(btex $1$ etex, (0,1*uy));

%draw the function in black
 path q;
 q:=(-pi/2,1)--(pi/2,1);
 q:=q xyscaled(ux,uy);
 draw q;
 draw q shifted (2*pi*ux,0);
 draw q shifted (-2*pi*ux,0);

%draw Fourier approximation in cyan
 path p;
 numeric x, y;
 x:=-3*pi;
 y:=ao/2;
 for k=1 step 1 until N:
  y:=y+a(k)*cos(k*pi*x/L)+b(k)*sin(k*pi*x/L);
 endfor;
 p:=(x,y);
 for x=-3*pi step .1 until 3*pi:
  y:=ao/2;
  for k=1 step 1 until N:
   y:=y+a(k)*cos(k*pi*x/L)+b(k)*sin(k*pi*x/L);
  endfor;
  p:=p--(x,y);
 endfor;
 x:=3*pi;
 y:=ao/2;
 for k=1 step 1 until N:
  y:=y+a(k)*cos(k*pi*x/L)+b(k)*sin(k*pi*x/L);
 endfor;
 p:=p--(x,y);
 p:=p xyscaled(ux,uy);
 draw p withcolor cyan;

endfig;
end;


On Mar 26, 2005, at 3:19 PM, Gerben Wierda wrote:

I am trying to learn metapost/fun, inline in ConTeXt source. Some basic things are clear, but now the issue is metapost itself.

For instance, I would like to plot a Fourier approximation of a block function.

For instance, I would like to plot a gaussian spread.

I am looking for examples on how to do this. I need to do a bit of programming here and these are my initial projects.

Thanks in advance,

G

_______________________________________________
ntg-context mailing list
ntg-context@ntg.nl
http://www.ntg.nl/mailman/listinfo/ntg-context


_______________________________________________ ntg-context mailing list ntg-context@ntg.nl http://www.ntg.nl/mailman/listinfo/ntg-context

Reply via email to