% 1. polynomials: coefficient vectors c = [ 3 -2.23 -5.1 9.8] c = 3.0000 -2.2300 -5.1000 9.8000 y = polyval(c,0) y = 9.8000 x = -1:0.01:+1; y = polyval(c,x); size(y) ans = 1 201 plot(x,y) % 2. Polynomials defined by roots % we can compute the roots of a polynomial: r = roots(c) r = -1.5985 1.1709 + 0.8200i 1.1709 - 0.8200i % let us check that r contains the roots: residual = polyval(c,r) residual = 1.0e-013 * -0.3197 0.1066 - 0.0178i 0.1066 + 0.0178i % via poly we can define a polynomial, % its coefficient vector given the roots q = poly(r) q = 1.0000 -0.7433 -1.7000 3.2667 q = c(1)*q q = 3.0000 -2.2300 -5.1000 9.8000 c c = 3.0000 -2.2300 -5.1000 9.8000 format long q q = Columns 1 through 2 3.000000000000000 -2.229999999999994 Columns 3 through 4 -5.100000000000011 9.800000000000001 c c = Columns 1 through 2 3.000000000000000 -2.230000000000000 Columns 3 through 4 -5.100000000000000 9.800000000000001 % we can make a plot of the roots plot(r,'bx') c = randn(200); size(c) ans = 200 200 r = roots(c(:,1)); plot(r,'r+') % 3. curve fitting c = [3 -2.23 -5.1 9.8]; x = -2:0.01:+2; y = polyval(c,x); % we just sampled the polynomial in the % range [-2,+2], with space 0.01 % (x,y) are the exact data noise = randn(1,size(y,2)); noise = noise/norm(noise); ey = y + noise; figure; plot(x,y) hold on plot(x,ey,'r+') hold off plot(x,y); hold on plot(x,ey,'r'); % we would like to recognize if the noisy % data comes from a cubic or a quadric ec = polyfit(x,ey,3) ec = Columns 1 through 2 3.005585921349974 -2.227866664172377 Columns 3 through 4 -5.112285076466999 9.797437655746483 % we see a coefficient vector close to % the original one appearing fy = polyval(ec,x); plot(x,fy) figure subplot(3,1,1) plot(x,y); subplot(3,1,2) plot(x,ey); subplot(3,1,3) plot(x,fy) diary off