% function norm_demo(A) % % Illustrates matrix 1-, 2-, and infinity-norm for 2-by-2 matrices. % After Trefethen and Bau, Figure 3.1. function norm_demo(A) if (max(size(A)) ~= 2) | (min(size(A)) ~= 2) | ~(isreal(A)) fprintf('A must be a real 2-by-2 matrix\n') return end % compute scale factor for the plot axes ax = 1.05*max([1 norm(A,1) norm(A,2) norm(A,inf)]); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % 1-NORM %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % sample vectors of the form [x(1,k);x(2,k)] that are on the 1-norm unit ball, % i.e., |x(1,k)|+|x(2,k)| = 1. x1 = linspace(0,1,100); x2 = [1-x1 (fliplr(x1)-1)]; x2 = [x2 fliplr(x2)]; x1 = [x1 fliplr(x1)]; x1 = [x1 -fliplr(x1)]; x = [x1 -fliplr(x1); x2 fliplr(x2)]; % plot the 1-norm unit ball figure(1), clf subplot(1,2,1) plot(x(1,:),x(2,:),'b-','linewidth',2), axis equal, axis(ax*[-1 1 -1 1]) title('1-norm unit ball') hold on % find the vector on the unit ball that maximizes A*x. % for the 1-norm, this is +/- [1;0] or +/- [0;1]. % plot it as a red line. if (abs(A(1,1))+abs(A(2,1))) > (abs(A(1,2))+abs(A(2,2))) xmax = [1;0]; else xmax = [0;1]; end ymax = A*xmax; plot([0 xmax(1)], [0 xmax(2)], 'r-','linewidth',2) % now show the effect of applying A to these vectors on the unit ball. y = A*x; subplot(1,2,2) plot(y(1,:),y(2,:),'b-','linewidth',2), axis equal, axis(ax*[-1 1 -1 1]) title('A*(1-norm unit ball)') hold on % plot the maximal A*x in red (computed above) plot([0 ymax(1)], [0 ymax(2)], 'r-','linewidth',2) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % 2-NORM %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % sample vectors of the form [x(1,k);x(2,k)] that are on the 2-norm unit ball, % i.e., |x(1,k)|^2+|x(2,k)|^2 = 1. theta = linspace(0,2*pi,400); x = [cos(theta); sin(theta)]; % plot the 2-norm unit ball figure(2), clf subplot(1,2,1) plot(x(1,:),x(2,:),'b-','linewidth',2), axis equal, axis(ax*[-1 1 -1 1]) title('2-norm unit ball') hold on % find the vector on the unit ball that maximizes A*x. % for the 2-norm, we can compute this using the % singular value decomposition, covered in a later lecture. % plot this vector as a red line. [U,S,V] = svd(A); xmax = V(:,1); ymax = A*xmax; plot([0 xmax(1)], [0 xmax(2)], 'r-','linewidth',2) % now show the effect of applying A to these vectors on the unit ball. y = A*x; subplot(1,2,2) plot(y(1,:),y(2,:),'b-','linewidth',2), axis equal, axis(ax*[-1 1 -1 1]) title('A*(2-norm unit ball)') hold on % plot the maximal A*x in red (computed above) plot([0 ymax(1)], [0 ymax(2)], 'r-','linewidth',2) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % INFINITY-NORM %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % sample vectors of the form [x(1,k);x(2,k)] that are on the infinity-norm unit ball, % i.e., max{|x(1,k)|,|x(2,k)|} = 1. x = linspace(-1,1,100); o = ones(1,100); x = [x o fliplr(x) -o; o fliplr(x) -o x]; % plot the infinity-norm unit ball figure(3), clf subplot(1,2,1) plot(x(1,:),x(2,:),'b-','linewidth',2), axis equal, axis(ax*[-1 1 -1 1]) title('\infty-norm unit ball') hold on % find the vector on the unit ball that maximizes A*x. % for the infinity-norm, this is +/- [1;+/- 1]. % plot this vector as a red line. if norm(A*[1;1],inf)>norm(A*[1;-1],inf) xmax = [1;1]; else xmax = [-1;1]; end ymax = A*xmax; plot([0 xmax(1)], [0 xmax(2)], 'r-','linewidth',2) % now show the effect of applying A to these vectors on the unit ball. y = A*x; subplot(1,2,2) plot(y(1,:),y(2,:),'b-','linewidth',2), axis equal, axis(ax*[-1 1 -1 1]) title('A*(\infty-norm unit ball)') hold on % plot the maximal A*x in red (computed above) plot([0 ymax(1)], [0 ymax(2)], 'r-','linewidth',2)