%Deze m-file powerseries(type,LS) berekent verschillende powerseries. Het gebruik is als volgt: %Je geeft twee parameterwaardes mee type en LS. De parameter type is het %type van functie die je wenst te benaderen, type=1 is een bgtan(x) rond %x=0, type=2 is een sin(x)/x rond x=0, type=3 is de functie %f(x)=x/(x-1)/(x+2), en type=4 de convergentie f_n(x)=tanh(n*x). %De paramter LS is een {0,1}-parameter. LS=0 betekent %dat je alleen de Laurent-reeksontwikkeling wil zien terwijl LS=1 betekent %dat je de polynoombenadering in Least Squares wil zien. Je krijgt steeds 2 %plots te zien. De bovenste is de echte functie en diens benadering(en), de %onderste plot is fout in logaritmische schaal. function powerseries(type,LS) % mov = avifile('powerseries0.avi','quality',100,'compression', 'Cinepak','fps',5) %% Atan benadering %Atan benaderen via een Taylorreeks switch type case 1 % clear,close all x=-2:0.01:2;x=x(:); f=@(x)atan(x);g=f(x); A=@(x,n)(-1)^n/(2*n+1).*x.^(2*n+1); if LS==0 for n=0:1:15 Approx(:,n+1)=A(x,n); if round((n-1)/3)==floor((n-1)/3) Taylor(:,n+1)=sum(Approx,2); subplot(2,1,1) axis([-2,2,-2,2]) plot(x,Taylor(:,n+1),'r') xlabel('x') ylabel('f(x)') hold on subplot(2,1,2) semilogy(x,abs(g-Taylor(:,n+1)),'r') xlabel('x'),ylabel('error') hold on subplot(2,1,1) plot(x,f(x),'b'); pause(1) end end subplot(2,1,1) plot(x,f(x),'b'); else %MS-approximation for n=0:1:15 P=polyfit(x,g,n); if round((n-1)/3)==floor((n-1)/3) Y(:,n+1)=polyval(P,x); subplot(2,1,1) axis([-2,2,-2,2]) plot(x,Y(:,n+1),'r') xlabel('x') ylabel('f(x)') hold on subplot(2,1,2) semilogy(x,abs(g-Y(:,n+1)),'r') xlabel('x'),ylabel('error') hold on subplot(2,1,1) plot(x,f(x),'b');,pause(1) end end subplot(2,1,1) plot(x,f(x),'b'); end %% sinc function %sinc function case 2 x=-10:0.01:10;x=x(:); f=@(x)sin(x)./x; g=f(x);g(1001)=1; A=@(x,n)(-1)^n*x.^(2*n)/factorial(2*n+1); if LS==0 for n=0:1:15 Approx(:,n+1)=A(x,n); if round((n-1)/3)==floor((n-1)/3) Taylor(:,n+1)=sum(Approx,2); subplot(2,1,1) axis([-10,10,-2,2]) plot(x,Taylor(:,n+1),'r') xlabel('x') ylabel('f(x)') hold on subplot(2,1,2) semilogy(x,abs(g-Taylor(:,n+1)),'r') xlabel('x'),ylabel('error') hold on subplot(2,1,1) plot(x,f(x),'b'); pause(1) end end subplot(2,1,1) plot(x,f(x),'b'); else %MS-approximation for n=0:1:15 P=polyfit(x,g,n); if round((n-1)/3)==floor((n-1)/3) Y(:,n+1)=polyval(P,x); subplot(2,1,1) axis([-10,10,-2,2]) plot(x,Y(:,n+1),'r') xlabel('x') ylabel('f(x)') hold on subplot(2,1,2) semilogy(x,abs(g-Y(:,n+1)),'r') xlabel('x'),ylabel('error') hold on subplot(2,1,1) plot(x,f(x),'b');,pause(1) end end subplot(2,1,1) plot(x,f(x),'b'); end %% f(z)=z/(z-1)/(z+2) rond z=-2 case 3 x=-3:0.01:-1-0.01;x=x(:); f=@(x)x./(x+1)./(x+2); g=abs(f(x)); A=@(x,n)(x+2).^n; Approx(:,1)=2./(x+2); if LS==0 for n=1:1:10 Approx(:,n+1)=A(x,n-1); if round((n-1)/3)==floor((n-1)/3) Taylor(:,n+1)=sum(Approx,2); subplot(2,1,1) xlabel('x'),ylabel('f(x)') semilogy(x,abs(Taylor(:,n+1)),'r') hold on subplot(2,1,2) xlabel('x'),ylabel('error') semilogy(x,abs(g-Taylor(:,n+1)),'r') hold on subplot(2,1,1) semilogy(x,g,'b');,pause(1) end end else % %MS-approximation for n=0:1:20 x(find(g==inf))=[];g(find(g==inf))=[]; P=polyfit(x,g,n); if round((n-1)/3)==floor((n-1)/3) Y(:,n+1)=polyval(P,x); subplot(2,1,1) xlabel('x'),ylabel('f(x)') semilogy(x,abs(Y(:,n+1)),'r') hold on subplot(2,1,2) xlabel('x'),ylabel('error') semilogy(x,abs(g-Y(:,n+1)),'r') hold on subplot(2,1,1) semilogy(x,g,'b');,pause(1) end end end %% %machtreeks case 4 x=-2:0.01:2;x=x(:); f=@(x)-(x<0)+(x>0); A=@(x,n)tanh(n*x); if LS==0 for n=0:1:25 Approx(:,n+1)=A(x,n); if round((n-1)/3)==floor((n-1)/3) Taylor(:,n+1)=Approx(:,n+1); subplot(2,1,1) plot(x,Taylor(:,n+1),'r') hold on plot(x,f(x),'b'); xlabel('x') ylabel('f(x)') axis([-2,2,-2,2]) subplot(2,1,2) hold on semilogy(x,abs(f(x)-Taylor(:,n+1)),'r') m(n)=max(abs(f(x(102:300))-Taylor(102:300,n+1))); xlabel('x') ylabel('error') axis([-2,2,10^(-15),10^1]) pause(1) end end subplot(2,1,1) plot(x,f(x),'b'); xlabel('x') ylabel('f(x)') axis([-2,2,-2,2]) subplot(2,1,2) xlabel('x') ylabel('|f(x)-Taylor_n(x)|') axis([-2,2,10^-20,10^1]) %MS-approximation else for n=0:1:25 P=polyfit(x,f(x),n); if round((n-1)/3)==floor((n-1)/3) Y(:,n+1)=polyval(P,x); subplot(2,1,1) axis([-2,2,-2,2]) plot(x,Y(:,n+1),'r') hold on subplot(2,1,2) semilogy(x,abs(f(x)-Y(:,n+1)),'r') hold on,subplot(2,1,1) plot(x,f(x),'b');pause(1) end end end end