在坛子里发个网址真费劲,感谢原帖的几位大侠。
http://www.ilovematlab.cn/viewthread.php?tid=2538~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
polyval 只是计算一个多项式,这个你应该明白吧,help里的例子很清楚:
就是把x值带进去。
polyvalm比较特别的。但是明白这个之前,你需要明白什么是 characteristic polynomial:
In linear algebra, one associates a polynomial to every square matrix, its characteristic polynomial. This polynomial encodes several important properties of the matrix, most notably its eigenvalues, its determinant and its trace.
Suppose we want to compute the characteristic polynomial of the matrix
We have to compute the determinant of
and this determinant is
The latter is the characteristic polynomial of A.
现在回到我们自己的问题, 对于Y = polyvalm(p,X), 他的计算方法是:
Y = P(1)*X^N + P(2)*X^(N-1) + ... + P(N)*X + P(N+1)*I
如果写出for循环是:
for i = 1:np %多项式长度,m是X的维数
Y = X * Y + diag(p(i) * ones(m,1));
end
这个循环比较简单,不要我解释吧?
举个例子给你看,我把循环拆开:复制内容到剪贴板代码:
p=[1 2 3]
X=[1 2; 3 4]
np = length(p);
[m,n] = size(X);
Y = zeros(m,m)
i = 1
Y = X * Y + diag(p(i) * ones(m,1))
i = 2
Y = X * Y + diag(p(i) * ones(m,1))
i = 3
Y = X * Y + diag(p(i) * ones(m,1))
你再运行:
p=[1 2 3]
X=[1 2; 3 4]
polyvalm(p,X) (By math)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
总结一下:
polyval(p, t)是计算出来:
p(1)*t^n + p(2)*t^(n-1) + .... + p(n)
polyvalm(p, X)是计算出来:
p(1)*X^n + p(2)*X^(n-1) + .... + p(n)*I
这里要注意最后的I就是单位阵,不要忘了,否则会出错,比如:
>> p=[1 2 3];
>> X=[1 2; 3 4];
>> polyvalm(p, X)
ans =
12 14
21 33
>> p(1)*X*X+p(2)*X+p(3)
ans =
12 17
24 33
>> p(1)*X*X+p(2)*X+p(3)*eye(2, 2)
ans =
12 14
21 33
(By )