Среда программирования:
Borland Delphi 7.0
Статья по теме:
Мы можем построить по любому произвольно задаваемому набору точек
среднеквадратическое приближение методом наименьших квадратов.
Вводим данные как показано на примере, и программа строит МНК для линейных, квадратичных и степенных функций.
Код программы:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, Math; type TForm1 = class(TForm) PaintBox1: TPaintBox; Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Label1: TLabel; Label2: TLabel; Label3: TLabel; Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; Edit4: TEdit; Edit5: TEdit; Edit6: TEdit; Edit7: TEdit; Edit8: TEdit; Edit9: TEdit; Edit10: TEdit; Edit11: TEdit; Edit12: TEdit; Edit13: TEdit; Edit14: TEdit; Edit15: TEdit; Edit16: TEdit; Edit17: TEdit; Edit18: TEdit; Edit19: TEdit; Edit20: TEdit; Label4: TLabel; Label5: TLabel; Label6: TLabel; Label7: TLabel; Button5: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; xmin, xmax, ymin, ymax, ris1, ris2 : real; { математические координаты } a,b,di:integer; N:integer; l,k,j:real; Func : Array [1..10] of TPoint; i:integer; implementation {$R *.dfm} {функции перевода в экранные координаты} FUNCTION Xs( x : real) : integer; BEGIN Xs:= round( a * (x-xmin)/(xmax-xmin) ); END; FUNCTION Ys( y : real) : integer; BEGIN Ys := round( b * (1-(y-ymin)/(ymax-ymin)) ); END; procedure TForm1.Button1Click(Sender: TObject); begin PaintBox1.Canvas.Pen.Color:=clRed; {задание масштаба} xmin := 0; xmax := 15; ymin := 0; ymax := 15; a:=Paintbox1.Width; b:=Paintbox1.Height; {зарисовка поля} PaintBox1.Canvas.Brush.Color:=clWhite; Paintbox1.Canvas.Rectangle(0,0,a,b); PaintBox1.Canvas.Pen.Color:=clBlack; {координатные оси} PaintBox1.Canvas.MoveTo(Xs(xmin),Ys(0)); PaintBox1.Canvas.LineTo(Xs(xmax),Ys(0)); PaintBox1.Canvas.MoveTo(Xs(0),Ys(ymax)); PaintBox1.Canvas.LineTo(Xs(0),Ys(ymin)); {рисочки} {риска от xmin до xmax} PaintBox1.Canvas.Pen.Color:=clRed; ris1:=xmin; while (ris1<=xmax) do begin di:=Round(ris1); PaintBox1.Canvas.MoveTo(Xs(di),Ys(0.1)); PaintBox1.Canvas.LineTo(Xs(di),Ys(-0.1)); ris1:=ris1+0.5; end; PaintBox1.Canvas.Pen.Color:=clBlack; PaintBox1.Canvas.MoveTo(Xs(0),Ys(0.1)); PaintBox1.Canvas.LineTo(Xs(0),Ys(-0.1)); {риска от ymin до ymax} PaintBox1.Canvas.Pen.Color:=clRed; ris2:=ymin; while (ris2<=ymax) do begin di:=Round(ris2); PaintBox1.Canvas.MoveTo(Xs(-0.05),Ys(di)); PaintBox1.Canvas.LineTo(Xs(0.05),Ys(di)); ris2:=ris2+0.5; end; PaintBox1.Canvas.Pen.Color:=clBlack; PaintBox1.Canvas.MoveTo(Xs(-0.1),Ys(0)); PaintBox1.Canvas.LineTo(Xs(0.1),Ys(0)); // t:=-1; //while (t<=1) do begin //PaintBox1.Canvas.Pixels[Xs(t), Ys((1/(1+17*t*t)))]:= clBlack; //t:=t+0.01; N:=10; PaintBox1.Canvas.Brush.Color:=clRed; PaintBox1.Canvas.Pen.Color:=clRed; { for i:=1 to 10 do begin Func[i].x:=1+Random(10); Func[i].y:=1+Random(10); PaintBox1.Canvas.Ellipse(Xs(Func[i].x+0.03),Ys(Func[i].y+0.05),Xs(Func[i].x-0.03),Ys(Func[i].y-0.05)); end; } Func[1].x:=StrToInt(Edit1.Text); Func[1].y:=StrToInt(Edit2.Text); Func[2].x:=StrToInt(Edit3.Text); Func[2].y:=StrToInt(Edit4.Text); Func[3].x:=StrToInt(Edit5.Text); Func[3].y:=StrToInt(Edit6.Text); Func[4].x:=StrToInt(Edit7.Text); Func[4].y:=StrToInt(Edit8.Text); Func[5].x:=StrToInt(Edit9.Text); Func[5].y:=StrToInt(Edit10.Text); Func[6].x:=StrToInt(Edit11.Text); Func[6].y:=StrToInt(Edit12.Text); Func[7].x:=StrToInt(Edit13.Text); Func[7].y:=StrToInt(Edit14.Text); Func[8].x:=StrToInt(Edit15.Text); Func[8].y:=StrToInt(Edit16.Text); Func[9].x:=StrToInt(Edit17.Text); Func[9].y:=StrToInt(Edit18.Text); Func[10].x:=StrToInt(Edit19.Text); Func[10].y:=StrToInt(Edit20.Text); for i:=1 to 10 do begin PaintBox1.Canvas.Ellipse(Xs(Func[i].x+0.03),Ys(Func[i].y+0.05),Xs(Func[i].x-0.03),Ys(Func[i].y-0.05)); end; Label1.Caption:=''; Label2.Caption:=''; Label3.Caption:=''; Label5.Caption:=''; Label6.Caption:=''; Label7.Caption:=''; end; //end; procedure TForm1.Button2Click(Sender: TObject); var i: integer; delta:real; delta1:real; delta2:real; a0:real; a1:real; t:real; Sx:real; Sx2:real; Sy:real; Sxy:real; begin Sx:=0; Sx2:=0; Sy:=0; Sxy:=0; for i:=1 to 10 do begin Sx:=Sx+Func[i].x; Sx2:=Sx2+(Func[i].x)*(Func[i].x); Sy:=Sy+Func[i].y; Sxy:=Sxy+(Func[i].x)*(Func[i].y); end; delta:=N*Sx2-Sx*Sx; delta1:=Sy*Sx2-Sx*Sxy; delta2:=N*Sxy-Sy*Sx; a0:=delta1/delta; a1:=delta2/delta; t:=0; while (t<=xmax) do begin PaintBox1.Canvas.Pixels[Xs(t), Ys(a0+a1*t)]:= clBlack; t:=t+0.01; end; l:=0; for i:=1 to 10 do begin l:=l+(Func[i].y-(a0+a1*Func[i].x))*(Func[i].y-(a0+a1*Func[i].x)); end; Label1.Caption:=FloatToStr(l); end; procedure TForm1.Button3Click(Sender: TObject); var i: integer; delta:real; delta1:real; delta2:real; delta3:real; a0:real; a1:real; a2:real; t:real; Sx:real; Sx2:real; Sy:real; Sxy:real; Sx3:real; Sx4:real; Sx2y:real; begin Sx:=0; Sx2:=0; Sy:=0; Sxy:=0; Sx3:=0; Sx4:=0; Sx2y:=0; for i:=1 to 10 do begin Sx:=Sx+Func[i].x; Sx2:=Sx2+(Func[i].x)*(Func[i].x); Sy:=Sy+Func[i].y; Sxy:=Sxy+(Func[i].x)*(Func[i].y); Sx3:=Sx3+(Func[i].x)*(Func[i].x)*(Func[i].x); Sx4:=Sx4+(Func[i].x)*(Func[i].x)*(Func[i].x)*(Func[i].x); Sx2y:=Sx2y+(Func[i].x)*(Func[i].x)*(Func[i].y); end; delta:=N*(Sx2*Sx4-Sx3*Sx3)-Sx*(Sx*Sx4-Sx2*Sx3)+Sx2*(Sx*Sx3-Sx2*Sx2); delta1:=Sy*(Sx2*Sx4-Sx3*Sx3)-Sx*(Sxy*Sx4-Sx3*Sx2y)+Sx2*(Sxy*Sx3-Sx2*Sx2y); delta2:=N*(Sxy*Sx4-Sx3*Sx2y)-Sy*(Sx*Sx4-Sx3*Sx2)+Sx2*(Sx*Sx2y-Sxy*Sx2); delta3:=N*(Sx2*Sx2y-Sx3*Sxy)-Sx*(Sx*Sx2y-Sxy*Sx2)+Sy*(Sx*Sx3-Sx2*Sx2); a0:=delta1/delta; a1:=delta2/delta; a2:=delta3/delta; t:=0; while (t<=xmax) do begin PaintBox1.Canvas.Pixels[Xs(t), Ys(a0+a1*t+a2*t*t)]:= clAqua; t:=t+0.01; end; k:=0; for i:=1 to 10 do begin k:=k+(Func[i].y-(a0+a1*Func[i].x+a2*Func[i].x*Func[i].x))*(Func[i].y-(a0+a1*Func[i].x+a2*Func[i].x*Func[i].x)); end; Label2.Caption:=FloatToStr(k); end; procedure TForm1.Button4Click(Sender: TObject); var i: integer; delta:real; delta1:real; delta2:real; a:real; lag_a:real; b:real; t:real; Slnx:real; Slny:real; Slnx2:real; Slnxlny:real; begin Slnx:=0; Slny:=0; Slnx2:=0; Slnxlny:=0; for i:=1 to 10 do begin Slnx:=Slnx+ln(Func[i].x); Slny:=Slny+ln(Func[i].y); Slnx2:=Slnx2+(ln(Func[i].x)*ln(Func[i].x)); Slnxlny:=Slnxlny+(ln(Func[i].x))*(ln(Func[i].y)); end; delta:=N*Slnx2-Slnx*Slnx; delta1:=Slny*Slnx2-Slnxlny*Slnx; delta2:=N*Slnxlny-Slnx*Slny; lag_a:=delta1/delta; b:=delta2/delta; a:=Exp(lag_a); t:=0.000000001; while (t<=xmax) do begin PaintBox1.Canvas.Pixels[Xs(t), Ys(a*(Power(t,b)))]:= clYellow; t:=t+0.01; end; j:=0; for i:=1 to 10 do begin j:=j+(Func[i].y-a*(Power(Func[i].x,b)))*(Func[i].y-a*(Power(Func[i].x,b))); end; Label3.Caption:=FloatToStr(j); end; procedure TForm1.Button5Click(Sender: TObject); begin if abs(l)<abs(k) then if abs(l)<abs(j)then Label5.Caption:='THE BEST' else Label7.Caption:='THE BEST' else if abs(k)<abs(j) then Label6.Caption:='THE BEST' else Label7.Caption:='THE BEST'; end; end.