Uses CRT,GRAPH;
Const
  Eps = 0.01;
  Del = $ffff;
  LeftGr = -10;
  RightGr = 10;
Procedure InitG;
Var
  grDriver: Integer;
  grMode: Integer;
  i : integer;
Begin
  grDriver := Detect;
  InitGraph(grDriver, grMode,'');
  SetFillStyle(XHatchFill,Green);
  Bar(0,0,GetMaxX,GetMaxY);
  SetFillStyle(SolidFill,Black);
  Bar(100,50,540,450);
  SetColor(Red);
  Line(100,250,540,250);
End;{InitG}
{--------------------------------}
Function Func(x: real): real;
Begin
{  Func  := sin(PI*x/4)/cos(PI*x/4)-x+3;}
  Func  := x * x;
End;
{-----------------------------}
Procedure WriteGraph(l,r,Color:Integer);
Var
  x,y,Razm : Integer;
Begin
  Razm := r-l;
  SetColor(Color);
  MoveTo(100,250-Round(Func(l)));
  for x := L to R do
    begin
      y := round(Func(x));
      LineTo(round(100+(x-l)*((440)/razm)),250-y);
    end;
End;{WriteGraph}
{---------------------------------}
Function AddX(x : real):integer;
Begin
  AddX :=round(100+(x-leftGR)*((440)/(RightGr-LeftGr)));
End;
{-----------------}
Procedure Calculate;
Var
  R,L,dx,lx,b,k,x : real;
  str1 : string;
Begin
  R := RightGr;
  L := LeftGr;
  WriteGraph(round(L),round(R),Blue);
  dx := maxint;
  lx := 0;
  x := r;
  repeat
      setcolor(green);
      line(addx(l),250-round(Func(l)),addx(x),250-round(Func(x)));
      Delay(DEL);
      line(addx(x),250-round(Func(x)),addx(r),250-round(Func(r)));
      Delay(DEL);
      k := (Func(x)-Func(l))/(x-l);
      b := func(x)-k*x;
      if (-b/k>=l)and(-b/k<=x) then
        begin
          r := x;
          x := -b/k;
        end
      else
        begin
         k := (Func(r)-Func(x))/(r-x);
         b := func(r)-k*r;
         l := x;
         x := -b/k;
        end;
        dx := abs(lx-x);
        lx := x;
      setcolor(yellow);
      line(addx(x),250,addx(x),250-round(Func(x)));
      delay(del);
  until dx < Eps;
  setcolor(yellow);
  circle(addx(x),250,5);
  SetColor(Magenta);
  SetTextJustify(LeftText,Centertext);
  SetTextStyle(DefaultFont,HorizDir,4);
  OutTextXY(250,20,'Done');
  str(x:1:6,str1);
  SetTextStyle(Defaultfont,HorizDir,3);
  insert('X = ',str1,1);
  OutTextXY(150,440,str1);
End;
{------------------}
Begin
  InitG;
  Calculate;
  ReadKey;
  CloseGraph;
End.