其程序代码如下:
#include "stdafx.h"
#include
#include
#include
#define MAX 10
void Inverse(double *matrix1[],double*matrix2[],int n,double d);//矩阵求拟
double Determinant(double* matrix[],int n);//方阵,行列式
double Alco(double* matrix[],int jie,introw,int column); 代数余子式A=(-1)I+J(M)
double Cofactor(double* matrix[],int jie,introw,int column); 余子式
int _tmain(int argc, _TCHAR* argv[])
{
double array[12][3],Y[3];
double A,B,C;
A = B = C = 0.0;
ZeroMemory(array,sizeof(array));
ZeroMemory(Y,sizeof(Y));
for (int i = 0;i <12;i++)
{
for(int j = 0;j < 3;j++)
{
array[i][j] = (double)rand();
}
}
for (int i = 0; i <12;i++)
{
array[i][0] = 1.0;
}//设计了12个最简单的数据点,x = 1平面上的点,
double*Matrix[3],*IMatrix[3];
for (int i = 0;i <3;i++)
{
Matrix[i] = new double[3];
IMatrix[i] = new double[3];
}
for (int i = 0;i <3;i++)
{
for(int j = 0;j < 3;j++)
{
*(Matrix[i] + j) = 0.0;
}
}
for (int j = 0;j <3;j++)
{
for(int i = 0;i < 12;i++)
{
*(Matrix[0] + j) += array[i][0]*array[i][j];
*(Matrix[1] + j) += array[i][1]*array[i][j];
*(Matrix[2] + j) += array[i][2]*array[i][j];
Y[j] -= array[i][j];
}
}
double d =Determinant(Matrix,3);
if (abs(d) < 0.0001)
{
printf("\n矩阵奇异");
getchar();
return-1;
}
Inverse(Matrix,IMatrix,3,d);
for (int i = 0;i <3;i++)
{
A += *(IMatrix[0]+ i)*Y[i];
B +=*(IMatrix[1] + i)*Y[i];
C +=*(IMatrix[2] + i)*Y[i];
}
printf("\n A = %5.3f,B = %5.3f,C= %5.3f",A,B,C);
for (int i = 0;i <3;i++)
{
delete[] Matrix[i];
delete[] IMatrix[i];
}
getchar();
return 0;
}
void Inverse(double *matrix1[],double*matrix2[],int n,double d)
{
int i,j;
for(i=0;i matrix2[i]=(double *)malloc(n*sizeof(double)); for(i=0;i for(j=0;j *(matrix2[j]+i)=(AlCo(matrix1,n,i,j)/d); } double Determinant(double* matrix[],intn) { doubleresult=0,temp; int i; if(n==1) result=(*matrix[0]); else { for(i=0;i { temp=AlCo(matrix,n,n-1,i); result+=(*(matrix[n-1]+i))*temp; } } return result; } double AlCo(double* matrix[],int jie,introw,int column) { double result; if((row+column)%2 == 0) result= Cofactor(matrix,jie,row,column); elseresult=(-1)*Cofactor(matrix,jie,row,column); return result; } double Cofactor(double* matrix[],int jie,introw,int column) { double result; int i,j; double*smallmatr[MAX-1]; for(i=0;i smallmatr[i]= new double[jie - 1]; for(i=0;i for(j=0;j *(smallmatr[i]+j)=*(matrix[i]+j); for(i=row;i for(j=0;j *(smallmatr[i]+j)=*(matrix[i+1]+j); for(i=0;i for(j=column;j *(smallmatr[i]+j)=*(matrix[i]+j+1); for(i=row;i for(j=column;j *(smallmatr[i]+j)=*(matrix[i+1]+j+1); result =Determinant(smallmatr,jie-1); for(i=0;i delete[] smallmatr[i]; returnresult; }