类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:
#include<iostream.h>
//定义一个矩阵乘法函数
float chen_fa(float a[][2],int m)
{
float (*c)[2]=new float[][2];
for (int i=0;i<2;i++)
for(int t=0;t<2;t++)
for(int j=0;j<m;j++)
c[i][t]=c[i][t]+a[i][j]*a[j][t];
return (c[2][2]);
}
//定义求逆阵的函数
float li_zhen(float c[][2])
{
float c_1[2][2];
float c1=0.0;
c1=c[0][0]*c[1][1]-c[1][0]*c[0][1];
c_1[0][0]=c[1][1]/c1;
c_1[0][1]=c[1][0]/c1;
c_1[1][0]=c[0][1]/c1;
c_1[1][1]=c[0][0]/c1;
return (c_1[2][2]);
}
//最小二乘法最后一步函数
float aty(float a[][2],int m,float y[],float c[2][2])
{
float b[2];
for (int i=0;i<2;i++)
for (int j=0;j<m;j++)
b[i]=a[j][i]*y[j]+b[i];
float x[2];
for(int w=0;w<2;w++)
for (int t=0;t<2;t++)
x[w]=c[w][t]*b[t]+x[w];
return (x[2]);
}
//主函数
main()
{
//申请动态空间,将数据输入数组中
float (*a)[2]=new float[][2];
cout<<"please cin a num"<<endl;
int m;//M表示数组元素个数
cin>>m;
for (int i=0;i<m;i++)
{
cin>>a[i][1];
a[i][0]=1;
}
cout<<"please cin Y"<<endl;
float *y=new float[m];
for (int k=0;k<m;k++)
cin>>y[i];
//输出数组的值
for(int l=0;l<m;l++)//这时对L循环,非1,不要误会
for(int p=0;p<2;p++)
cout<<a[l][p];
cout<<" "<<y[l];
cout<<endl;
float c[2][2];
(*c)[2]=chen_fa(a[][2],m);
float c_1[2][2];
(*c_1)[2]=li_zhen(c[][2]);
float last[2];
last[2]=aty(a[][2],m,y,c[2][2]);
cout<<"k="<<last[1]<<",k0="<<last[0]<<endl;
}
//出现错误就只有一个,请朋友们帮我调试一下
//最好是帮我调试一下,错误简单,主要是二维数组与函数的返回值与函数参数的问题
网友回答:
#include<iostream> /////////
using namespace std;
//定义一个矩阵乘法函数
void chen_fa(float c[][2], const float a[][2], const int m) /////////
{
//float (*c)[2]=new float[m][2]; /////////
for (int i=0;i<2;i++)
for(int t=0;t<2;t++)
for(int j=0;j<m;j++)
c[i][t]=c[i][t]+a[i][j]*a[j][t];
//return (c[2][2]); ///////
}
//定义求逆阵的函数
void li_zhen(float c_1[][2], const float c[][2]) ///////////
{
//float c_1[2][2]; ////////
float c1=0.0;
c1=c[0][0]*c[1][1]-c[1][0]*c[0][1];
c_1[0][0]=c[1][1]/c1;
c_1[0][1]=c[1][0]/c1;
c_1[1][0]=c[0][1]/c1;
c_1[1][1]=c[0][0]/c1;
//return (c_1[2][2]); ////////
}
//最小二乘法最后一步函数
void aty(float x[], const float a[][2], const int m, const float y[], const float c[][2]) /////////
{
float b[2];
for (int i=0;i<2;i++)
for (int j=0;j<m;j++)
b[i]=a[j][i]*y[j]+b[i];
//float x[2]; ///////
for(int w=0;w<2;w++)
for(int t=0;t<2;t++)
x[w]=c[w][t]*b[t]+x[w];
//return (x[2]); ////////////
}
//主函数
main()
{
cout<<"please cin a num"<<endl;
int m;//M表示数组元素个数
cin>>m;
//申请动态空间,将数据输入数组中
float (*a)[2]=new float[m][2]; ////////
for (int i=0;i<m;i++)
{
cin>>a[i][1];
a[i][0]=1;
}
cout<<"please cin Y"<<endl;
float *y=new float[m];
for (int k=0;k<m;k++)
cin>>y[k]; ///////
//输出数组的值
for(int l=0;l<m;l++)//这时对L循环,非1,不要误会
{ ///////
for(int p=0;p<2;p++)
cout<<a[l][p] << ; //////
cout<<" "<<y[l];
cout<<endl;
} //////
float c[2][2];
chen_fa(c, a, m); ///////
float c_1[2][2];
li_zhen(c_1, c); //////
float last[2];
aty(last, a, m, y, c); ///////
cout<<"k="<<last[1]<<",k0="<<last[0]<<endl;
}
//出现错误就只有一个,请朋友们帮我调试一下
//最好是帮我调试一下,错误简单,主要是二维数组与函数的返回值与函数参数的问题