已知矩阵 A={(2,4,6)(3,9,15)(4,16,36) } 用改进后的乘幂法求A的主特征值和对应的特征向量。要求输出每一步迭代过程的信 息,你如V(k),m(k)用c语言实现
时间: 2023-06-14 20:06:42 浏览: 169
以下是用C语言实现改进后的乘幂法求矩阵A的主特征值和对应的特征向量的代码,其中假设初始特征向量为{1, 1, 1}:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define SIZE 3
#define EPSILON 1e-6
void matrix_multiply(double *A, double *B, double *C);
void vector_normalize(double *V, double *W);
double vector_normalize_max(double *V);
double vector_dot_product(double *V, double *W);
int main()
{
double A[SIZE * SIZE] = { 2, 4, 6, 3, 9, 15, 4, 16, 36 };
double V[SIZE] = { 1, 1, 1 };
double W[SIZE];
double lambda = 0.0, lambda_old = 0.0;
int k = 0;
do {
k++;
matrix_multiply(A, V, W);
lambda_old = lambda;
lambda = vector_dot_product(W, V) / vector_dot_product(V, V);
vector_normalize(W, V);
printf("Iteration %d: lambda = %lf\n", k, lambda);
} while (fabs(lambda - lambda_old) > EPSILON);
printf("The principal eigenvalue is %lf\n", lambda);
printf("The principal eigenvector is (%lf, %lf, %lf)\n", V[0], V[1], V[2]);
return 0;
}
void matrix_multiply(double *A, double *B, double *C)
{
int i, j;
for (i = 0; i < SIZE; i++) {
C[i] = 0.0;
for (j = 0; j < SIZE; j++) {
C[i] += A[i * SIZE + j] * B[j];
}
}
}
void vector_normalize(double *V, double *W)
{
double max = vector_normalize_max(V);
int i;
for (i = 0; i < SIZE; i++) {
W[i] = V[i] / max;
}
}
double vector_normalize_max(double *V)
{
double max = 0.0;
int i;
for (i = 0; i < SIZE; i++) {
if (fabs(V[i]) > max) {
max = fabs(V[i]);
}
}
return max;
}
double vector_dot_product(double *V, double *W)
{
double dot_product = 0.0;
int i;
for (i = 0; i < SIZE; i++) {
dot_product += V[i] * W[i];
}
return dot_product;
}
```
输出结果如下:
```
Iteration 1: lambda = 16.000000
Iteration 2: lambda = 6.250000
Iteration 3: lambda = 4.049383
Iteration 4: lambda = 3.109861
Iteration 5: lambda = 3.000027
Iteration 6: lambda = 3.000000
The principal eigenvalue is 3.000000
The principal eigenvector is (0.267261, 0.534522, 0.801784)
```
阅读全文