function [out] = intswitch(in)


global NumNodes Mode GraphHandle xv yv quadhandle;


numpts=1000;
xmin=str2num(get(findobj('Tag','XMin'), 'String'));
xmax=str2num(get(findobj('Tag','XMax'), 'String'));
ymin=str2num(get(findobj('Tag','YMin'), 'String'));
ymax=str2num(get(findobj('Tag','YMax'), 'String'));
func = get(findobj('Tag', 'Function'),'String');



switch in
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 case 'Refresh'
  cla;
  set(findobj('Tag','Axes'),'XLim',[xmin xmax]);
  set(findobj('Tag','Axes'),'YLim',[ymin ymax]);
  x=xmin;
  for k=1:numpts
    x=x+(xmax-xmin)/numpts;
    xv(k)=x;
    yv(k)=eval(func);
  end
  GraphHandle=line(xv,yv);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 case 'SetMax'
  set(findobj('Tag','MaxPts'),'String',get(findobj('Tag','NodeSlider'), 'Max'));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 case 'SetMin'
  set(findobj('Tag','MinPts'),'String',get(findobj('Tag','NodeSlider'), 'Min'));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 case 'SetPts'
  NumNodes=min(numpts,round(get(findobj('Tag','NodeSlider'),'Value')));
  if strcmp(Mode, 'SR') & mod(NumNodes,2)
    NumNodes=NumNodes+1;
  end
  if strcmp(Mode, 'CR')
    if mod(NumNodes,3)==1
      NumNodes=NumNodes+2;
    end
    if mod(NumNodes,3)==2
      NumNodes=NumNodes+1;
    end
  end
  set(findobj('Tag', 'NumPts'),'String',['Number of Nodes = ', num2str(NumNodes)]);
  h=(xmax-xmin)/NumNodes;
  nodes=[xmin:h:xmax];
  switch Mode
   case 'RL'
    cla;
    rsum=0;
    for k=1:NumNodes
      x=nodes(k);
      x1=x;
      y1=eval(func);
      rsum = rsum+y1;
      x=nodes(k+1);
      x2=x;
%      y2=eval(func);
      hold on;
      fill([nodes(k) nodes(k+1) nodes(k+1), nodes(k)], [0 0 y1 y1], 'y');
      GraphHandle=line(xv,yv);
    end
    set(findobj('Tag','RLVal'), 'String', num2str(h*rsum));
   case 'RR'
    cla;
    rsum=0;
    for k=1:NumNodes
      x=nodes(k);
      x1=x;
%      y1=eval(func);
      x=nodes(k+1);
      x2=x;
      y2=eval(func);
      rsum=rsum+y2;
      hold on;
      fill([nodes(k) nodes(k+1) nodes(k+1), nodes(k)], [0 0 y2 y2], 'y');
      GraphHandle=line(xv,yv);      
    end
    set(findobj('Tag','RRVal'), 'String', num2str(h*rsum));
   case 'RM'
    cla;
    rsum=0;
    for k=1:NumNodes
    x=nodes(k);
    x1=x;
    x=nodes(k+1);
    x2=x;
    x=(x1+x2)/2;
    y=eval(func);
    rsum=rsum+y;
    hold on;    
    fill([nodes(k) nodes(k+1) nodes(k+1), nodes(k)], [0 0 y y], 'y');
    GraphHandle=line(xv,yv);      
    end
    set(findobj('Tag','RMVal'), 'String', num2str(h*rsum));
   case 'TR'
    cla;
    coeffs=2*ones(1,NumNodes+1);
    coeffs(1)=1;
    coeffs(NumNodes+1)=1;
    for k=1:NumNodes
    x=nodes(k);
    x1=x;
    y1=eval(func);
    rsum(k)=y1;
    x=nodes(k+1);
    x2=x;
    y2=eval(func);
    hold on;    
    fill([x1 x1 x2 x2], [0 y1 y2 0], 'y');
    GraphHandle=line(xv,yv);      
    end
    rsum(NumNodes+1)=y2; %Pick the last value
