当前位置:文档之家› 第四章 MATLAB的数值计算功能

第四章 MATLAB的数值计算功能

第四章 MATLAB的数值计算功能
第四章 MATLAB的数值计算功能

第四章MATLAB 的数值计算功能Chapter 4: Numerical computation of MATLAB

一、多项式(Polynomial)`

1.多项式的表达与创建(Expression and Creating of polynomial) (1) 多项式的表达(expression of polynomial)_

Matlab用行矢量表达多项式系数(Coefficient),各元素按变量的降

幂顺序排列,如多项式为:

P(x)=a0x n+a1x n-1+a2x n-2…a n-1x+a n

则其系数矢量(Vector of coefficient)为:P=[a0 a1… a n-1 a n]

如将根矢量(Vector of root)表示为:

ar=[ ar1 ar2… ar n]

则根矢量与系数矢量之间关系为:

(x-ar1)(x- ar2) … (x- ar n)= a0x n+a1x n-1+a2x n-2…a n-1x+a n

(2)多项式的创建(polynomial creating)

a)系数矢量的直接输入法

利用poly2sym函数直接输入多项式的系数矢量,就可方便的建立符号形式的多项式。

例:创建多项式x3-4x2+3x+2

poly2sym([1 -4 3 2])

ans =

x^3-4*x^2+3*x+2

POLY Convert roots to polynomial.

POLY(A), when A is an N by N matrix, is a row vector with

N+1 elements which are the coefficients of the characteristic

polynomial, DET(lambda*EYE(SIZE(A)) - A) .

POLY(V), when V is a vector, is a vector whose elements are

the coefficients of the polynomial whose roots are the

elements of V . For vectors, ROOTS and POLY are inverse

functions of each other, up to ordering, scaling, and

roundoff error.

b) 由根矢量创建多项式

通过调用函数p=poly(ar)产生多项式的系数矢量, 再利用poly2sym函数就可方便的建立符号形式的多项式。

注:(1)根矢量元素为n ,则多项式系数矢量元素为n+1;

(2)函数poly2sym(pa) 把多项式系数矢量表达成符号形式的多项式,缺省情况下自变量符号为x,可以指定自变量。

(3)使用简单绘图函数ezplot可以直接绘制符号形式多项式的曲线。

例1:由根矢量创建多项式。将多项式(x-6)(x-3)(x-8)表示为系数形式a=[6 3 8] %根矢量

pa=poly(a) %求系数矢量

ppa=poly2sym(pa) %以符号形式表示原多项式

ezplot(ppa,[-50,50])

pa =

1 -17 90 -144

ppa =

x^3-17*x^2+90*x-144

注:含复数根的根矢量所创建的多项式要注意:

(1)要形成实系数多项式,根矢量中的复数根必须共轭成对;

(2)含复数根的根矢量所创建的多项式系数矢量中,可能带有很小的虚部,此时可采用取实部的命令(real)把虚部滤掉。

进行多项式的求根运算时,有两种方法,一是直接调用求根函数roots,poly和roots互为逆函数。另一种是先把多项式转化为伴随矩阵,然后再求其特征值,该特征值即是多项式的根。

例3:由给定复数根矢量求多项式系数矢量。

r=[-0.5 -0.3+0.4i -0.3-0.4i];

p=poly(r)

pr=real(p)

ppr=poly2sym(pr)

p =

1.0000 1.1000 0.5500 0.1250

pr =

1.0000 1.1000 0.5500 0.1250

ppr =

x^3+11/10*x^2+11/20*x+1/8

c) 特征多项式输入法

用poly函数可实现由矩阵的特征多项式系数创建多项式。

条件:特征多项式系数矢量的第一个元素必须为一。

例2:求三阶方阵A的特征多项式系数,并转换为多项式形式。

a=[6 3 8;7 5 6; 1 3 5]

Pa=poly(a) %求矩阵的特征多项式系数矢量

Ppa=poly2sym(pa)

Pa =

1.0000 -16.0000 38.0000 -83.0000

Ppa =

x^3-17*x^2+90*x-144

注:n 阶方阵的特征多项式系数矢量一定是n +1阶的。

注:(1)要形成实系数多项式,根矢量中的复数根必须共轭成对;

(2)含复数根的根矢量所创建的多项式系数矢量中,可能带有很小的虚部,此时可采用取实部的命令(real)把虚部滤掉。

进行多项式的求根运算时,有两种方法,一是直接调用求根函数roots,poly和roots互为逆函数。另一种是先把多项式转化为伴随矩阵,然后再求其特征值,该特征值即是多项式的根。

例4:将多项式的系数表示形式转换为根表现形式。

求x3-6x2-72x-27的根

a=[1 -6 -72 -27]

r=roots(a)

r =

12.1229

-5.7345

-0.3884

MATLAB约定,多项式系数矢量用行矢量表示,根矢量用列矢量表示。>>

1. 多项式的乘除运算(Multiplication and division of polynomial)

多项式乘法用函数conv(a,b)实现,除法用函数deconv(a,b)实现。

例1:a(s)=s2+2s+3, b(s)=4s2+5s+6,计算a(s)与b(s)的乘积。

a=[1 2 3]; b=[4 5 6];

c=conv(a,b)

cs=poly2sym(c,’s’)

c =

4 13 28 27 18

cs =

4*s^4+13*s^3+28*s^2+27*s+18

例2:展开(s2+2s+2)(s+4)(s+1) (多个多项式相乘)

c=conv([1,2,2],conv([1,4],[1,1]))

cs=poly2sym(c,’s’)%(指定变量为s)

c =

1 7 16 18 8

cs =

s^4+7*s^3+16*s^2+18*s+8

例2:求多项式s^4+7*s^3+16*s^2+18*s+8分别被(s+4),(s+3)除后的结果。

c=[1 7 16 18 8];

[q1,r1]=deconv(c,[1,4]) %q—商矢量,r—余数矢量

[q2,r2]=deconv(c,[1,3])

cc=conv(q2,[1,3]) %对除(s+3)结果检验

test=((c-r2)==cc)

q1 =

1 3 4 2

r1 =

0 0 0 0 0

q2 =

1 4 4 6

r2 =

0 0 0 0 -10

cc =

1 7 16 18 18

test =

1 1 1 1 1

1. 其他常用的多项式运算命令(Other computation command of polynomial)

pa=polyval(p,s) 按数组运算规则计算给定s 时多项式p 的值。 pm=polyvalm(p,s) 按矩阵运算规则计算给定s 时多项式p 的值。

[r,p,k]=residue(b,a) 部分分式展开,b,a 分别是分子分母多项式系数

矢量,r,p,k 分别是留数、极点和直项矢量

p=polyfit(x,y,n) 用n 阶多项式拟合x ,y 矢量给定的数据。

polyder(p) 多项式微分。

注: 对于多项式b(s)与不重根的n 阶多项式a(s)之比,其部分分式展开为:)()()(2211s k p s r L p s r p s r s a s b n n +-++-+-=

式中:p 1,p 2,…,p n 称为极点(poles),r 1,r 2,…,r n 称为留数(residues),k(s)称为直项(direct terms),假如a(s)含有m 重根p j ,则相应部分应写成:m j m j j j j j

p s r L p s r p s r )()(121-++-+--++

RESIDUE Partial-fraction expansion (residues).

[R,P,K] = RESIDUE(B,A) finds the residues, poles and direct term of a partial fraction expansion of the ratio of two polynomials B(s)/A(s). If there are no multiple

roots,

B(s) R(1) R(2) R(n)

---- = -------- + -------- + ... + -------- + K(s)

A(s) s - P(1) s - P(2) s - P(n)

Vectors B and A specify the coefficients of the numerator and denominator

polynomials in descending powers of s. The residues

are returned in the column vector R, the pole locations in column vector P, and the direct terms in row vector K. The number of poles is n = length(A)-1 = length(R) = length(P). The direct term coefficient vector is empty if length(B) < length(A),

otherwise

length(K) = length(B)-length(A)+1.

If P(j) = ... = P(j+m-1) is a pole of multplicity m, then the expansion includes terms of the form

R(j) R(j+1) R(j+m-1)

-------- + ------------ + ... + ------------

s - P(j) (s - P(j))^2 (s - P(j))^m

[B,A] = RESIDUE(R,P,K), with 3 input arguments and 2 output arguments, converts the partial fraction expansion back to the polynomials with coefficients in B and A.

例3:对(3x4+2x3+5x2+4x+6)/(x5+3x4+4x3+2x2+7x+2) 做部分分式展开

a=[1 3 4 2 7 2];

b=[3 2 5 4 6];

[r,s,k]=residue(b,a)

r =

1.1274 + 1.1513i

1.1274 - 1.1513i

-0.0232 - 0.0722i

-0.0232 + 0.0722i

0.7916

s =

-1.7680 + 1.2673i

-1.7680 - 1.2673i

0.4176 + 1.1130i

0.4176 - 1.1130i

-0.2991

k =

[] (分母阶数高于分子阶数时,k将是空矩阵,表示无此项)

例5:对一组实验数据进行多项式最小二乘拟合(least square fit)

x=[1 2 3 4 5]; % 实验数据

y=[5.5 43.1 128 290.7 498.4];

p=polyfit(x,y,3) %做三阶多项式拟合

x2=1:.1:5;

y2=polyval(p,x2); % 根据给定值计算多项式结果

plot(x,y,’o’,x2,y2)

一.线性代数(Linear Algebra)

解线性方程(Linear equation)就是找出是否存在一个唯一的矩阵x,使得a,b满足关系:

ax=b 或xa=b

MALAB中x=a\b 是方程ax=b 的解,x=b/a是方程式xa=b的解。

通常线性方程多写成ax=b,“\”较多用,两者的关系为:

(b/a)’=(a’\b’)

系数矩阵a可能是m行n列的,有三种情况:

*方阵系统: (Square matrix)m=n 可求出精确解(a必须是非奇异

(nonsingular),即满秩(full rank))

*超定系统:(Overdetermind system)m>n 可求出最小二乘解

*欠定系统:(Underdetermind system) m

MATLAB对不同形式的参数矩阵,采用不同的运算法则来处理,它会自动检测参数矩阵,以区别下面几种形式:

*三角矩阵(Triangular Matrix)

*对称正定矩阵(symmetrical positive determined matrix)

*非奇异方阵(Nonsingular matrix)

*超定系统(Overdetermind system)

*欠定系统(Underdetermind system)

1.方阵系统:(Square array)

最常见的是系数矩阵为方阵a,常数项b为列矢量, 其解x可写成x=a\b, x和b大小相同。

例1:求方阵系统的根。

a=[11 6 7; 5 13 9; 17 1 8]

b=[16 13 4]’

x=a\b

a =

11 6 7

5 13 9

17 1 8

b =

16

13

4

x =

3.9763

5.4455

-8.6303

例2:假如a,b为两个大小相同的矩阵,求方阵系统的根。

a=[4 5 9; 18 19 5; 1 4 13]

b=[1 5 12; 3 15 19; 7 6 10]

x=a\b

C=a*x

a =

4 5 9

18 19 5

1 4 13

b =

1 5 12

3 15 19

7 6 10

x =

-3.6750 -0.7333 2.9708

3.7250 1.4667 -2.1292

-0.3250 0.0667 1.1958

C =

1.0000 5.0000 1

2.0000

3.0000 15.0000 19.0000

7.0000 6.0000 10.0000

若方阵a的各个行矢量线性相关(linear correlation),则称方阵a为奇异矩阵。这时线性方程将有无穷多组解。若方阵是奇异矩阵,则反斜线运算因子将发出警告信息。

2.超定系统(Overdetermind system)

实验数据较多,寻求他们的曲线拟合。

如在t内测得一组数据y:

t y

0.0 0.82

0.3 0.72

0.8 0.63

1.10.60

1.6 0.55

2.2 0.50

这些数据显然有衰减指数趋势:y(t)~c1+c2e-t

此方程意为y矢量可以由两个矢量逐步逼近而得,一个是单行的常数矢量,一个是由指数e-t项构成,两个参数c1和c2可用最小二乘法求得,它们表示实验数据与方程y(t)~c1+c2e-t之间距离的最小平方和。

例1:求上述数据的最小二乘解。将数据带入方程式y(t)~c1+c2e-t中,可得到含有两个未知数的6个等式,可写成6行2 列的矩阵e.

t=[0 0.3 0.8 1.1 1.6 2.2]’;

y=[0.82 0.72 0.63 0.60 0.55 0.50]’;

e=[ones(size(t)) exp(-t)] %求6个y(t)方程的系数矩阵

c=e\y %求方程的解

e =

1.0000 1.0000

1.0000 0.7408

1.0000 0.4493

1.0000 0.3329

1.0000 0.2019

1.0000 0.1108

c =

0.4744

0.3434

带入方程得:y(t)~0.4744+0.3434e-t

用此方程可绘制曲线:

t=[0 0.3 0.8 1.1 1.6 2.2]’;

y=[0.82 0.72 0.63 0.60 0.55 0.50]’;

t1=[0:0.1:2.5]’;y1=[ones(size(t1)),exp(-t1)]*c

plot(t1,y1,’b’,t,y,’ro’)

如果一个矩阵的行矢量是线性相关的,则它的最小二乘解并不唯一,因此,a\b运算将给出警告,并产生含有最少元素的基解。

3 .欠定系统:(Underdetermind system)

欠定系统为线性相关系统,其解都不唯一,MATLAB会计算一组构成通解的基解,而方程的特解则用QR分解法决定。

两种解法:最少元素解a\b,最小范数解pinv(a)*b.

例:用两种方法求解欠定系统。

对a和矢量b分别用a\b和pinv(a)*b求解:

a=[1 1 1; 1 1 -1]

b=[10 6]’

p=a\b

q=pinv(a)*b

a =

1 1 1

1 1 -1

b =

10

6

p =

8.0000

2.0000

q =

4.0000

4.0000

2.0000

三.逆矩阵及行列式(Revers and determinant of matrix)

1.方阵的逆和行列式(Revers and determinant of square matrix)若a是方阵,且为非奇异阵,则方程ax=I和xa=I有相同的解X。X 称为a的逆矩阵,记做a-1,在MATLAB中用inv函数来计算矩阵的逆。

计算方阵的行列式则用det函数。

DET Determinant.

DET(X) is the determinant of the square matrix X.

Use COND instead of DET to test for matrix singularity.

INV Matrix inverse.

INV(X)is the inverse of the square matrix X. A warning message is printed if X is badly scaled or nearly singular.

例:计算方阵的行列式和逆矩阵。

a=[3 -3 1;-3 5 -2;1 -2 1];

b=[14 13 5; 5 1 12;6 14 5];

d1=det(a)

x1=inv(a)

d2=det(b)

x2=inv(b)

d1 =

1

x1 =

1.0000 1.0000 1.0000

1.0000

2.0000

3.0000

1.0000 3.0000 6.0000

d2 =

-1351

x2 =

0.1207 -0.0037 -0.1118

-0.0348 -0.0296 0.1058

-0.0474 0.0873 0.0377

2.广义逆矩阵(伪逆)(Generalized inverse matrix)

一般非方阵无逆矩阵和行列式,方程ax=I 和xa=I至少有一个无解,这种矩阵可以求得特殊的逆矩阵,成为广义逆矩阵(generalized inverse matrix)(或伪逆pseudoinverse)。矩阵a mn存在广义逆矩阵x nm,使得ax=I mn,

MATLAB用pinv函数来计算广义逆矩阵。

例:计算广义逆矩阵。

a=[8 14; 1 3; 9 6]

x=pinv(a)

b=x*a

c=a*x

d=c*a %d=a*x*a=a

e=x*c %e=x*a*x=x

a =

8 14

1 3

9 6

x =

-0.0661 -0.0402 0.1743

0.1045 0.0406 -0.0974

b =

1.0000 -0.0000

-0.0000 1.0000

c =

0.9334 0.2472 0.0317

0.2472 0.0817 -0.1177

0.0317 -0.1177 0.9849

d =

8.0000 14.0000

1.0000 3.0000

9.0000 6.0000

e =

-0.0661 -0.0402 0.1743

0.1045 0.0406 -0.0974

PINV Pseudoinverse.

X = PINV(A) produces a matrix X of the same dimensions as A' so that A*X*A

= A, X*A*X = X and A*X and X*A are Hermitian. The computation is based on SVD(A) and any singular values less than a tolerance are treated as zero.

The default tolerance is MAX(SIZE(A)) * NORM(A) * EPS. PINV(A,TOL) uses the tolerance TOL instead of the default.

四.矩阵分解(Matrix decomposition)

MATLAB求解线性方程的过程基于三种分解法则:

(1)Cholesky分解,针对对称正定矩阵;

(2)高斯消元法,针对一般矩阵;

(3)正交化,针对一般矩阵(行数≠列数)

这三种分解运算分别由chol, lu和qr三个函数来分解.

1.Cholesky分解(Cholesky Decomposition)

仅适用于对称和上三角矩阵

例:cholesky分解。

a=pascal(6)

b=chol(a)

a =

1 1 1 1 1 1

1 2 3 4 5 6

1 3 6 10 15 21

1 4 10 20 35 56

1 5 15 35 70 126

1 6 21 56 126 252

b =

1 1 1 1 1 1

0 1 2 3 4 5

0 0 1 3 6 10

0 0 0 1 4 10

0 0 0 0 1 5

0 0 0 0 0 1

CHOL Cholesky factorization.

CHOL(X) uses only the diagonal and upper triangle of X. The lower triangular is assumed to be the (complex conjugate) transpose of the upper. If X is positive definite, then R = CHOL(X) produces an upper triangular

R so that R'*R = X. If X is not positive definite, an error message is printed.

[R,p] = CHOL(X), with two output arguments, never produces an

error message. If X is positive definite, then p is 0 and R is the same as above. But if X is not positive definite, then p is a positive integer.

When X is full, R is an upper triangular matrix of order q = p-1

so that R'*R = X(1:q,1:q). When X is sparse, R is an upper triangular matrix of size q-by-n so that the L-shaped region of the first q rows and first q columns of R'*R agree with those of X.

2.LU分解(LU factorization).

用lu函数完成LU分解,将矩阵分解为上、下两个三角阵,其调用格式为:[l,u]=lu(a) l代表下三角阵,u代表上三角阵。

例:

LU分解。

a=[47 24 22; 11 44 0;30 38 41]

[l,u]=lu(a)

a =

47 24 22

11 44 0

30 38 41

l =

1.0000 0 0

0.2340 1.0000 0

0.6383 0.5909 1.0000

u =

47.0000 24.0000 22.0000

0 38.3830 -5.1489

0 0 30.0000

LU LU factorization.

[L,U] = LU(X) stores an upper triangular matrix in U and a "psychologically lower triangular matrix" (i.e. a product of lower triangular and permutation matrices) in L, so that X = L*U. X can be rectangular.

[L,U,P] = LU(X) returns unit lower triangular matrix L, upper triangular matrix U, and permutation matrix P so that P*X = L*U.

3.QR分解(Orthogonal-triangular decomposition).

函数调用格式:[q,r]=qr(a), q代表正规正交矩阵,r代表三角形矩阵。原始阵a不必一定是方阵。如果矩阵a是m×n阶的,则矩阵q是m×m阶的,矩阵r是m×n阶的。

例:QR分解.

A=[22 46 20 20; 30 36 46 44;39 8 45 2];

[q,r]=qr(A)

q =

-0.4082 -0.7209 -0.5601

-0.5566 -0.2898 0.7786

-0.7236 0.6296 -0.2829

r =

-53.8981 -44.6027 -66.3289 -34.1014

0 -38.5564 0.5823 -25.9097

0 0 11.8800 22.4896

QR Orthogonal-triangular decomposition.

[Q,R] = QR(A) produces an upper triangular matrix R of the same dimension as A and a unitary matrix Q so that A = Q*R.

[Q,R,E] = QR(A) produces a permutation matrix E, an upper

triangular R and a unitary Q so that A*E = Q*R. The column

permutation E is chosen so that abs(diag(R)) is decreasing.

[Q,R] = QR(A,0) produces the "economy size" decomposition. If A is m-by-n

with m > n, then only the first n columns of Q are computed.

4. 特征值与特征矢量(Eigenvalues and eigenvectors).

MATLAB中使用函数eig计算特征值和特征矢量,有两种调用方法:*e=eig(a), 其中e是包含特征值的矢量;

*[v,d]=eig(a),其中v是一个与a相同的n×n阶矩阵,它的每一列是矩阵a的一个特征值所对应的特征矢量,d为对角阵,其对角元素即为矩阵a 的特征值。

例:计算特征值和特征矢量。

a=[34 25 15; 18 35 9; 41 21 9]

e=eig(a)

[v,d]=eig(a)

a =

34 25 15

18 35 9

41 21 9

e =

68.5066

15.5122

-6.0187

v =

-0.6227 -0.4409 -0.3105

-0.4969 0.6786 -0.0717

-0.6044 -0.5875 0.9479

d =

68.5066 0 0

0 15.5122 0

0 0 -6.0187

EIG Eigenvalues and eigenvectors.

E = EIG(X) is a vector containing the eigenvalues of a square matrix X.

[V,D] = EIG(X) produces a diagonal matrix D of eigenvalues and a full matrix V whose columns are the corresponding eigenvectors so that X*V = V*D. [V,D] = EIG(X,'nobalance') performs the computation with balancing disabled, which sometimes gives more accurate results for certain

problems with unusual scaling. If X is symmetric, EIG(X,'nobalance')

is ignored since X is already balanced.

5. 奇异值分解.( Singular value decomposition).

如存在两个矢量u,v及一常数c,使得矩阵A满足:Av=cu, A’u=cv

称c为奇异值,称u,v为奇异矢量。

将奇异值写成对角方阵∑,而相对应的奇异矢量作为列矢量则可写成两个正交矩阵U,V,使得:A V=U∑,A‘U=V∑因为U,V正交,所以可得奇异值表达式:

A=U∑V’。

一个m行n列的矩阵A经奇异值分解,可求得m行m列的U, m行n 列的矩阵∑和n行n列的矩阵V.。

奇异值分解用svd函数实现,调用格式为;

[u,s,v]=svd(a)

SVD Singular value decomposition.

[U,S,V] = SVD(X) produces a diagonal matrix S, of the same dimension as X and with nonnegative diagonal elements in decreasing order, and unitary matrices U and V so that X = U*S*V'.

S = SVD(X) returns a vector containing the singular values.

[U,S,V] = SVD(X,0) produces the "economy size" decomposition. If X is

m-by-n with m > n, then only the first n columns of U are computed and S is

n-by-n.

例:奇异值分解。

a=[8 5; 7 3;4 6];

[u,s,v]=svd(a) % s为奇异值对角方阵

u =

-0.6841 -0.1826 -0.7061

-0.5407 -0.5228 0.6591

-0.4895 0.8327 0.2589

s =

13.7649 0

0 3.0865

0 0

v =

-0.8148 -0.5797

-0.5797 0.8148

五.数据分析(Data Analyaia)

MATLAB对数据分析有两条约定:

(1)若输入量X是矢量,则不论是行矢量还是列矢量,运算是对整个矢量进行的;

(2)若输入量X是数组,(或称矩阵),则命令运算是按列进行的。即默认每个列是有一个变量的不同“观察“所得的数据组成。

1. 基本统计命令(表4-1)

例:做各种基本统计运算。

A=[5 -10 -6 0;2 6 3 -3;-9 5 -10 11;-22 17 0 -19;-1 6 -4 4]

Amax=max(A) %找A各列的最大元素

Amin=min(A) %找A各列的最小元素

Amed=median(A) %找A各列的中位元素

Amean=mean(A) %找A各列的平均值

Astd=std(A) %求A各列的标准差

Aprod=prod(A) %求A各列元素的积

Asum=sum(A) %求A各列元素的和

S=cumsum(A) %求A各列元素的累积和

P=cumprod(A) %求A各列元素的累积j积

I=sort(A) %使A的各列元素按递增排列

A =

5 -10 -

6 0

2 6

3 -3

-9 5 -10 11

-22 17 0 -19

-1 6 -4 4

Amax =

5 17 3 11

Amin =

-22 -10 -10 -19

Amed =

-1 6 -4 0

Amean =

-5.0000 4.8000 -3.4000 -1.4000

Astd =

10.8397 9.6281 5.0794 11.1490

Aprod =

-1980 -30600 0 0 Asum =

-25 24 -17 -7

S =

5 -10 -

6 0

7 -4 -3 -3

-2 1 -13 8

-24 18 -13 -11

-25 24 -17 -7

P =

5 -10 -

6 0

10 -60 -18 0

-90 -300 180 0

1980 -5100 0 0

-1980 -30600 0 0

相关主题
文本预览
相关文档 最新文档