鍍金池/ 教程/ 數(shù)據(jù)分析&挖掘/ Matlab多項(xiàng)式
Matlab代數(shù)(方程求解)
Matlab開發(fā)環(huán)境設(shè)置
Matlab GNU Octave
Matlab字符串
Matlab矩陣
Matlab微分和導(dǎo)數(shù)
Matlab數(shù)字
Matlab數(shù)據(jù)導(dǎo)入
Matlab整合集成
Matlab冒號符號
Matlab變量
Matlab數(shù)組
Matlab運(yùn)算符
Matlab數(shù)據(jù)導(dǎo)出
Matlab向量
Matlab命令
Matlab決策
Matlab微積分
Matlab圖形
Matlab教程
Matlab繪圖
Matlab多項(xiàng)式
Matlab .m腳本文件
Matlab循環(huán)
Matlab基礎(chǔ)語法
Matlab函數(shù)
Matlab轉(zhuǎn)換
Matlab概述
Matlab數(shù)據(jù)類型

Matlab多項(xiàng)式

MATLAB表示多項(xiàng)式為包含由下降冪排列的系數(shù)的行向量。 例如,方程式

可以表示為 -

p = [1 7 0 -5 9];

評估計(jì)算多項(xiàng)式

多值函數(shù)用于評估計(jì)算指定值的多項(xiàng)式。 例如,要評估前面的多項(xiàng)式p,在x = 4,可使用以下代碼 -

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

MATLAB執(zhí)行上述語句返回以下結(jié)果 -

ans = 693

MATLAB還提供polyvalm函數(shù)用于評估矩陣多項(xiàng)式。 矩陣多項(xiàng)式是以矩陣為變量的多項(xiàng)式。

例如,下面創(chuàng)建一個(gè)方陣X并評估求值多項(xiàng)式p,在X -

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執(zhí)行上述代碼語句返回以下結(jié)果 -

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

計(jì)算多項(xiàng)式的根

roots函數(shù)計(jì)算多項(xiàng)式的根。 例如,要計(jì)算多項(xiàng)式p的根,可參考以下語法 -

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

MATLAB執(zhí)行上述代碼語句返回以下結(jié)果 -

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

poly函數(shù)是roots函數(shù)的逆,并返回到多項(xiàng)式系數(shù)。 例如 -

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

MATLAB執(zhí)行上述代碼語句返回以下結(jié)果 -

Trial>> p = [1 7 0  -5 9];
r = roots(p)
p2 = poly(r)

r =

  -6.8661 + 0.0000i
  -1.4247 + 0.0000i
   0.6454 + 0.7095i
   0.6454 - 0.7095i


p2 =

    1.0000    7.0000    0.0000   -5.0000    9.0000

多項(xiàng)式曲線擬合

polyfit函數(shù)用來查找一個(gè)多項(xiàng)式的系數(shù),它符合最小二乘法中的一組數(shù)據(jù)。 如果xy包含要擬合到n度多項(xiàng)式的xy數(shù)據(jù)的兩個(gè)向量,則得到通過擬合數(shù)據(jù)的多項(xiàng)式,參考代碼 -

p = polyfit(x,y,n)

示例

創(chuàng)建腳本文件并鍵入以下代碼 -

x = [1 2 3 4 5 6]; y = [5.5 43.1 128 290.7 498.4 978.67];  %data
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執(zhí)行上述代碼語句返回以下結(jié)果 -

Trial>> x = [1 2 3 4 5 6]; y = [5.5 43.1 128 290.7 498.4 978.67];  %data
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

p =

    4.1056  -47.9607  222.2598 -362.7453  191.1250

同時(shí)還輸出一個(gè)圖形 -