MATLAB表示多項(xiàng)式為包含由下降冪排列的系數(shù)的行向量。 例如,方程式
可以表示為 -
p = [1 7 0 -5 9];
多值函數(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
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
polyfit
函數(shù)用來查找一個(gè)多項(xiàng)式的系數(shù),它符合最小二乘法中的一組數(shù)據(jù)。 如果x
和y
包含要擬合到n
度多項(xiàng)式的x
和y
數(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è)圖形 -