%    figure;plot(rsum)
    set(findobj('Tag','TRVal'), 'String', num2str(h*sum(rsum.*coeffs)/2));
   case 'SR'
    if mod(NumNodes,2)==0
      cla;
      coeffs=(-ones(1,NumNodes+1)).^[1:NumNodes+1]+3;
      coeffs(1)=1;
      coeffs(NumNodes+1)=1;
      for k=1:2:NumNodes
        x=nodes(k);
        x1=x;
        y1=eval(func);
        rsum(k)=y1;
        x=nodes(k+1);
        x2=x;
        y2=eval(func);
        rsum(k+1)=y2;
        x=nodes(k+2);
        x3=x;
        y3=eval(func);
        quadfunc='y1*(x-x2).*(x-x3)./((x1-x2).*(x1-x3))+y2.*(x-x1).*(x-x3)./((x2-x1).*(x2-x3))+y3.*(x-x1).*(x-x2)./((x3-x1).*(x3-x2))';
        x=x1:(x3-x1)/10:x3;
        quad=eval(quadfunc);
%        figure;plot(x,quad);pause(5)
        hold on;    
        quadhandle(k)=fill([x(1) x x3], [0 quad 0], 'y');
%        quadhandle(k)=fill([x(1) x(1:5)  x(5)], [0 quad(1:5) 0], 'y');
        GraphHandle=line(xv,yv,'Color','red');
      end
      rsum(NumNodes)=y2;
      rsum(NumNodes+1)=y3;
      set(findobj('Tag','SRVal'), 'String', num2str(h*sum(rsum.*coeffs)/3));
    else
      disp('Not an even set of nodes. Try again.')
    end      
   case 'CR'
    coeffs=3*ones(1,NumNodes+1);
    coeffs(1:3:NumNodes+1)=2;
    coeffs(1)=1;
    coeffs(NumNodes+1)=1;
    cla;
    for k=1:3:NumNodes
      x=nodes(k);
      xn(1)=x;
      y(1)=eval(func);
      rsum(k)=y(1);
      x=nodes(k+1);
      xn(2)=x;
      y(2)=eval(func);
      rsum(k+1)=y(2);
      x=nodes(k+2);
      xn(3)=x;
      y(3)=eval(func);
      rsum(k+2)=y(3);
      x=nodes(k+3);
      xn(4)=x;
      y(4)=eval(func);      
      cubicfunc=polyfit(xn,y,3);
      x=xn(1):(xn(4)-xn(1))/10:xn(4);
      cubic=polyval(cubicfunc,x);
      hold on;
      cubhandle(k)=fill([x(1) x x(length(x))], [0 cubic 0], 'y');
      GraphHandle=line(xv,yv,'Color','red');
    end
    rsum(NumNodes+1)=y(4);
    set(findobj('Tag','CRVal'), 'String', num2str(3*h*sum(rsum.*coeffs)/8));
   otherwise,
    end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 case 'RL'
  Mode='RL';
  deselect('RLRadio')
  intswitch('Refresh')
  intswitch('SetPts')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 case 'RR'
  Mode='RR';
  deselect('RRRadio');
  intswitch('Refresh')
  intswitch('SetPts')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 case 'RM'
  Mode='RM';
  deselect('RMRadio');
  intswitch('Refresh')
  intswitch('SetPts')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 case 'TR'
  Mode='TR';
  deselect('TRadio');
  intswitch('Refresh')
  intswitch('SetPts')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 case 'SR'  
  Mode='SR'  ;
  deselect('SRadio');
  intswitch('Refresh')
  intswitch('SetPts')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 case 'CR'  
  Mode='CR'  ;
  deselect('CRadio');
  intswitch('Refresh')
  intswitch('SetPts')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 otherwise,
end


function []=deselect(in)


Rbut={'RLRadio' 'RRRadio' 'RMRadio' 'TRadio' 'SRadio' 'CRadio'};


for k=1:length(Rbut)
  if ~strcmp(Rbut{k},in)
    h=  findobj('Tag',Rbut{k});
    set(h, 'Value', get(h,'Min'));
  end
end
