matlab多项式多个值代⼊求值,[转载]Matlab 解惑之——多项式
%降幂排列的。
% f(x)=an^n+an-1^n-1+……+a0
% 可⽤⾏向量 p=[an an-1 …… a1
a0]表⽰
clear all ;
matlab求导clc;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1. poly —— 产⽣特征多项式系数向量poly(A) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 特征多项式⼀定是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) %P为多项式系剩,按降幂排列
% r = 12.12
% -5.73 ——显然
r是矩阵a的特征值
% -0.39
% 当然我们可⽤poly令其返回多项式形式
p2 = poly(r) %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乘积求导
% 例:
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')
1. 多项式的算术运算
参加加减运算的多项式应该具有相同的阶次。
多项式乘法采⽤conv函数,除法由deconv函数完成。
2. 求根
求多项式的根采⽤roots函数。
3. 求值
函数polyval可以将某个特定数值代⼊多项式
函数polyvalm可以求出当多项式中的未知数为⽅阵时的值。
4. 求导
使⽤polyder函数对多项式求导
例: 将表达式(x-4)(x+5)(x2-6x+9)展开为多项式形式,并求其对应的⼀元n次⽅程的根。>>p=conv([1 -4],conv([1 5],[1
-6 9]))
>>px=poly2str(p,’x’)
>>x=roots(p)
例: 已知向量A=[1 –34 –80 0 0],⽤此向量构造⼀多项式并显⽰结果。
(x-1)(x+34)(x+80)(x-0)(x-0)
>>PA=poly(A)
>>PAX=poly2str(PA,'X')
X^5 + 113 X^4 + 2606
X^3 - 2720 X^2
发布评论