var yc_huice: Tyc_huice; type PReal=array of double; function MatrixOpp(A :array of double;m, n :integer) :PReal; function MatrixInver(A:array of double; m,n :integer) :PReal; function Surplus(A :array of double; m, n :integer) :real; implementation
{$R *.dfm} uses Math;
function MatrixOpp(A :array of double;m, n :integer) :PReal; var i,j,x,y,k :integer; SP,AB, B,C :PReal; X1 :double; begin Setlength(SP, m*n*sizeof(double)); Setlength(AB, m*n*sizeof(double)); Setlength(B, m*n*sizeof(double)); X1 :=Surplus(A,m,n); X1 :=1/X1; for i :=0 to m do for j :=0 to n do begin for k:=0 to m*n do begin B[k]:=A[k]; begin for x:=0 to n do B[i*n+x] :=0; for y:=0 to m do B[m*y+j] :=0; B[i*n+j] :=1; SP[i*n+j] :=Surplus(B,m,n); AB[i*n+j] :=X1*SP[i*n+j]; end; end; end; C :=MatrixInver(AB,m,n); Result := C; Exit; end;
function MatrixInver(A:array of double; m,n :integer) :PReal; //*矩阵转置*/ var i,j :integer; B :PReal; begin Setlength(B, m*n*sizeof(double)); for i :=0 to n do for j :=0 to m do B[i*m+j] :=A[j*n+i]; Result := B; end;
function Surplus(A :array of double; m, n :integer) :real; //*求矩阵行列式*/ var i,j,k,p,r :integer; X,temp,temp1,s,s1 :double; begin temp:=1; temp1:=1; s:=0; s1:=0; if(n=2) then begin for i :=0 to m do for j :=0 to n do if((i+j) mod 2 <> 0) then temp1 :=temp1 * A[i*n+j] else temp := temp *A[i*n+j]; X :=temp-temp1; end else begin for k :=0 to n do begin j := k; for i :=0 to m do begin temp := temp * A[i*n+j]; if j < n then inc(j) end;
if (m-i) <>0 then begin r :=m-1; for p := m-i downto 0 do begin temp := temp * A[r*n+p-1]; dec(r); end; end; s :=s + temp; temp :=1; end; for k :=n-1 downto 0 do begin j := k; for i :=0 to m do begin temp1 := temp1 *A[i*n+j]; dec(j); end; if(m-i) <>0 then begin r := i; for p :=m-1 downto m do begin temp1:= temp1*A[r*n+p]; inc(r); end; end; s1 := s1 + temp1; temp1 :=1; end; X :=s-s1; end; result := X; end;
procedure Tyc_huice.SpeedButton1Click(Sender: TObject); var yuan:array[0..17] of double; x,xy,xh,ek:array[0..17] of double; xk,e,ej,xj,s1,s2,c,s11,p,m,ai:double; i,j,k,count,xx:integer; a,u:integer; a0:array[0..1] of double; B,BT,BTB,BTB1,BTB2:PReal; begin count:=17; // Setlength(yuan, 0*17*sizeof(Real)); yuan[0]:=0;yuan[1]:=0;yuan[2]:=0;yuan[3]:=0;yuan[4]:=0;yuan[5]:=0; yuan[6]:=0;yuan[7]:=0;yuan[8]:=0;yuan[9]:=0;yuan[10]:=0;yuan[11]:=0;yuan[12]:=0;yuan[13]:=0;yuan[14]:=0;yuan[15]:=0;yuan[16]:=0; yuan[17]:=0; x[0]:=0;x[1]:=0;x[2]:=0;x[3]:=0;x[4]:=0;x[5]:=0;x[6]:=0;x[7]:=0;x[8]:=0;x[9]:=0;x[10]:=0;x[11]:=0;x[12]:=0;x[13]:=0;x[14]:=0;x[15]:=0;x[16]:=0; yuan[0]:=strtofloat(edit1.Text);yuan[1]:=strtofloat(edit2.Text);yuan[0*0+2]:=strtofloat(edit3.Text);yuan[0*0+3]:=strtofloat(edit4.Text);yuan[0*0+4]:=strtofloat(edit5.Text);yuan[0*0+5]:=strtofloat(edit6.Text); yuan[6]:=strtofloat(edit7.Text);yuan[7]:=strtofloat(edit8.Text);yuan[0*0+8]:=strtofloat(edit9.Text);yuan[0*0+9]:=strtofloat(edit10.Text);yuan[0*0+10]:=strtofloat(edit11.Text);yuan[0*0+11]:=strtofloat(edit12.Text); yuan[12]:=strtofloat(edit13.Text);yuan[0*0+13]:=strtofloat(edit14.Text);yuan[0*0+14]:=strtofloat(edit15.Text);yuan[0*0+15]:=strtofloat(edit16.Text);yuan[0*0+16]:=strtofloat(edit17.Text); yuan[0*0+17]:=strtofloat(edit18.Text); //将原始数据作一次累加生成 for i:=0 to count do begin for j:=0 to i do begin x[i]:=yuan[j]+x[i]; end; end; //计算参数a,u Setlength(B, (count-1)*1*sizeof(double)); // Setlength(a0, 1*0*sizeof(double)); // Setlength(BT, 1*(count-2)*sizeof(Real)); Setlength(BTB, 1*1*sizeof(double)); Setlength(BTB2, 1*(count-1)*sizeof(double)); // Setlength(YN, (count-1)*0*sizeof(double)); for i:=0 to count-1 do begin B[i+0]:=-0.5*(x[i]+x[i+1]); B[i+1]:=1; end; // for i:=0 to count-1 do // YN[i]:=yuan[i+1]; BT:=MatrixInver(B,count-1,1); for i:=0 to 3 do //求BTb BTB[i]:=0; for i:=0 to 1 do begin for j:=0 to 1 do begin for k:=0 to count-1 do BTB[i*1+j]:=B[i+k]*BT[k*1+j]+BTB[i*1+j]; end; end; BTB1:=MatrixOpp(BTB,1,1); //求BTB的逆
for i:=0 to 1 do for j:=0 to count-1 do for k:=0 to count-1 do BTB2[i*(count-1)+j]:=BTB1[i*1+k]*BT[k*(count-1)+j]+BTB2[i*(count-1)+j];
a0[0]:=0;a0[1]:=0;
for i:=0 to 1 do begin
for k:=0 to count-1 do a0[i]:=BTB2[i*(count-1)+k]*yuan[k+1]+a0[i]; end; u:=round(a0[0*1+0]); a:=round(a0[1*1+0]); xx:=round(x[0]); label2.Caption:='['+inttostr(xx)+'-'+inttostr(u)+ '/'+inttostr(a)+']'+'e^'+inttostr(a)+'k'+'+'+ inttostr(u)+'/'+inttostr(a); e:=2.7; ej:=0; xj:=0;s1:=0;s2:=0;c:=0;p:=0;
m:=0; xy[0]:=x[0]; xh[0]:=xy[0]; if a<>0 then begin xk:=(yuan[0]-u/a)*power(e,a*count)+u/a; for i:=1 to count do begin xy[i]:=(yuan[0]-u/a)*power(e,a*i)+u/a; xh[i]:=xy[i]-xy[i-1]; ek[i]:=yuan[i]-xh[i]; ej:=mean(ek); xj:=mean(yuan); //求原始数据均值 s1:=popnstddev(yuan); //求原始数据标准差 s2:=popnstddev(ek);
c:=s2/s1; s11:=0.6745*s1; if (ek[i]-ej>s11) or (ek[i]-ej<s11) then m:=m+1; p:=m/(count+1); end; end else begin xk:=0; xy[i]:=0; end;
//计算x^(k+1),预测值的还原
//模型精度检验: 求预测误差 //求预测均值
// if p<=0.7 or c>=0.65 then//确定预测精度
end;
e
----------------------------------------------
-