Docsity

Polynomials in Matlab - Numerical Analysis | MATH 436, Assignments of Mathematical Methods for Numerical Analysis and Optimization

Toggle sidebar
bg1
Math 436
Fall 2008
November 5
Polynomials in MATLAB
1 Polynomial Evaluation
A polynomial is completely known by its coefficients! For example the polynomial
p(x) = x3โˆ’3x2+ 1
has coefficients, beginning with the largest power, a1= 1, a2=โˆ’3, a3= 0 and a4= 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 -19
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
โˆ’1 โˆ’0.5 0 0.5 1 1.5 2 2.5 3 3.5 4
โˆ’5
0
5
10
15
20
pf3
pf4

Partial preview of the text

Download Polynomials in Matlab - Numerical Analysis | MATH 436 and more Assignments Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

Math 436 Fall 2008 November 5

Polynomials in MATLAB

1 Polynomial Evaluation

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

2 Roots

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

3 Polynomial Interpolation

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

4 Parametric Curves

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.