% 1. Polynomials and Roots % We can define a polynomial by % (1) algebraically, give its coefficients; % (2) geometrically, give its roots. c = [3 -2.23 -5.1 9.8]; polyval(c,0) ans = 9.8000 % to plot the polynomial with coefficients in c: x = -1:0.1:1; y = polyval(c,x); plot(x,y) % we can compute the roots of p via roots: r = roots(c) r = -1.5985 1.1709 + 0.8200i 1.1709 - 0.8200i polyval(c,r) ans = 1.0e-013 * -0.3197 0.1066 - 0.0178i 0.1066 + 0.0178i % Now we may define the same polynomial, % up to its leading constant via "poly" : cr = poly(r) cr = 1.0000 -0.7433 -1.7000 3.2667 % the coefficient vector cr defines a cubic % polynomial with the same roots as r, % how would we check this ? polyval(cr,r) ans = 1.0e-015 * 0.4441 0 + 0.2220i 0 - 0.2220i p = c(1)*cr p = 3.0000 -2.2300 -5.1000 9.8000 c c = 3.0000 -2.2300 -5.1000 9.8000 norm(p-c) ans = 1.2909e-014 % we just computed the backward error: % ans measures the change to c we should % make for the polynomial defined by c % to have the computed roots r % % 2. Curve Fitting % Polynomial of low degree are often used % in modeling. We will generate some noise % to the current polynomial defined by c. % Given the samples in x and y % -- we used these samples for plotting. rc = polyfit(x,y,3) rc = 3.0000 -2.2300 -5.1000 9.8000 % linear regression approximates the data % with a linear function lc = polyfit(x,y,1) lc = -3.1260 8.9823 hold on ly = polyval(lc,x); plot(x,ly,'r') % an application of polyfit is the % removal of noise % suppose that our samples "y" have % errors on them noise = randn(1,size(y,2)); ey = y + noise; % noisy samples plot(x,ey,'g+') plot(x,ey,'g') % to remove the noise, we do a polyfit cc = polyfit(x,ey,3) cc = 3.2885 -3.2438 -5.0382 10.2963 cy = polyval(cc,x); % for plotting plot(x,cy,'r'); % we started by looking at polynomials % given by their coefficient vectors, % then by their roots, and finally by % possibly noisy samples diary off