unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, TeeProcs, TeEngine, Chart, Grids, Series,
  jpeg;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Label11: TLabel;
    Edit3: TEdit;
    Label12: TLabel;
    Edit4: TEdit;
    Label13: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    Edit5: TEdit;
    Label16: TLabel;
    Edit6: TEdit;
    Label17: TLabel;
    Edit7: TEdit;
    Label18: TLabel;
    Label19: TLabel;
    Edit8: TEdit;
    Label20: TLabel;
    Edit9: TEdit;
    Label21: TLabel;
    Edit10: TEdit;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    StringGrid1: TStringGrid;
    Label22: TLabel;
    Chart1: TChart;
    Label23: TLabel;
    Edit11: TEdit;
    Series1: TLineSeries;
    Image1: TImage;
    Image2: TImage;
    Series2: TLineSeries;
    Series3: TLineSeries;
    Series4: TLineSeries;
    procedure Button3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button3Click(Sender: TObject);  //Кнопка "Закрыть"
begin
  close;
end;

procedure TForm1.Button1Click(Sender: TObject);  //Кнопка "Очистить"
var I,J: integer;
begin
  Edit1.Text := '';
  Edit2.Text := '';
  Edit3.Text := '';
  Edit4.Text := '';

  Edit5.Text := '';
  Edit6.Text := '';
  Edit7.Text := '';
  Edit8.Text := '';
  Edit9.Text := '';
  Edit10.Text := '';
  Edit11.Text := '';
  for I := 0 to 20 do
    for J := 0 to 20 do
      Form1.StringGrid1.Cells[I,J] := '';
  for I := 0 to 3 do Form1.Chart1.Series[I].Clear;
end;

procedure TForm1.Button2Click(Sender: TObject);  // Кнопка "Вычислить"
const Nmax = 20;
type
  Tmy = Array[1..Nmax, 1..Nmax] of real;
  Tmx = Array[1..Nmax] of real;
var
  My, Er: Tmy;
  Mx, Ma: Tmx;
  Xn,Dx,An,Ak,Da,Eps,C,D,Int,B: real;
  N,Km,Err,Ka: integer;

procedure Trap(C,D,Eps:real; Km:integer; var Int:real; var Err:integer); //Процедура вычисления интеграла
function F(X: real): real;
begin
  F := X*X*Exp(X)*cos(6*X)*cos(6*X);
end;
var
  Y1,Yn,Int1,X,H,Del: real;
  M,I,J: integer;
begin
  Y1 := F(C);
  Yn := F(D);
  Int1 := 0;
  M := 2;
  Err := 1;
  J := 1;
  while J <= Km do
  begin
    H := (D-C)/M;
    X := C + H;
    Int := 0;
    repeat
      Int := Int + F(X);
      X := X + H;
      I := I + 1;
    until I >= M;
    Int := H*(Y1/2 + Yn/2 + Int);
    Del := Abs(Int1-Int);
    if Del < Eps then
    begin
      J := Km;
      Err := Err - 1;
    end
    else
    begin
      Int1 := Int;
      M := M*2;
    end;
    J := J + 1;
  end;
end;

procedure Tab(An,Ak,Da,Xn,Dx,B:real; N:integer; var My, Er: Tmy; var Mx, Ma: Tmx; var Ka: integer); //Процедура табулирования функции
var
  A,X :real;
  I,J:integer;
begin
  A := An;
  I := 1;
  while A <= Ak do
  begin
    X := Xn;
    for J := 1 to N do
    begin
      Mx[J] := X;
      if x < 1 then My[I,J] := 4*B-exp(A*X)
        else
          if A*X+B*B > 0 then My[I,J] := (x*x*x)/(sqrt(A*X+B*B))
            else Er[I,J] := 1;
      X := X + Dx;
    end;
    Ma[I] := A;
    I := I + 1;
    A := A + Da;
  end;
  Ka := I-1;
end;

procedure RezOut(var Mx,Ma: Tmx; var My,Er: Tmy; Ka,N:integer); //Процедура вывода результатов
var
  I,J:integer;
begin
  //Формирование заголовков таблицы
  StringGrid1.Cells[0,0] := 'X/A';
  for I := 1 to Ka do
  begin
    StringGrid1.Cells[I,0]:=('A['+IntToStr(I)+']='+FloatToStr(Ma[I]));
    Form1.Chart1.Series[I-1].Title:='A['+IntToStr(I)+']';
    Form1.Chart1.Series[I-1].Clear;
    for J := 1 to N do
    begin
      with Form1.StringGrid1 do          
      begin
        Cells[0,J]:=('X['+IntToStr(J)+']='+FloatToStr(Mx[J]));
        if Er[I,J] = 1 then Cells[I,J]:= 'Err' else Cells[I,J]:=FloatToStrF(My[I,J],ffGeneral,6,5);
      end;
        Form1.Chart1.Series[I-1].AddXY(Mx[J],My[I,J]);
    end;
  end;
end;

begin
  //Исходные данные для вычисления интеграла
  C:= StrToFloat(Edit1.Text);
  D:= StrToFloat(Edit2.Text);
  Eps:= StrToFloat(Edit3.Text);
  Km:= StrToInt(Edit4.Text);
  //Исходные данные для табулирования
  Xn:= StrToFloat(Edit5.Text);
  Dx:= StrToFloat(Edit6.Text);
  N:= StrToInt(Edit7.Text);
  An:= StrToFloat(Edit8.Text);
  Ak:= StrToFloat(Edit9.Text);
  Da:= StrToFloat(Edit10.Text);
  Trap(C,D,Eps,Km,Int,Err);
  if Eps >= 1 then
    begin
      ShowMessage('Погрешность Eps должна быть < 1');
      exit;
    end
  else
  if Err = 1 then
  begin
    ShowMessage('Интеграл не найден за '+IntToStr(Km)+' итераций');
    exit;
  end
  else
  begin
     B := Int;
     Edit11.Text := FloatToStrF(B,ffGeneral,10,6);
     Tab(An,Ak,Da,Xn,Dx,B,N,My,Er,Mx,Ma,Ka);
     RezOut(Mx,Ma,My,Er,Ka,N);
  end;
end;

end.
