{############################################################
 # 10 Variant        f1=1+4/(x^2+1)    f2=x^3    f3=2^(-x)  #
 # 02 Variant        Metod Hord                             #
 # 02 Variant        Metod Trapeciy                         #
 ############################################################}

{!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 ! VNIMANIE :: pered zapuskom programmy neobhodimo vkluchit'!
 ! Options -> Compiler -> 8086/80286;                       !
 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!}

{::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 :: Dla priblijeniya grafika ispolzuyte + i -;             ::
 :: Dla peremechenia mejdu tochkami peresechenia UP i DOWN ::
 ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::}

{$N+}

program Integral_Tka4enko_112(input, output);

 uses InGraph,       { Graphi4eskiy modul }
      Functions,     { Modul s opisaniem funkciy }
      TypesAndConst, { Types i constants}
      graph, crt;

{--- ROOT --- }
procedure root(fq, gq: FFunc; a, b, eps: real; var x: real);
var
  c, fa, fb : real;
  FirstType : boolean;
 function F(x : real) : real;
  begin
   F := fq(x) - gq(x);
  end;
begin
 c := (a+b)/2;
  FirstType := not (F(a) < 0) xor
   (F(c) < (F(b) - F(a)) * (c - a) / (b-a) + F(a));
  repeat
   fa := F(a); fb := F(b);
    c := (a*fb - b*fa) / (fb - fa);
     if FirstType then a := c else b := c;
  until F(c)*F(c+eps) < 0;
 x := (c+eps/2);
end;


{--- INTEGRAL ---}
function integral(f: FFunc; a, b, eps: real): real;
var
  n, i : integer;
  I_n, I_2n, h, epsi : real;
begin
 epsi := eps * 3.0;
  n := 1;
  h := b-a;
   I_2n := h*(f(a + h/2));
  repeat
   I_n := I_2n;
   I_2n := 0;
    h := h/2;
    n := n*2;
     for i := 1 to n do
        I_2n := I_2n + f( a + h*( i - 0.5) ) ;
    I_2n := I_2n*h;
  until abs(I_n-I_2n) < epsi;
integral := I_2n;
end;




{--- MAIN PROGRAM ---}
var sys : TSystem;
begin
 clrscr;

  sys.a := -3;
  sys.b :=  3;
  sys.epsroot := 0.00001;
  sys.epsint  := 0.0001;

  root(f1, f2, sys.a, sys.b, sys.epsroot, sys.crp[1].x);
   sys.crp[1].y := f1(sys.crp[1].x);
  root(f1, f3, sys.a, sys.b, sys.epsroot, sys.crp[2].x);
   sys.crp[2].y := f1(sys.crp[2].x);
  root(f2, f3, sys.a, sys.b, sys.epsroot, sys.crp[3].x);
   sys.crp[3].y := f3(sys.crp[3].x);

  sys.I[1] := integral(f1, sys.crp[2].x, sys.crp[1].x, sys.epsint);
  sys.I[2] := integral(f2, sys.crp[1].x, sys.crp[3].x, sys.epsint);
  sys.I[3] := integral(f3, sys.crp[2].x, sys.crp[3].x, sys.epsint);

  sys.Iall := abs(sys.I[1]) - abs(sys.I[2]) - abs(sys.I[3]);

{   if ingraph.Init then
    { esli inicilizacia graphiki prowla uspewno, zapuskaem obolochku
      ShellWork(f1,f2,f3, sys);}

  writeln(' f1 crossed f3 at point =', sys.crp[1].x);
  writeln(' f2 crossed f3 at point =', sys.crp[2].x);
  writeln(' f1 crossed f2 at point =', sys.crp[3].x);

  writeln;

  writeln(' | I(f1, x2, x1) - I(f3, x1, x3) - I(f2, x2, x3) | = ', sys.Iall);

  writeln;

  writeln(f1(2)-f2(2));

 readln;
end.
