Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Отчет Расчетка ВЫЧМАТ.docx
Скачиваний:
0
Добавлен:
01.03.2025
Размер:
3.19 Mб
Скачать

3.7 Метод наименьших квадратов

x

0

1

2

3

4

5

y

2,1

5,9

12,2

19,8

30,3

41,6

3.7.1 Листинг

unit Unitmnk;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

Grids, StdCtrls, ExtCtrls, jpeg,Math;

type

TForm1 = class(TForm)

SG1: TStringGrid;

Button2: TButton;

Image1: TImage;

Button3: TButton;

Button4: TButton;

Button5: TButton;

Button6: TButton;

lbl7: TLabel;

bvl1: TBevel;

lbl10: TLabel;

lbl1: TLabel;

lbl2: TLabel;

lbl3: TLabel;

lbl4: TLabel;

lbl5: TLabel;

lbl6: TLabel;

Image2: TImage;

lbl9: TLabel;

Edit1: TEdit;

Edit2: TEdit;

lbl8: TLabel;

lbl11: TLabel;

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

procedure Button3Click(Sender: TObject);

procedure Button4Click(Sender: TObject);

procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

procedure FormCreate(Sender: TObject);

procedure Button5Click(Sender: TObject);

procedure Button6Click(Sender: TObject);

private

{ Private declarations }

public

function f(m:extended; var Xmax,Xmin,Ymax,Ymin:extended):extended;

function f2(m:extended; var Xmax,Xmin,Ymax,Ymin:extended):extended;

{ Public declarations }

end;

var

Form1: TForm1;

implementation

var kol,i,j,k,l,k1,n1,n,rcnt,check,check2:integer;

x,fk,t:array [1..50] of extended;

fksum,xsum,a,b,v,w:extended;

Xmax,Xmin,Ymax,Ymin,summa,summa2:extended;

matrix: array [1..10,1..11] of extended;

r,s,xsumkv,xsumkub,xsum4,fksum1, fksum2, fksum3:extended;

{$R *.DFM}

function Tform1.f(m:extended; var Xmax,Xmin,Ymax,Ymin:extended):extended;

Begin

for i:=1 to kol do begin

x[i]:=StrToFloat(SG1.Cells[0,i-1]);

fk[i]:=StrToFloat(SG1.Cells[1,i-1]);

end;

fksum:=0;

xsum:=0;

for i:=1 to kol do begin

fksum:=fksum+fk[i];

xsum:=xsum+x[i];

end;

fksum:=fksum/(kol);

xsum:=xsum/(kol);

b:=0;

v:=0;

w:=0;

for i:=1 to kol do begin

v:=v+(x[i]-xsum)*(fk[i]-fksum); //верхняя сумма

w:=w+sqr(x[i]-xsum); //нижняя сумма

end;

b:=v/w;

a:=fksum-b*xsum;

f:=a+b*m;

End;

function Tform1.f2(m:extended; var Xmax,Xmin,Ymax,Ymin:extended):extended;

Begin

for i:=1 to kol do begin

x[i]:=StrToFloat(SG1.Cells[0,i-1]);

fk[i]:=StrToFloat(SG1.Cells[1,i-1]);

end;

{ФУНКЦИЯ!!!!}

xsum:=0;

xsumkv:=0;

xsumkub:=0;

xsum4:=0;

fksum1:=0;

fksum2:=0;

fksum3:=0;

for i:=1 to kol do begin

xsum:=xsum+x[i];

xsumkv:=xsumkv+sqr(x[i]);

xsumkub:=xsumkub+sqr(x[i])*x[i];

xsum4:=xsum4+sqr(sqr(x[i]));

fksum1:=fksum1+fk[i];

fksum2:=fksum2+fk[i]*x[i];

fksum3:=fksum3+fk[i]*sqr(x[i]);

end;

matrix[1,1]:=kol+1;

matrix[1,2]:=xsum;

matrix[1,3]:=xsumkv;

matrix[1,4]:=fksum1;

matrix[2,1]:=xsum;

matrix[2,2]:=xsumkv;

