input courbescp13;
input TEX;

vardef flechemilieu(expr aa,bb)=
  save fle;
  picture fle;
  fle=image(
    drawarrow aa--(1/2[aa,bb]);
    draw (1/2[aa,bb])--bb;
    );
  fle
enddef;

vardef suite(expr uu,npt)(text fonct)=%uu=u0,npt=rang maximal à placer, fonct : porte bien son nom :)
  save $;
  picture $;
  numeric dep;%depart
  dep=uu;
  $=image(
    for i:=1 upto npt:
      x:=dep;
      y@[i]:=fonct;
      if i=1 :
	draw flechemilieu(placepoint(dep,0),placepoint(dep,y@[i]));
	draw flechemilieu(placepoint(dep,y@[i]),placepoint(y@[i],y@[i]));
	fill (fullcircle scaled 1mm shifted placepoint(dep,0)) withcolor violet;
      fi;
      fill (fullcircle scaled 1mm shifted placepoint(y@[i],0)) withcolor orange;
      label.bot(TEX("$u_"&decimal(i)&"$"),placepoint(y@[i],0));
      if i<>1:
	draw flechemilieu(placepoint(dep,dep),placepoint(dep,y@[i]));
	draw flechemilieu(placepoint(dep,y@[i]),placepoint(y@[i],y@[i]));
      fi;
      dep:=y@[i];
    endfor;
    );
  $
enddef;


beginfig(1);
  cadre((0,0),(13,13));
  origine((1,1));
  unites(4,4);
  draw grille(0.1) withcolor gris;
  draw axes;
  graduantx.bot;
  graduanty.lft;
  draw courbe1(0,3,100,x**2) withcolor bleu;
  draw labelise1(btex $y=x^2$ etex,0.55);
  draw courbe2(0,3,2,x) withcolor rouge;
  draw suite(1.1,3,x**2);
  label.bot(btex $u_0$ etex,placepoint(1.1,0));
  draw suite(0.8,4,x**2);
  label.bot(btex $v_0$ etex,placepoint(0.8,0));
endfig;


beginfig(2);
  cadre((0,0),(13,13));
  origine((1,1));
  unites(11,11);
  draw grille(0.1) withcolor gris;
  draw axes;
  graduantx.bot;
  graduanty.lft;
  draw courbe1(0,2,100,-((x-0.5)**2)+1) withcolor bleu;
  draw labelise1(btex $y=-(x-0,5)^2+1$ etex,0.1);
  draw courbe2(0,3,2,x) withcolor rouge;
  draw suite(0.5,8,-((x-0.5)**2)+1);
  label.bot(btex $u_0$ etex,placepoint(0.5,0));
endfig;


beginfig(3);
  cadre((0,0),(13,13));
  origine((1,1));
  unites(8,8);
  draw grille(0.1) withcolor gris;
  draw axes;
  graduantx.bot;
  graduanty.lft;
  draw courbe1(0,2,100,sqrt(x)) withcolor bleu;
  draw labelise1(btex $y=\sqrt x$ etex,0.7);
  draw courbe2(0,3,2,x) withcolor rouge;
  draw suite(0.5,3,sqrt(x));
  label.bot(btex $u_0$ etex,placepoint(0.5,0));
endfig;
end