


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Math 436 Fall 2008 November 5
A polynomial is completely known by its coefficients! For example the polynomial
p(x) = x^3 โ 3 x^2 + 1
has coefficients, beginning with the largest power, a 1 = 1, a 2 = โ3, a 3 = 0 and a 4 = 1. Once these numbers are given, it is plain that p(1) = โ1 or p(4) = 17 or p(โ2) = โ19. MATLAB lets you compute polynomials with the polyval command. It has the structure
polyval(coefficient vector, input data)
In our case, we could enter
a = [1 -3 0 1] a = 1 -3 0 1 polyval(a,[1 4 -2]) ans = -1 17 -
As you can see, we can calculate the three values simultaneously entering a vector as the second argument. For a plot of the curve on the interval [โ 1 , 4] one could proceed as follows:
a = [1 -3 0 1] a = 1 -3 0 1 x=linspace(-1,4); % generate x data p = polyval(a,x); % calculate polynomial values plot(x,p); grid on % graph the function
โ5โ1 โ0.5 0 0.5 1 1.5 2 2.5 3 3.5 4
0
5
10
15
20
It is known that a polynomial of degree n has exactly n, possibly complex roots. Here you have to account for the multiplicity of the roots as well. The roots command determines the roots from the coefficients. For our polynomial p(x) = x^3 โ 3 x^2 + 1 we find
roots([1 -3 0 1]) ans =
-0.
which is consistent with the information from the graph. The reverse of the roots command is poly. Here you can easily calculate the coefficients of a polynomial with roots at desired locations. Suppose you want to find a polynomial with simple roots at โ1 and 4, and a double root at 2. With poly you just need to enter
poly([-1 4 2 2]) ans = 1 -7 12 4 -
The desired polynomial is p(x) = x^4 โ 7 x^3 + 12x^2 + 4x โ 16. A graph confirms the result:
a = poly([-1 4 2 2]); x = linspace(-2,5); p = polyval(a,x); plot(x,p), grid on
โ20โ2 โ1 0 1 2 3 4 5
0
20
40
60
80
100
Polynomial interpolation is built into MATLAB. The command is
a = polyfit(x data, y data, degree of the polynomial)
It returns the coefficients of the interpolating polynomial. From class there is exactly one polynomial of degree โค N โ 1 which passes through N given points. MATLAB lets you approximate with lower
b = polyfit(X,Y,1) b = 3.8571 -1. y = polyval(b,x); % for a plot plot(x,y,X,Y,โoโ), grid on
Thus the least square regression line is y = 3. 8571 x โ 1.
โ5โ1 0 1 2 3 4 5
0
5
10
15
20
For parametric curves (x(t), y(t) you need to display the x-values versus the y with t as a parameter. For example, the curve x(t) = t^3 and y(t) = t^2 with โ 1 โค t โค 1 can be generated as follows:
t = linspace(-1,1); x = polyval([1 0 0 0],t); y = polyval([1 0 0],t); plot(x,y), grid on, axis(โequalโ)
โ1 โ0.8 โ0.6 โ0.4 โ0.2 0 0.2 0.4 0.6 0.8 1
โ0.
0
1
This, of course, is the graph of the function y = 3
x^2.