matrix[2,3]:=xsumkub;

matrix[2,4]:=fksum2;

matrix[3,1]:=xsumkv;

matrix[3,2]:=xsumkub;

matrix[3,3]:=xsum4;

matrix[3,4]:=fksum3;

n:=3;

n1:=n+1;

for k:=1 to n do begin k1:=k+1;

s:=matrix[k,k];

for j:=k1 to n1 do matrix[k,j]:=matrix[k,j]/s;

for i:=k1 to n do begin r:=matrix[i,k];

for j:=k1 to n1 do matrix[i,j]:=matrix[i,j]-matrix[k,j]*r;

end;

end;

for i:=n downto 1 do begin s:=matrix[i,n1];

for j:=i+1 to n do s:=s-matrix[i,j]*t[j];

t[i]:=s;

end;

f2:=t[1]+t[2]*m+t[3]*sqr(m);

End;

procedure TForm1.Button1Click(Sender: TObject);

begin

button2.Visible:=true;

button3.Visible:=true;

button4.Visible:=true;

SG1.Visible:=true;

SG1.rowcount:=kol;

with SG1 do

for i:=0 to RowCount do //очищаем её

for j:=0 to ColCount do

Cells[j, i]:='';

SG1.height:=SG1.DefaultRowHeight*kol+10;

end;

procedure TForm1.Button2Click(Sender: TObject);

var x,y:real;

PX,PY:longInt;

o:Integer;

begin

image1.Canvas.MoveTo(0,Image1.height div 2);

image1.Canvas.LineTo(Image1.width,Image1.height div 2);

image1.Canvas.MoveTo(Image1.width div 2,0);

image1.Canvas.LineTo(Image1.width div 2,Image1.height);

image1.Canvas.MoveTo(0,Image1.height);

for PX:=0 to Image1.Width do

begin

x:=Xmin+PX*(Xmax-Xmin)/Image1.Width;

y:=F(x,Xmax,Xmin,Ymax,Ymin);

for o:=0 to SG1.RowCount-1 do begin

if (round(x)=round(StrToFloat(SG1.Cells[0,o]))) then begin

summa2:=summa2+Power((y-StrToFloat(SG1.Cells[1,o])),2);

// ShowMessage('Сумма квадратов отклонений для полинома 1 '+FloatToStr(summa2));

check2:=check2+1;

if check2=1 then

if check2=5 then check2:=0;

end;

end;

PY:=round(Image1.Height-(y-Ymin)*Image1.height/(Ymax-Ymin));

Image1.Canvas.LineTo(PX,PY);

{image1.Canvas.Pixels[PX,PY]:=clRed; }

//Button2.Enabled:=FALSE;

end;

ShowMessage('Сумма квадратов отклонений для полинома первой степени '+FloatToStr(summa2));

Edit2.Text:=FloatToStr(summa2);

end;

procedure TForm1.Button3Click(Sender: TObject);

var y:extended;

px,PY:longInt;

begin

image1.Canvas.MoveTo(0,Image1.height div 2);

image1.Canvas.LineTo(Image1.width,Image1.height div 2);

image1.Canvas.MoveTo(Image1.width div 2,0);

image1.Canvas.LineTo(Image1.width div 2,Image1.height);

image1.Canvas.MoveTo(0,Image1.height);

for i:=1 to kol do begin

x[i]:=StrToFloat(SG1.Cells[0,i-1]);

fk[i]:=StrToFloat(SG1.Cells[1,i-1]);

end;

for i:=1 to kol do begin

px:=round(Image1.width div 2+x[i]*Image1.width/(Xmax-Xmin));

py:=round(Image1.height div 2-fk[i]*Image1.width/(Xmax-Xmin));

image1.Canvas.Ellipse(px-5, py-5, px+5, py+5);

end;

end;

procedure TForm1.Button4Click(Sender: TObject);

var x,y,m,b:real;

n:string;

PX,PY:longInt;

o:Integer;

begin

