Стася
Постоялец
Offline
|
|
« : 23-03-2009 18:37 » |
|
Точки на графике строятся, но они слишком далеко от основной линии. А еще не строятся графики для разных степеней полинома. unit Unit11;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Grids, matrfunctions, Math, TeEngine, Series, ExtCtrls, TeeProcs, Chart, BubbleCh;
type TForm11 = class(TForm) Edit1: TEdit; Label1: TLabel; StringGrid1: TStringGrid; Button1: TButton; StringGrid2: TStringGrid; Button2: TButton; Edit2: TEdit; Button3: TButton; Label2: TLabel; Button4: TButton; Chart1: TChart; Series1: TLineSeries; Edit3: TEdit; Label3: TLabel; Edit4: TEdit; Label4: TLabel; Button5: TButton; Series2: TLineSeries; procedure Button5Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private { Private declarations } public { Public declarations } end;
var Form11: TForm11; xydata:array of array of real; m,n:word; MatX:matrix; VecA:matrix; VecY:matrix;
implementation
{$R *.dfm}
procedure GetArray; Var i:word; n:integer; begin n:=strtoint(form11.Edit1.text); Setlength(xydata,n,2); for i:=1 to n do begin xydata[i-1,0]:=strtofloat(form11.Stringgrid1.Cells[0,i]); xydata[i-1,1]:=strtofloat(form11.Stringgrid1.cells[1,i]); end; end;
procedure TForm11.Button1Click(Sender: TObject); Var n:integer; begin n:=strtoint(Edit1.Text); Stringgrid1.ColCount:=2; Stringgrid1.RowCount:=n+1; Stringgrid1.FixedCols:=0; Stringgrid1.Cells[0,0]:='x'; Stringgrid1.Cells[1,0]:='y'; end;
procedure TForm11.FormClose(Sender: TObject; var Action: TCloseAction); Var tf:textfile; n:integer; i:word; begin n:=strtoint(edit1.Text); Assignfile(tf,'data.txt'); Rewrite(tf); Writeln(tf,n); for i:=1 to n do begin Writeln(tf,Stringgrid1.Cells[0,i]); Writeln(tf,Stringgrid1.Cells[1,i]); end; Closefile(tf); end;
procedure TForm11.FormCreate(Sender: TObject); Var tf:textfile; i:word; s:string; n:integer; begin Assignfile(tf,'data.txt'); Reset(tf); Readln(tf,n); Edit1.Text:=inttostr(n); Stringgrid1.ColCount:=n+1; Stringgrid1.Rowcount:=n+2; For i:=1 to n do begin Readln(tf,s); Stringgrid1.Cells[0,i]:=s; Readln(tf,s); Stringgrid1.Cells[1,i]:=s; end; closefile(tf); end;
procedure TForm11.Button2Click(Sender: TObject); Var i,j,k:word; begin GetArray; m:=strtoint(Edit2.Text); n:=strtoint(Edit1.Text); Setlength(MatX,n,m+1); for i := 0 to n- 1 do begin MatX[i,0]:=1; for j := 1 to m do MatX[i,j]:=power(xydata[i,0],j); end; Setlength(VecA,m+1,1); Setlength(VecY,n,1); for i := 0 to n-1 do VecY[i,0]:=xydata[i,1]; VecA:=MMultaplication(MMultaplication(MInversion(MMultaplication(MTranspose(MatX),MatX)),MTranspose(MatX)),VecY); for k := 0 to m do begin Stringgrid2.Cells[k,1]:=floattostr(VecA[k,0]); end; end;
procedure TForm11.Button3Click(Sender: TObject); Var i,j:word; begin m:=strtoint(Edit2.Text); for i := 0 to m do Stringgrid2.Cells[i,0]:='a'+inttostr(i); Stringgrid2.FixedCols:=0; Stringgrid2.RowCount:=2; end;
function GetYkr(x:real):real; Var i:byte; s:real; begin s:=VecA[0,0]; for i := 0 to m do s:=s+(VecA[i,0]*power(x,i)); result:=s; end;
function GetR:real; Var i:word; sum:real; begin for i := 1 to n-1 do sum:=sum+sqr(GetYkr(xydata[i,0])-xydata[i,1]); result:=sum; end;
procedure TForm11.Button4Click(Sender: TObject); Var i:word; a,b,k:real; begin Chart1.SeriesList[0].Clear; a:=strtofloat(Edit3.Text); b:=strtofloat(Edit4.Text); k:=(b-a)/1000; repeat Chart1.Series[0].AddXY(a,GetYkr(a),'',clblue); a:=a+k; until a>b; for i := 0 to n-1 do Chart1.Series[1].AddXY(xydata[i,0],xydata[i,1],'',clgreen); end;
procedure TForm11.Button5Click(Sender: TObject); Var i,j:byte; a,b,k:real; begin for i := 1 to n do Chart1.Series[0].Clear; m:=strtoint(Edit2.Text); for i := 0 to m do begin Edit2.Text:=InttoStr(i); Button2Click(Sender); Button4Click(Sender); end; end;
end. И еще код отдельно созданной библиотеки для этой проги unit matrfunctions;
interface
type matrix=array of array of real; vector=array of real; function MMultaplication(m1,m2:matrix):matrix; function MTranspose(m:matrix):matrix; function MInversion(m:matrix):matrix;
implementation
function MMultaplication; var i,j,k:word; nr1,nc1,nr2,nc2:word; s:real; begin nc1:=length(m1[0]); nr1:=length(m1); nc2:=length(m2[0]); nr2:=length(m2); if nc1=nr2 then begin setlength(result,nr1,nc2); for i:=0 to nr1-1 do begin for j:=0 to nc2-1 do begin s:=0; for k:=0 to nc1-1 do s:=s+m1[i,k]*m2[k,j]; result[i,j]:=s; end; end; end else begin setlength(result,1,1); result[0,0]:=0; end; end;
function MTranspose; var i,j:word; nr,nc:word; begin nc:=length(m[0]); nr:=length(m); setlength(result,nc,nr); for i:=0 to nr-1 do for j:=0 to nc-1 do result[j,i]:=m[i,j]; end;
function MInversion; Var i,j,k:word; nr,nc:word; MW:matrix; de,ed:real; Begin nc:=Length(M[0]); nr:=Length(M); SetLength(MW,nr,nc+nc); if nr<>nc then begin SetLength(Result,1,1); Result[0,0]:=0; exit; end; SetLength(Result,nr,nc); for i:=0 to nr-1 do begin for j:=0 to nc-1 do MW[i,j]:=M[i,j]; for j:=nc to nc+nc-1 do if i=j-nc then MW[i,j]:=1 else MW[i,j]:=0; end; for i:=0 to nr-1 do begin de:=MW[i,i]; if de=0 then begin SetLength(Result,1,1); Result[0,0]:=0; exit; end; for j:=0 to nc+nc-1 do MW[i,j]:=MW[i,j]/de; for k:=0 to nr-1 do begin ed:= MW[k,i]; if k<>i then for j:=0 to nc+nc-1 do MW[k,j]:=MW[k,j]-MW[i,j]*ed; end; end; for i:=0 to nr-1 do for j:=0 to nc-1 do Result[i,j]:=MW[i,nc+j]; end;
end.
|
|
|
Записан
|
|
|
|