% matlab语言把多项式表达成一个行向量,该向量中的元素是按多项式 %降幂排列的。
% f(x)=an^n+an-1^n-1+……+a0
% 可用行向量 p=[an an-1 …… a1 a0]表示
clear all ;
clc;
close all;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1. poly —— 产生特征多项式系数向量
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 特征多项式一定是n+1维的
% 特征多项式第一个元素一定是1
a=[1 2 3;4 5 6;7 8 0];
p=poly(a)
% p =1.00 -6.00 -72.00 -27.00
% p是多项式p(x)=x^3-6x^2-72x-27的matlab描述方法,我们可用:
p1=poly2str(p,'x') %— 函数文件,显示
% 数学多项式的形式
% p1 =x^3 - 6 x^2 - 72 x - 27
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 2.roots —— 求多项式的根
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
r=roots(p)
% r = 12.12
% -5.73 ——显然 r是矩阵a的特征值
% -0.39
% 当然我们可用poly令其返回多项式形式
p2 = poly(r)
% p2 =
% 1.00 -6.00 -72.00 -27.00
% matlab规定多项式系数向量用行向量表示,一组根用列向量表示
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 3.conv,convs多项式乘运算
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 例:a(x)=x^2+2x+3; b(x)=4x^2+5x+6;
% c = (x^2+2x+3)(4x^2+5x+6)
a=[1 2 3];b=[4 5 6];
c=conv(a,b) %=conv([1 2 3],[4 5 6])
% c = 4.00 13.00 28.00 27.00 18.00
p=poly2str(c,'x')
% p = 4 x^4 + 13 x^3 + 28 x^2 + 27 x + 18
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 4.deconv多项式除运算
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
a=[1 2 3];
c = [4.00 13.00 28.00 27.00 18.00]
d=deconv(c,a)
% d =4.00 5.00 6.00
% [d,r]=deconv(c,a)
% r为余数
% d为c除a后的整数
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 5.多项式微分
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% matlab提供了polyder函数多项式的微分。
% 命令格式:
% polyder(p): 求p的微分
% polyder(a,b): 求多项式a,b乘积的微分
% [p,q]=polyder(a,b): 求多项式a,b商的微分
% 例:
a=[1 2 3 4 5];
poly2str(a,'x')
% ans = x^4 + 2 x^3 + 3 x^2 + 4 x + 5
b=polyder(a)
% b = 4 6 6 4
poly2str(b,'x')
% ans =4 x^3 + 6 x^2 + 6 x + 4
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%6.多项式拟合
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% P = POLYFIT(X,Y,N) finds the coefficients of a polynomial P(X) of
% degree N that fits the data Y best in a least-squares sense. P is a
% row vector of length N+1 containing the polynomial coefficients in
% descending powers, P(1)*X^N + P(2)*X^(N-1) +...+ P(N)*X + P(N+1).
x0=0:0.1:1;
y0=[-.447 1.978 3.11 5.25 5.02 4.66 4.01 4.58 3.45 5.35 9.22];
p=polyfit(x0,y0,3)
% p = 56.6915 -87.1174 40.0070 -0.9043
% Y = POLYVAL(P,X) returns the value of a polynomial P evaluated at X. P
% is a vector of length N+1 whose elements are the coefficients of the
% polynomial in descending powers.
% Y = P(1)*X^N + P(2)*X^(N-1) + ... + P(N)*X + P(N+1)
xx=0:0.01:1;yy=polyval(p,xx);
plot(xx,yy,'-b',x0,y0,'or')