English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
MATLAB represents polynomials as row vectors, containing coefficients arranged in descending order of powers. For example, the equation P(x) = X 4 + 7× 3 - 5×+ 9Can be expressed as-
p = [1 7 0 -5 9]
polyvalThis function is used to evaluate a polynomial for a specific value. For example, at x = 4 When calculating the polynomial p we mentioned earlier, please enter-
1 7 0 -5 9] polyval(p,4)
MATLAB executes the above statement and returns the following result-
ans = 693
MATLAB also provides the polyvalm function for calculating matrix polynomials. A matrix polynomial is a polynomial with matrices as variables.
For example, let's create a square matrix X and calculate the polynomial p at X-
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 executes the above statement and returns the following result-
ans = 2307 -1769 -939 4499 2314 -2376 -249 4695 2256 -1892 -549 4310 4570 -4532 -1062 9269
rootsThe function calculates the roots of a polynomial. For example, to calculate the roots of the polynomial p, please enter-
1 7 0 -5 9] r = roots(p)
MATLAB executes the above statement and returns the following result-
r = -6.8661 + 0.0000i -1.4247 + 0.0000i 0.6454 + 0.7095i 0.6454 - 0.7095i
This functionpolyis the inverse function of the root function and returns to the polynomial coefficients. For example-
p2 = poly(r)
MATLAB executes the above statement and returns the following result-
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
polyfitThe function finds the coefficients of a polynomial that fits a set of data in the least squares sense. If x and y are two vectors containing the x and y data to be fitted as a polynomial of degree n, then we can write-to fit the data with a polynomial-
p = polyfit(x,y,n)
Create a script file and enter the following code-
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 % Calculate the polyfit estimate over a smaller range % And plot the estimated values based on actual data for comparison x2 = 1:.1:6; y2 = polyval(p,x2); plot(x,y,'o',x2,y2) grid on
When running the file, MATLAB displays the following result-
p = 4.1056 -47.9607 222.2598 -362.7453 191.1250
and draw the following figure-