 function plotplane2(x1,x2)

% function plotplane2(x1,x2)
% 
% Graphically represents the span of two vectors x1 and x2 in R^2.
% (If x1 and x2 are independent, this span is really all of R^2.)

 x1 = x1/norm(x1);              % normalize the vector x1 
 x2 = x2/norm(x2);              % normalize the vector x2 
 x1hat = x1 - x2*x2'*x1;        % compute the part of x1 orthogonal to x2
 x2hat = x2 - x1*x1'*x2;        % compute the part of x2 orthogonal to x1

 if norm(x2hat)<1e-10*norm(x2),
    fprintf('warning: degenerate plane; vectors are colinear.\n')
    plotline2(x1);
 else

% normalize the orthogonal vectors x1hat and x2hat;
% (x1,x2hat) will form an orthogonal basis for span(x1,x2).
    x1hat = x1hat/norm(x1hat);
    x2hat = x2hat/norm(x2hat);

% use the orthogonal basis to plot a nice square 
% to represent the 2-dimensional subspace span(x1,x2).
    X = 2*[x1 x2hat -x1 -x2hat];
    fill(X(1,:),X(2,:),[0.7 0.7 1]), hold on

% plot basis vectors for the plane as red and blue lines
    plot([-x1(1) x1(1)],[-x1(2) x1(2)],'r-','linewidth',2) 
    plot([-x2(1) x2(1)],[-x2(2) x2(2)],'b-','linewidth',2) 

% the following lines plot some crude arrowheads
    h = .035;
    arrowhead = [x1+h*x2hat (1+2*h)*x1 x1-h*x2hat];
    fill(arrowhead(1,:),arrowhead(2,:),[1 0 0],'edgecolor',[1 0 0])
    fill(-arrowhead(1,:),-arrowhead(2,:),[1 0 0],'edgecolor',[1 0 0])
    arrowhead = [x2+h*x1hat (1+2*h)*x2 x2-h*x1hat];
    fill(arrowhead(1,:),arrowhead(2,:),[0 0 1],'edgecolor',[0 0 1])
    fill(-arrowhead(1,:),-arrowhead(2,:),[0 0 1],'edgecolor',[0 0 1])

% use equal scaling of axes
    axis equal, axis(2.1*[-1 1 -1 1])
 end
