% October 2017 % % Written by Per-Gunnar Martinsson, University of Oxford % % The methods follow Lloyd N. Trefethen's % % "Approximation Theory and Approximation Practice" % % textbook published by SIAM in 2013. Many code snippets are taken from % this text. They have been modified freey, however, and any errors are % due to PGM. function lecture07 DRIVER_remez1 DRIVER_remez2 DRIVER_remez3 return %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function DRIVER_remez1 x = chebfun('x'); f = min(abs(x+0.5),2*abs(x-0.5)); n = 6; pstar = minimax(f,n); err = max(abs(f-pstar)); xx = linspace(-1,1,1000); figure(1) subplot(1,2,1) hold off plot(xx,f(xx),'b',... xx,pstar(xx),'r',... 'LineWidth',2) legend('f','p^{*}','Location','NorthWest') title(sprintf('Best approximant for n=%d',n)) subplot(1,2,2) hold off plot(xx,f(xx)-pstar(xx),'r',... xx, err*ones(size(xx)),'g',... xx,-err*ones(size(xx)),'g',... 'LineWidth',2) title(sprintf('Error "f - p^{*}r" for n=%d',n)) keyboard return %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function DRIVER_remez2 x = chebfun('x'); f = sin(exp(3*x)); n = 28; pstar = minimax(f,n); err = max(abs(f-pstar)); xx = linspace(-1,1,1000); figure(1) subplot(1,2,1) hold off plot(xx,f(xx),'b',... xx,pstar(xx),'r',... 'LineWidth',2) legend('f','p^{*}','Location','NorthWest') title(sprintf('Best approximant for n=%d',n)) subplot(1,2,2) hold off plot(xx,f(xx)-pstar(xx),'r',... xx, err*ones(size(xx)),'g',... xx,-err*ones(size(xx)),'g',... 'LineWidth',2) title(sprintf('Error "f - p^{*}" for n=%d',n)) keyboard return %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function DRIVER_remez3 x = chebfun('x'); f = cumsum(sign(x+0.2)); %f = abs(x); %f = cumsum(sign(x-0.5)-sign(x+0.5)); figure(1) subplot(1,1,1) hold off plot(f) keyboard for n = 2.^(0:10) p = minimax(f,n); subplot(2,1,1) plot(f,'k',p,'r') title(sprintf('Functions: n = %d',n)) legend('f','p') subplot(2,1,2) plot(f-p) legend('f-p') title(sprintf('Errors: n = %d',n)) pause end return %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%