教程 > > 阅读:58

matlab 多项式——迹忆客-ag捕鱼王app官网

matlab将多项式表示为包含按降幂排序的系数的行向量。例如,方程p(x) = x4 7x3 - 5x 9可以表示为

p = [1 7 0 -5 9];

求解多项式

polyval 函数用于在指定的值处求解多项式。例如,要在x = 4处求解前面的多项式p,可以输入

p = [1 7 0 -5 9];
polyval(p,4)

matlab执行上述语句并返回以下结果

ans = 693

matlab还提供了 polyvalm 函数,用于求解矩阵多项式。矩阵多项式是具有矩阵变量的多项式。

例如,让我们创建一个方阵x,并在x处求解多项式p -

p = [1 7 0 -5 9];
x = [1 2 -3 4; 2 -5 6 3; 3 1 0 2; 5 -7 3 8];
polyvalm(p, x)

matlab执行上述语句并返回以下结果

ans =
      2307       -1769        -939        4499
      2314       -2376        -249        4695
      2256       -1892        -549        4310
      4570       -4532       -1062        9269

求解多项式的根

roots 函数用于计算多项式的根。例如,要计算多项式p的根,输入 -

p = [1 7 0 -5 9];
r = roots(p)

matlab执行上述语句并返回以下结果

r =
   -6.8661   0.0000i
   -1.4247   0.0000i
   0.6454   0.7095i
   0.6454 - 0.7095i

函数 polyroots 函数的反函数,并返回多项式系数。例如

p2 = poly(r)

matlab执行上述语句并返回以下结果

p2 =
   columns 1 through 3:
      1.00000   0.00000i   7.00000   0.00000i   0.00000   0.00000i
   columns 4 and 5:
      -5.00000 - 0.00000i   9.00000   0.00000i

多项式曲线拟合

polyfit 函数以最小二乘意义找到拟合一组数据的多项式系数。如果x和y是包含要拟合到n次多项式的x和y数据的两个向量,则可以编写以下代码来获得拟合数据的多项式

p = polyfit(x,y,n)

示例

创建一个脚本文件,输入以下代码

x = [1 2 3 4 5 6]; y = [5.5 43.1 128 290.7 498.4 978.67];   �ta
p = polyfit(x,y,4)   %get the polynomial
% compute the values of the polyfit estimate over a finer range, 
% and plot the estimate over the real data values for comparison:
x2 = 1:.1:6;          
y2 = polyval(p,x2);
plot(x,y,'o',x2,y2)
grid on

当我们运行该文件时,matlab 显示以下结果

p =
    4.1056 -47.9607 222.2598 -362.7453 191.1250

并绘制下图

matlab 多项式曲线拟合

查看笔记

扫码一下
查看教程更方便
网站地图