{image1.Canvas.MoveTo(0,Image1.height div 2);

image1.Canvas.LineTo(Image1.width,Image1.height div 2);

image1.Canvas.MoveTo(Image1.width div 2,0);

image1.Canvas.LineTo(Image1.width div 2,Image1.height);

image1.Canvas.MoveTo(0,Image1.height);}

for PX:=0 to Image1.Width do

begin

x:=Xmin+PX*(Xmax-Xmin)/Image1.Width;

y:=F2(x,Xmax,Xmin,Ymax,Ymin);

for o:=0 to SG1.RowCount-1 do begin

if (round(x)=round(StrToFloat(SG1.Cells[0,o]))) then begin

summa:=summa+Power((y-StrToFloat(SG1.Cells[1,o])),2);

check:=check+1;

if check=1 then

if check=5 then check:=0;

end;

end;

PY:=round(Image1.Height-(y-Ymin)*Image1.height/(Ymax-Ymin));

Image1.Canvas.LineTo(PX,PY);

{image1.Canvas.Pixels[PX,PY]:=clRed; }

//Button4.Enabled:=FALSE;

end;

ShowMessage('Сумма квадратов отклонений для полинома второй степени = '+FloatToStr(summa));

Edit1.Text:=FloatToStr(summa);

end;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

var x1,y1:extended;

PX,PY:longInt;

begin

SG1.Visible:=True;

x1:=Xmin+x*(Xmax-Xmin)/Image1.Width; //переводим из пикселей в наши координаты

y1:=(Xmax-Xmin)*(Image1.Height div 2-y)/Image1.Width;

if rcnt<>0 then SG1.rowcount:=SG1.rowcount+1;

SG1.Cells[0,SG1.rowcount-1]:=FloatToStr(x1);

SG1.Cells[1,SG1.rowcount-1]:=FloatToStr(y1);

SG1.height:=SG1.DefaultRowHeight*SG1.rowcount+15;

px:=round(Image1.width div 2+x1*Image1.width/(Xmax-Xmin));

py:=round(Image1.height div 2-y1*Image1.width/(Xmax-Xmin));

image1.Canvas.Ellipse(px-5, py-5, px+5, py+5);

kol:=kol+1;

rcnt:=1;

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

image1.Canvas.MoveTo(0,Image1.height div 2);

image1.Canvas.LineTo(Image1.width,Image1.height div 2);

image1.Canvas.MoveTo(Image1.width div 2,0);

image1.Canvas.LineTo(Image1.width div 2,Image1.height);

image1.Canvas.MoveTo(0,Image1.height);

//SG1.Visible:=false;

kol:=0;

Xmax:=40;

Xmin:=-40;

Ymax:=40;

Ymin:=-40;

rcnt:=0;

summa:=0;

summa2:=0;

check:=0;

check2:=0;

end;

procedure TForm1.Button5Click(Sender: TObject);

begin

Button3.visible:=true;

Button6.visible:=true;

Button4.visible:=true;

Button2.visible:=true;

image1.Canvas.MoveTo(0,Image1.height div 2);

image1.Canvas.LineTo(Image1.width,Image1.height div 2);

image1.Canvas.MoveTo(Image1.width div 2,0);

image1.Canvas.LineTo(Image1.width div 2,Image1.height);

image1.Canvas.MoveTo(0,Image1.height);

SG1.Visible:=true;

kol:=kol+1;

SG1.rowcount:=kol;

SG1.height:=SG1.DefaultRowHeight*kol+15;

//Button5.Enabled:=FALSE;

end;

procedure TForm1.Button6Click(Sender: TObject);

begin

Image1.Canvas.FillRect(Rect(0,0,Width,Height));

image1.Canvas.MoveTo(0,Image1.height div 2);

image1.Canvas.LineTo(Image1.width,Image1.height div 2);

image1.Canvas.MoveTo(Image1.width div 2,0);

image1.Canvas.LineTo(Image1.width div 2,Image1.height);

image1.Canvas.MoveTo(0,Image1.height);

end;

{procedure TForm1.Button8Click(Sender: TObject);

begin

Edit1.Text:=FloatToStr(xsumkv)

end;}

end.