- #include <iostream>
- #include <math.h>
- #include <string.h>
- using namespace std;
-
- #define N (3)
-
- /************************************************************************/
- /*
- 求向量按模最大值(相当于向量的无穷范数)
- */
- /************************************************************************/
- double GetMax(double *Array)
- {
- double max=0;
- for (int i=0;i<N;++i)
- {
- if (max < fabs(*(Array+i)))
- {
- max = fabs(*(Array+i));
- }
- }
- return max;
- }
-
- /************************************************************************/
- /*
- 矩阵相乘函数
- */
- /************************************************************************/
- void matrixMulti(double array[3][3],double *a0)
- {
- int i ,j;
- double *result = new double[3];
- memset(result,0,3*sizeof(double));
-
- for (i=0;i<N;++i)
- {
-
- for (j =0;j<N;++j)
- {
- result[i]+=array[i][j]*a0[j];
-
- }
- cout<<"a0["<<i<<"]=" <<result[i]<<endl;
- }
- for (i=0;i<N;++i)
- {
- *(a0+i)=result[i];
- }
- delete [] result;
- }
-
- int main()
- {
- double array[N][N] = {{6,-12,6},{-21,-3,24},{-12,-12,51} }; //矩阵A
- double a0[N]={1,0,0}; //初始向量
- int count=0; //迭代次数
- double maxElemt1,maxElemt2;
- do
- {
- ++count;
- maxElemt1=GetMax(a0);
-
- for (int j=0 ;j<N;++j)
- {
- *(a0+j)=*(a0+j)/maxElemt1; //列向量归一化
- }
- matrixMulti(array,a0);
- maxElemt2 = GetMax(a0);
- cout<<"maxElemt2 = "<<maxElemt2<<endl<<endl;
- }while(fabs((maxElemt2-maxElemt1)/maxElemt1 )> 0.00000001);
- cout<<"迭代次数n = "<<count<<endl;;
- cout<<"特征值="<<maxElemt2<<endl;
-
- return 0;
- }
|