# MATLAB 特征值和特征向量

Ammar Ali 2021年11月29日

## 在 MATLAB 中使用 `eig()` 函数查找特征值和特征向量

Matlab 提供了一个内置函数 `eig()` 来查找给定矩阵的特征值和特征向量。矩阵应该是方阵。否则，Matlab 会显示错误；语法：`E = eig(mat)` 返回一个列向量，其中包含矩阵 mat 的特征值。例如，让我们创建一个随机矩阵并使用 `eig()` 函数找到它的特征值。请参阅下面的代码。

``````mat = [1 3; 4 2]
E = eig(mat)
``````

``````mat =

1     3
4     2

E =

-2
5
``````

``````mat = [1 3; 4 2]
[EV,DV] = eig(mat)
``````

``````mat =

1     3
4     2

EV =

-0.7071   -0.6000
0.7071   -0.8000

DV =

-2     0
0     5
``````

``````mat = magic(3)
[EV,DV] = eig(mat)
[SV, indi] = sort(diag(DV))
``````

``````mat =

8     1     6
3     5     7
4     9     2

EV =

-0.5774   -0.8131   -0.3416
-0.5774    0.4714   -0.4714
-0.5774    0.3416    0.8131

DV =

15.0000         0         0
0    4.8990         0
0         0   -4.8990

SV =

-4.8990
4.8990
15.0000

indi =

3
2
1
``````

``````mat = magic(3);
[EV,DV] = eig(mat);
[SV, indi] = sort(diag(DV));
DVs = DV(indi,indi)
SVs = EV(:,indi)
``````

``````DVs =

-4.8990         0         0
0    4.8990         0
0         0   15.0000

SVs =

-0.3416   -0.8131   -0.5774
-0.4714    0.4714   -0.5774
0.8131    0.3416   -0.5774
``````

``````mat = magic(3)
[EV,DV,WV] = eig(mat)
``````

``````EV =

-0.5774   -0.8131   -0.3416
-0.5774    0.4714   -0.4714
-0.5774    0.3416    0.8131

DV =

15.0000         0         0
0    4.8990         0
0         0   -4.8990

WV =

-0.5774   -0.7416   -0.0749
-0.5774    0.6667   -0.6667
-0.5774    0.0749    0.7416
``````

``````mat1 = magic(2)
mat2 = [1 2; 2 1]
[EV,DV] = eig(mat1,mat2)
``````

``````mat1 =

1     3
4     2

mat2 =

1     2
2     1

EV =

1.0000   -0.5000
-1.0000    1.0000

DV =

2.0000         0
0    1.6667
``````

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.