clear all xN = [-2:.01:2]; n = [4 8 16 32] for i=1:length(n) xCheb{i} = -cos((2*[1:n(i)]-1)*pi/2/n(i)); xUnif{i} = -1+2*[1:n(i)]/(n(i)+1); end f1 = inline('abs(x)','x'); f2 = inline('sign(x)','x'); for i=1:length(n) coefCheb1{i} = polyfit(xCheb{i},f1(xCheb{i}),n(i)-1); coefUnif1{i} = polyfit(xUnif{i},f1(xUnif{i}),n(i)-1); coefCheb2{i} = polyfit(xCheb{i},f2(xCheb{i}),n(i)-1); coefUnif2{i} = polyfit(xUnif{i},f2(xUnif{i}),n(i)-1); end for i=1:length(n) YC1(i,:) = polyval(coefCheb1{i},xN); YU1(i,:) = polyval(coefUnif1{i},xN); YC2(i,:) = polyval(coefCheb2{i},xN); YU2(i,:) = polyval(coefUnif2{i},xN); end figure(1);clf for i=1:length(n) subplot(4,4,i) plot(xN,f1(xN),xN,YU1(i,:)); axis([-1.2 1.2 -.1 1.5]); title(['n = ' num2str(n(i))]); if i==1;ylabel('Unif');end end for i=1:length(n) subplot(4,4,i+4) plot(xN,f1(xN),xN,YC1(i,:)); axis([-1.2 1.2 -.1 1.5]); if i==1;ylabel('Cheb');end end for i=1:length(n) subplot(4,4,i+8) plot(xN,f2(xN),xN,YU2(i,:)); axis([-1.2 1.2 -1.5 1.5]); if i==1;ylabel('Unif');end end for i=1:length(n) subplot(4,4,i+12) plot(xN,f2(xN),xN,YC2(i,:)); axis([-1.2 1.2 -1.5 1.5]); if i==1;ylabel('Cheb');end end print -dtiff hw3.tiff