function h = plotsvm2d(trainx,trainy,supvecs,margvecs,predfunc,varargin)

  mm = [min(trainx) ; max(trainx)] ;
  mm = mm + [-0.1 ; 0.1] * (mm(2,:)-mm(1,:)) ;
  x1range = mm(:,1);
  x2range = mm(:,2);
  [x1,x2] = meshgrid(fillrange(x1range),fillrange(x1range));
  [s1 s2] = size(x1);

  [y,z] = feval(predfunc,[reshape(x1,[],1) reshape(x2,[],1)],varargin{:});
  y = reshape(y,s1,s2);
  z = reshape(z,s1,s2);
  
  h = figure;
  hold;
  % Plot the decision boundary
  contour(x1,x2,z,[0 0]);
  contour(x1,x2,z,[-1 1],':');
  % Circle the support vectors
  scatter(trainx(supvecs,1),trainx(supvecs,2),70,'k');
  % Double circle the margin vectors
  scatter(trainx(margvecs,1),trainx(margvecs,2),150,'k');
  % Plot the training set
  plot(trainx(trainy>0,1),trainx(trainy>0,2),'r+',...
      trainx(trainy<0,1),trainx(trainy<0,2),'bx');

  
function filledrange = fillrange(range)
  filledrange = range(1):((range(2)-range(1))/100):range(2);
      
