Graphics of functions

Ivan Ivanov <[email protected]> Mon, 29 Sep 2003 11:18:26 -0700 (PDT)
Newsgroups gmane.comp.tex.metapost,gmane.comp.tex.metafont
Message-ID <[email protected]>
Dear List,
please help me with what troubles my mind now. I have
some functions given analytically like f(x)=x^3-x+1 or
f(x)=-(x+1)*exp(x-1) and I want to draw their graphics
using MetaPost. Upto to now I found two ways but they
are not comfortable for my needs. The first one is the
obvious:
pair p[];
numeric i;
i:=0;
for x := a step .01 until b:
  numeric y;
  % y := f(x);
  y := x*x*x-x+1;
  p[i] := (x*1cm, y*1cm);
  i := i+1;
endfor;
for j := 1 upto i;
  draw p[j-1]..p[j];
endfor;

and the second one was to calculate again some values
of f(x), write them in file f.d and use this file in
this way:
draw begingraph(10cm,10cm);
  gdraw("f.d");
endgraph;

Both ways proved to be tedious since I want later to
decorate the graphics by contouring parts of the
graphics with thicker lines or adding labels to
specific points (for example where it crosses the
axes). The problem with the labels is that I had to
recalculate again the index j of the needed point p[j]
so i cannot use intersectionpoint.
Furthermore, I am trying to contour a part of the
graphics between point p[k] and point p[l] in this
way:for t=k+1 upto l:
  draw ((p[t-1]..p[t]) shifted (0,.2cm)) withcolor red
withpen pencircle scaled .2cm
endfor;
As a result there are some large gaps between the very
graphics and the coloured graphics. I fixed some of
these misbehaviours manually but it was now easy.

These are my pains with my functions. Thank you very
much in advance.
Kind Regards 
Ivan Ivanov

P.S. I am doing such things because I am preparing
some lecture notes in Math Analysis. So I hope someone
managed to do it with MetaPost

__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com