Lecture 31: Polynomials and Curve Fitting. ------------------------------------------ % saving/loading the data (revisited) M = [1 2 3 4 5]; openvar('M', M); save('h:\data'); clear load('h:\data'); openvar('M', M); diary off % a polynomial is presented by the array of its coefficients % c = 3x^3-2x^2-5x+11 c = [3 -2 -5 11] c = 3 -2 -5 11 polyval(c,3.3) ans = 80.5310 x = -1:0.1:1; openvar('x', x); y = polyval(c,x); openvar('y', y); plot(x,y); r = roots(c); openvar('r', r); plot(r,'rx') dc = polyder(c); openvar('dc', dc); hold on plot(roots(dc),'bo') openvar('dc', dc); rdc = roots(dc) rdc = 1.0000 -0.5556 cr = poly(r) cr = 1.0000 -0.6667 -1.6667 3.6667 c(1)*cr - c ans = 1.0e-013 * 0 0.0044 0.0533 -0.2487 % curve fitting % polyfit(x,y,d), where d is the degree of the curve hold off plot(x,y); hold on plot(x,y,'rx') noise = randn(1,size(y,2)); noise = noise/norm(noise); % normalize the noise ey = y + noise; plot(x,ey,'go') ec = polyfit(x,ey,3); yec = polyval(ec,x); plot(x,yec,'g--') ec = polyfit(x,ey,5); yec = polyval(ec,x); plot(x,yec,'r--') ec = polyfit(x,ey,20); yec = polyval(ec,x); plot(x,yec,'r--') % Exe 2, p 153 f = randn(1,101); openvar('f', f); r = roots(f); hold off plot(r); plot(r, 'bx'); diary off