Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed problem 3's chebyshev graph and displayed the graphs for N=20 a…

…nd N=50
  • Loading branch information...
commit 9ee6102b524fe4b6d9d202ec58e5931fc848cfea 1 parent 008f349
@uberj authored
Showing with 70 additions and 3 deletions.
  1. +70 −3 labs/lab4/lab4.m
View
73 labs/lab4/lab4.m
@@ -33,14 +33,14 @@
xcheb(i)=(a+b)/2 + (b-a)/2 * cos( (i-.5)*pi/K );
end
plot(xcheb, y, 'o');
-title('N+1 = 11 points spaced by using the chebychev method');
+title('Chebyshev data [N = 10]');
ycheb = f(xcheb);
PNcheb = polyfit(xcheb,ycheb,N);
vcheb = polyval(PNcheb,t);
cheberr = norm(f(t)-vcheb,inf);
figure;
-plot(x,y,'o',t,f(t),'-',t,vcheb,'--')
+plot(xcheb,ycheb, 'o', t, f(t), '-', t, vcheb, '--');
title(sprintf('f(t) and Chebychev Interpolation {10}(t), err=%g',cheberr))
%%
@@ -66,5 +66,72 @@
% chebyshev points get's more and more accurate.
%%
-% To see these poloynomials I changed N (at the top of the file) to 20
+% Here are the cases when N = 20 and N = 50
% and then 50.
+
+N = 20;
+x = linspace(-5,5,N+1);
+f = inline('1./(1+x.*x)', 'x');
+y = f(x);
+figure;
+plot(x,y,'o');
+title('N+1 = 21 equally-spaced data points');
+t = [-5:.1:5];
+
+PN = polyfit(x,y,N);
+v = polyval(PN,t);
+err = norm(f(t)-v,inf);
+
+figure;
+plot(x,y,'o',t,f(t),'-',t,v,'--');
+title(sprintf('f(t) and P_{20}(t) [N = 20], err%g', err));
+
+K = N+1;
+a = -5;
+b = 5;
+xcheb = zeros(1,K);
+for i=1:K
+ xcheb(i)=(a+b)/2 + (b-a)/2 * cos( (i-.5)*pi/K );
+end
+ycheb = f(xcheb);
+PNcheb = polyfit(xcheb,ycheb,N);
+vcheb = polyval(PNcheb,t);
+
+cheberr = norm(f(t)-vcheb,inf);
+figure;
+plot(x,y,'o',t,f(t),'-',t,vcheb,'--')
+title(sprintf('f(t) and Chebychev Interpolation {10}(t) [N = 20], err=%g',cheberr))
+
+% N = 50
+
+N = 50;
+x = linspace(-5,5,N+1);
+f = inline('1./(1+x.*x)', 'x');
+y = f(x);
+figure;
+plot(x,y,'o');
+title('N+1 = 51 equally-spaced data points');
+t = [-5:.1:5];
+PN = polyfit(x,y,N);
+v = polyval(PN,t);
+err = norm(f(t)-v,inf);
+
+figure;
+plot(x,y,'o',t,f(t),'-',t,v,'--');
+title(sprintf('f(t) and P_{50}(t) [N = 50], err%g', err));
+
+K = N+1;
+a = -5;
+b = 5;
+xcheb = zeros(1,K-1);
+for i=1:K
+ xcheb(i)=(a+b)/2 + (b-a)/2 * cos( (i-.5)*pi/K );
+end
+ycheb = f(xcheb);
+PNcheb = polyfit(xcheb,ycheb,N);
+vcheb = polyval(PNcheb,t);
+
+cheberr = norm(f(t)-vcheb,inf);
+figure;
+plot(x,y,'o',t,f(t),'-',t,vcheb,'--')
+title(sprintf('f(t) and Chebychev Interpolation {10}(t) [N = 50], err=%g',cheberr))
Please sign in to comment.
Something went wrong with that request. Please try again.