Re: Graphics of functions

Emil Hedevang Lohse <[email protected]> Mon, 29 Sep 2003 22:45:51 +0200
Newsgroups gmane.comp.tex.metapost,gmane.comp.tex.metafont
Organization Gul and
Message-ID <[email protected]>
Ivan Ivanov <[email protected]> writes:

> 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;

I think a solution to your problem is to store the whole graph of the
funktion i a single path:

  vardef f(expr x) = x*x*x-x+1 enddef;  

  u = 1cm;
  path p;
  numeric steps;
  steps := 1000;
  p := (a*u, f(a)*u)
    for x = 1 upto steps:
      .. ((a+(b-a)*x/steps)*u, f(a+(b-a)*x/steps)*u)
    endfor;

Notice how the for loop is used to build the path, this is a very nice
thing about METAPOST.

Regards,

-- 
Emil Hedevang Lohse <http://home.imf.au.dk/emil/> 

Alle spørgsmål er lige dumme. 
Og spørgsmålet "Kan ænder flyve?" er ikke dumt.