赞
踩
功能:采用最小二乘法求解方程组K[m][n]*X[1][n]=B[m][1]中的X[1][n]
格式:Matrix_Solve(&K[0][0],&B[0][0],m,n,&x[0][0])
引用:
Matrix_T:求矩阵转置
Matrix_Mul:矩阵乘法
Matrix_Inv:矩阵求逆
源代码:
bool Matrix_Solve(double *K,double *B,int m,int n,double *x)//Kx=B求解x。K[m][n]。其结果返回最小二乘解,B[m][1]
{
double *KT,*Kmul,*Kb,*Kinv;
int i;
i=n*n;
KT=new double[m*n];
Kmul=new double[i];
Kinv=new double[i];
Kb=new double[n];
Matrix_T(K,m,n,KT);
Matrix_Mul(KT,n,K,n,m,Kmul);
Matrix_Mul(KT,n,B,1,m,Kb);
if (Matrix_Inv(Kmul,n,Kinv))
{
Matrix_Mul(Kinv,n,Kb,1,n,x);
delete []KT;
delete []Kmul;
delete []Kinv;
delete []Kb;
return true;
}
else
{
delete []KT;
delete []Kmul;
delete []Kinv;
delete []Kb;
return false;
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。