unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls;
const MaxDimension = 10;
type
Vector = array[1..MaxDimension] of Double;
Matrix = array[1..MaxDimension] of Vector;
TForm3 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
StringGrid1: TStringGrid;
StringGrid2: TStringGrid;
Button1: TButton;
Label2: TLabel;
Label3: TLabel;
ListBox1: TListBox;
procedure Edit1Change(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
procedure TForm3.Button1Click(Sender: TObject);
var a: Matrix;
b,x: Vector;
h: Double;
i,j,k,n:integer;
begin
//Ввод данных
//Размерность системы
n := StrToIntDef(Text, StringGrid1.ColCount);
//Коэффициенты
for j := 0 to n - 1 do
for i := 0 to n - 1 do
a[i + 1, j + 1] := StrToFloatDef(StringGrid1.Cells[j, i], 0);
//Правая часть уравнения
for I := 0 to n - 1 do b[i + 1] := StrToFloatDef(StringGrid2.Cells[0, i], 0);
//Прямой ход - исключение переменных
for i:=1 to n-1 do
for j:=i+1 to n do
begin
a[j,i]:=-a[j,i]/a[i,i];
for k:=i+1 to n do
a[j,k]:=a[j,k]+a[j,i]*a[i,k];
b[j]:=b[j]+a[j,i]*b[i]
end;
x[n]:=b[n]/a[n,n]; //Деление на ноль
//Обратный ход - нахождение корней
for i:=n-1 downto 1 do
begin
h:=b[i];
for j:=i+1 to n do h:=h-x[j]*a[i,j];
x[i]:=h/a[i,i]
end;
//Вывод результата
for i:=1 to n do ListBox1.Items.Append('x(' + IntToStr(i) + ')=' + FloatToStr(x[i]));
end;
procedure TForm3.Edit1Change(Sender: TObject);
begin
with StringGrid1, Edit1 do
begin
ColCount := StrToIntDef(Text, 3);
RowCount := StrToIntDef(Text, 3);
end;
with StringGrid2, Edit1 do
RowCount := StrToIntDef(Text, 3)
end;
procedure TForm3.FormCreate(Sender: TObject);
var i, j: integer;
begin
//Заполнить коэф уравнения
Randomize;
for I := 0 to StrToIntDef(Text, StringGrid1.ColCount) - 1 do
for J := 0 to StrToIntDef(Text, StringGrid1.RowCount) - 1 do
StringGrid1.Cells[I, J] := IntToStr(Random(100));
for I := 0 to StrToIntDef(Text, StringGrid2.RowCount) - 1 do
StringGrid2.Cells[0, I] := IntToStr(Random(100))
end;
end.
//Прямой ход - исключение переменных
for i:=1 to n-1 do
begin
j:=i;
while (a[j,i]=0) and (j<=n) do inc(j);
if j<>i then
begin
for k:=1 to n do
begin
h:=a[i,k];
a[i,k]:=a[j,k];
a[j,k]:=h;
end;
h:=b[i];
b[i]:=b[j];
b[j]:=h;
end;
for j:=i+1 to n do
begin
a[j,i]:=-a[j,i]/a[i,i];
for k:=i+1 to n do
a[j,k]:=a[j,k]+a[j,i]*a[i,k];
b[j]:=b[j]+a[j,i]*b[i]
end;
end;
//Обратный ход - нахождение корней
for i:=n downto 1 do
begin
h:=b[i];
for j:=i+1 to n do
h:=h-x[j]*a[i,j];
x[i]:=h/a[i,i]
end;
Если Вы уже зарегистрированы на Портале - войдите в систему, если Вы еще не регистрировались - пройдите простую процедуру регистрации.