program e_1 (input,output);
uses crt, graph;
type TF = function(x:real):real;
var x1,x2,x3,I1,I2,I,eps1,eps:real;
    n0:integer;
      grDriver: Integer;
      grMode: Integer;
      ErrCode: Integer;
{$F+}
Function F1(x:real):real;
         begin
         f1:=3/((x-1)*(x-1)+1)
         end;
Function F11(x:real):real;
         begin
         f11:=-6*(x-1)/sqr((x-1)*(x-1)+1)
         end;
Function F2(x:real):real;
         begin
         f2:=sqrt(x+0.5)
         end;
Function F21(x:real):real;
         begin
         f21:=0.5/sqrt(x+0.5)
         end;
Function F3(x:real):real;
         begin
         f3:=exp(-x);
         end;
Function F31(x:real):real;
         begin
         f31:=-exp(-x)
         end;
procedure root(f,f1,g,g1:Tf; a,b,eps1:real; var x:real);
var x11,x12,e1,e,l,d:real;
    begin
    repeat
    l:=(f1(a)-g1(a))*((f((A+b)/2)-g((a+b)/2)-(f(b)-g(b)-F(a)+G(b))/2));
    if l<0 then begin d:=b; e:=a end else begin D:=a; e:=b end;
    x11:=d-(f(d)-g(d))/(f1(d)-g1(d));
    x12:=e-(d-e)*(f(E)-g(e))/(f(d)-g(d)-f(E)+g(e));
    e1:=(x11+x12)/2;
    if l<0
       then begin a:=x12;b:=x11 end
       else begin a:=x11;b:=x12 end;
    until abs(e1-x11)<eps1;
    x:=e1
    end;
Function ft(n:integer; f,g:TF; a,b:real):real;
var x,IntN,h:real;
         begin
         x:=a;
         IntN:=(f(a)-g(a)+f(b)+g(b))/2;
         h:=(b-a)/n;
         while x<b-h/2 do
               begin
               x:=x+h;
               IntN:=IntN+f(x)-g(x)
               end;
         ft:=IntN*h
         end;
function integral(a,b,eps:real; f,g:Tf; n0:integer):real;
    var IntN,Int2N:real;
    begin
    repeat
          IntN:=ft(n0, f, g, a, b);
          n0:=2*n0;
          Int2N:=ft(n0, f, g, a, b)
    until abs(IntN-Int2N)<(3*eps);
    writeln('   pri n0=',n0,' pogreshnost ne bolshe ',eps:5:3);
    integral:=IntN
    end;
begin clrscr;
repeat
      writeln ('Program has been made in order to find points of intersection of functions:');
      writeln ('        F1=3/((x-1)^2+1),  F2=sqrt(x+0.5),  F3=e^(-x)     ');
      writeln ('and to count the area of the figure limited with these three functions');
      readln;
      eps:=0.001;
      root(f1, f11, f3, f31, -0.35, -0.15, eps, x1);
      Writeln('  x1=',x1:5:3,' - point of intersection of f1 i f3, counted with eps=',eps:5:3);
      root(f2, f21, f3, f31, 0.15, 0.35, eps, x2);
      Writeln('  x2= ',x2:5:3,' - point of intersection of f2 i f3, counted with eps=',eps:5:3);
      root(f1, f11, f2, f21, 1.85, 2.25, eps, x3);
      Writeln('  x3= ',x3:5:3,' - point of intersection of f2 i f1, counted with eps=',eps:5:3);
      readln;
      write ('Vvedite nachalnoe chislo povtorenii n0:= ');
      readln (n0);
      writeln;
      I1:=integral(x1, x2, eps, f1, f3, n0);
      I2:=integral(x2, x3, eps, f1, f2, n0);
      I:=I1+I2;
      readln;
      Writeln('The area of figure S=',I:5:3,' limited of f1,f2,f3,');
      Writeln('counted with eps=',eps:5:3);
      readln;
      writeln('Do you want to continue computings');
      writeln('even number and 0 to quit, not even - to continue');
      readln(n0);
      grDriver := Detect;
      InitGraph(grDriver, grMode,'');
      ErrCode := GraphResult;
      if ErrCode = grOk then
      begin
        { Do graphics }


        Line(0, 0, GetMaxX, GetMaxY);
        Readln;
        CloseGraph;
      end
    else
    Writeln('Graphics error:', GraphErrorMsg(ErrCode));
until not odd(n0) or (n0=0)


  {grDriver := Detect;
  InitGraph(grDriver, grMode,'');
  ErrCode := GraphResult;
  if ErrCode = grOk then
  begin  { Do graphics }
    {Line(0, 0, GetMaxX, GetMaxY);
    Readln;
    CloseGraph;
  end
  else
    Writeln('Graphics error:', GraphErrorMsg(ErrCode));  }





end.








