%MONTECARLO                             last updated 3/20/01
%           An interactive routine to approximate area under a curve y = f(x)
%           using a Monte Carlo estimation procedure.
%
%PROCEDURE: A curve y = f(x) is sketched on an interval [a,b] over which
%           it is assumed to be nonnegative. A rectangle with base on the
%           x-axis over [a,b] is constructed with height d >= max(f(x) 
%           over [a,b]. A stream of randomly generated points in the rectangle 
%           is generated and plotted. A count of the number of points with
%           y-coordinate less thatn the corresponding y-coordinate on f(x) is
%           tallied. The approximation of the area under f(x) on [a,b] is computed
%           as (#tallied points/total # used) times (the area of enclosing rectangle).
%
%RESTRICTIONS:   ==> the function must be in terms of variable x;
%                    no check is made
%                ==> the routine assumes it is >= 0 on interval [a,b];
%                    no check is made
%                
%INPUTS: The user is asked to enter the following information and click on
%        "ACCEPT" buttons. 
%
%              <><> click in the input areas prior to trying to type
%                   in the corresponding information <><>
%
%        The information can be changed prior to clicking
%        the "ACCEPT" button. If the information is not acceptable to the 
%        program defaults are used. At any time you can get HELP, RESTART, or
%        QUIT.
%
%        ==> formula in x for function f(x)  (default is f(x) = x)
%        ==> interval over which f(x) in >= 0  (default is [0,1])
%        ==> d = height of enclosing rectangle  (default is max(f(x)) on [a,b])
%        ==> number of random trials  (default is 100)
%
%OUTPUT: A sketch of y = f(x) in the rectangle from a<= x < = b, 0 <= f(x) <= d
%        together with random points. The screen is labeled with the formula
%        for f(x), [a,b], [0,d], number of trials, number of points with y-coordinate
%        less that the corresponding value of f(x), and the area approximation.
%
%OPTIONS: The number of trials used can be re-entered to make further approximations.
%         To change any other information use the RESTART button.
%
%
%
%  By: David R. Hill, Mathematics Dept., Temple Univ.
%      Philadelphia, PA. 19122    Email: hill@math.temple.edu
  
%=======================================================================
%CONSTRUCTED for use with NSF Project DEMOs with POSITIVE IMPACT
%=======================================================================

%strings
s0=' ';
header='Monte Carlo Area Approximation';
cont='Press ENTER to continue.';
mess1='Enter an expression for f(x) ==>';
mess2='Then Press ACCEPT f(x) Button.';
mess3='Enter interval [a,b] for x-values ==>';
mess4='f(x) must be >= 0 on this interval.';
mess5='Then Press ACCEPT [a,b] Button.';
mess6='Enter d >= max(f(x)) over [a,b] ==>';
mess7='Then Press ACCEPT d Button.';
mess8='ERROR: d set to max of f(x).';
mess9='Enter number of random trials ==>';
mess10='Then Press ACCEPT Trials Button.';
mess11='Improper choice; set to 100 Trials.';
mess12='OPTION: new number of trials.  ==>';
mess13='For any other changes click RESTART.';
mess14='If done, click QUIT.';

menu=[
   'Options;                             ';
   '                                     ';
   '1. Get information on this routine.  ';
   '                                     ';
   '2. Execute this routine.             ';
   '                                     ';
   '0. QUIT!                             ';
   '                                     '];

%INTRO
goodch='N';
while goodch=='N'
   clc,disp(header),disp(s0),disp(menu)
   ch=input('Make your choice ==> ');
   if isempty(ch)==1,ch==99;end
   if ch==1,help montecarlo,disp(s0),disp(cont),pause,goodch='Y';end
   if ch==2,goodch='Y';end
   if ch==0,clc,disp('Montecarlo is over!');goodch='Y';return,end
   if goodch=='N',disp('IMPROPER choice; try again.');disp(cont);pause;end
end
%seeding the random number generator
rand('state',sum(100*clock))
%
%
%COLOR settings
bkgr='white'; %background color
bkgray1=[.8 .8 .8]; %background for editable text

%dummy callback
donothing=' ';

%
%CALL back for quit button
done = ['close(gcf);clc,disp(''MONTECARLO is over!'')'];
       
%callback for the help button
helps='set(gcf,''visible'',''off'');clc,help montecarlo,disp(cont),';
helps=[helps 'pause,set(gcf,''visible'',''on'');'];
%

%callback for funcin
%
getfunc='delete(funcin),delete(funcinfrm),set(txtHndl,''string'',mess1),';
getfunc=[getfunc 'set(txtHndl2,''string'',mess2);'];
getfunc=[getfunc 'fbox=uicontrol(''Style'',''edit'',''units'',''normal'','];
getfunc=[getfunc '''fontsize'',12,''position'',[.55 .2 .3 .05],'];
getfunc=[getfunc '''back'',bkgray1,''fontweight'',''bold'',''call'',donothing);'];
getfunc=[getfunc 'fenter=uicontrol(''Style'',''push'',''units'',''normal'','];
getfunc=[getfunc '''fontsize'',12,''position'',[.65 .1 .13 .05],'];
getfunc=[getfunc '''string'',''ACCEPT f(x)'','];
getfunc=[getfunc '''back'',bkgray1,''fontweight'',''bold'',''call'',gotfunc);'];
%

%got function callback
gotfunc='set(txtHndl,''string'',s0),set(txtHndl2,''string'',s0);'; 
gotfunc=[gotfunc 'f=get(fbox,''string'');if isempty(f)==1,f=''x'';end;'];
gotfunc=[gotfunc 'fname=[''f(x) = '' f];subplot(datasub);'];
gotfunc=[gotfunc 'text(0,.9,fname,''color'',''red'',''fontsize'',14,'];
gotfunc=[gotfunc '''fontweight'',''bold'',''erasemode'',''none'');']; 
gotfunc=[gotfunc 'delete(fbox),delete(fenter),'];
gotfunc=[gotfunc 'set(txtHndl,''string'',mess3),set(txtHndl2,''string'',mess4);'];
gotfunc=[gotfunc 'set(txtHndl3,''string'',mess5);'];
gotfunc=[gotfunc 'abbox=uicontrol(''Style'',''edit'',''units'',''normal'','];
gotfunc=[gotfunc '''fontsize'',12,''position'',[.55 .2 .15 .05],'];
gotfunc=[gotfunc '''back'',bkgray1,''fontweight'',''bold'',''call'',donothing);'];
gotfunc=[gotfunc 'abenter=uicontrol(''Style'',''push'',''units'',''normal'','];
gotfunc=[gotfunc '''fontsize'',12,''position'',[.55 .1 .15 .05],'];
gotfunc=[gotfunc '''string'',''ACCEPT [a,b]'','];
gotfunc=[gotfunc '''back'',bkgray1,''fontweight'',''bold'',''call'',gotab);'];

%gotab callback
gotab='set(txtHndl,''string'',s0),set(txtHndl2,''string'',s0);set(txtHndl3,''string'',s0);'; 
gotab=[gotab 'xint=get(abbox,''string'');if isempty(xint)==1,xint=''[0,1]'';end;'];
gotab=[gotab 'xval=str2num(xint);a=xval(1);b=xval(2);'];
gotab=[gotab 'abname=[''on interval '' xint];subplot(datasub);'];
gotab=[gotab 'text(0,.8,abname,''color'',''red'',''fontsize'',14,'];
gotab=[gotab '''fontweight'',''bold'',''erasemode'',''none'');']; 
gotab=[gotab 'delete(abbox),delete(abenter),'];
gotab=[gotab 'vecf=vectorize(f);x=a:.01:b;y=eval(vecf);xdata=x;ydata=y;'];
gotab=[gotab 'pltsub=subplot(''position'',[.05 .3 .60 .60]);'];
gotab=[gotab 'plot(xdata,ydata,''-m'',''linewidth'',2);'];
gotab=[gotab 'axis([a b 0 max(y)+.1]);axis(axis),hold on;'];
gotab=[gotab 'set(txtHndl,''string'',mess6),set(txtHndl2,''string'',mess7);'];
gotab=[gotab 'dbox=uicontrol(''Style'',''edit'',''units'',''normal'','];
gotab=[gotab '''fontsize'',12,''position'',[.55 .2 .15 .05],'];
gotab=[gotab '''back'',bkgray1,''fontweight'',''bold'',''call'',donothing);'];
gotab=[gotab 'denter=uicontrol(''Style'',''push'',''units'',''normal'','];
gotab=[gotab '''fontsize'',12,''position'',[.55 .1 .15 .05],'];
gotab=[gotab '''string'',''ACCEPT d'','];
gotab=[gotab '''back'',bkgray1,''fontweight'',''bold'',''call'',gotd);'];


%got d callback
gotd='dst=get(dbox,''string'');';
gotd=[gotd 'if isempty(dst)==1,dst=''0'';end;'];
gotd=[gotd 'dval=str2num(dst);if dval<max(ydata),dval=max(ydata);'];
gotd=[gotd 'set(txtHndl3,''string'',mess8);set(dbox,''string'',num2str(dval));'];
gotd=[gotd 'pause(3);end;'];
gotd=[gotd 'delete(dbox),delete(denter),set(txtHndl,''string'',s0),'];
gotd=[gotd 'set(txtHndl2,''string'',s0);set(txtHndl3,''string'',s0);'];
gotd=[gotd 'uplim=dval;newax=[a b 0 uplim];'];
gotd=[gotd 'axis(newax);subplot(pltsub);plot([a b],[dval dval],''k-'');'];
gotd=[gotd 'plot([b b],[0 dval],''k-'');'];
gotd=[gotd 'dname=[''rectangle has y in [0,'' dst '']''];'];
gotd=[gotd 'subplot(datasub);'];
gotd=[gotd 'text(0,.7,dname,''color'',''red'',''fontsize'',14,'];
gotd=[gotd '''fontweight'',''bold'',''erasemode'',''none'');']; 
gotd=[gotd 'set(txtHndl,''string'',mess9),set(txtHndl2,''string'',mess10);'];
gotd=[gotd 'trbox=uicontrol(''Style'',''edit'',''units'',''normal'','];
gotd=[gotd '''fontsize'',12,''position'',[.55 .2 .15 .05],'];
gotd=[gotd '''back'',bkgray1,''fontweight'',''bold'',''call'',donothing);'];
gotd=[gotd 'trenter=uicontrol(''Style'',''push'',''units'',''normal'','];
gotd=[gotd '''fontsize'',12,''position'',[.55 .1 .15 .05],'];
gotd=[gotd '''string'',''ACCEPT Trials'','];
gotd=[gotd '''back'',bkgray1,''fontweight'',''bold'',''call'',gottr);'];

%got number of trials callback
gottr='trst=get(trbox,''string'');mess11sig=''N'';';
gottr=[gottr 'if isempty(trst)==1,trst=''100'';mess11sig=''Y'';end;'];
gottr=[gottr 'trval=str2num(trst);'];
gottr=[gottr 'if trval==0 | trval~=abs(fix(trval)),'];
gottr=[gottr 'trst=''100'';trval=100;mess11sig=''Y'';end;'];
gottr=[gottr 'if mess11sig==''Y'','];
gottr=[gottr 'set(txtHndl3,''string'',mess11);set(trbox,''string'',''100'');'];
gottr=[gottr 'pause(3),end;'];
gottr=[gottr 'delete(trbox),delete(trenter),set(txtHndl,''string'',s0),'];
gottr=[gottr 'set(txtHndl2,''string'',s0);set(txtHndl3,''string'',s0);'];
gottr=[gottr 'trname=[''Number of Trials = '' trst ];'];
gottr=[gottr 'subplot(datasub);'];
gottr=[gottr 'tr1=text(0,.6,trname,''color'',''red'',''fontsize'',14,'];
gottr=[gottr '''fontweight'',''bold'',''erasemode'',''none'');']; 
gottr=[gottr 'eval(dotr);'];

%do trials
dotr='incnt=0;subplot(pltsub);for j=1:trval,';
%dotr=[dotr 'xt=rnumh(a,b,0);yt=rnumh(0,dval,0);'];
%removed this statement to make routine independent of other  m-files
%put in line below
dotr=[dotr 'rnum=rand;xt=a*(1-rnum)+b*rnum;rnum=rand;yt=dval*rnum;'];
dotr=[dotr 'plot(xt,yt,''*b'',''erasemode'',''none''),drawnow,'];
dotr=[dotr 'if trval<101 & fix(j/20)*20==j,pause(1);end,'];
dotr=[dotr 'x=xt;if yt<eval(f),incnt=incnt+1;end;end;'];
dotr=[dotr 'subplot(datasub);'];
dotr=[dotr 'resname=[num2str(incnt) '' out of '' trst '' trials''];'];
dotr=[dotr 'res=text(0,.45,resname,''color'',''blue'',''fontsize'',14,'];
dotr=[dotr '''fontweight'',''bold'',''erasemode'',''none'');']; 
dotr=[dotr 'resname1=['' below curve y = f(x).''];'];
dotr=[dotr 'res1=text(0,.35,resname1,''color'',''blue'',''fontsize'',14,'];
dotr=[dotr '''fontweight'',''bold'',''erasemode'',''none'');']; 
dotr=[dotr 'resname2=[''Approximate area = '' num2str(incnt/trval*(b-a)*dval)];'];
dotr=[dotr 'res2=text(0,.2,resname2,''color'',''black'',''fontsize'',14,'];
dotr=[dotr '''fontweight'',''bold'',''erasemode'',''none'');']; 
dotr=[dotr 'set(txtHndl,''string'',mess12),set(txtHndl2,''string'',mess13);'];
dotr=[dotr 'set(txtHndl3,''string'',mess14);'];
dotr=[dotr 'change=uicontrol(''Style'',''push'',''units'',''normal'','];
dotr=[dotr '''fontsize'',12,''position'',[.55 .2 .21 .05],'];
dotr=[dotr '''string'',''CHANGE # of Trials'','];
dotr=[dotr '''back'',bkgray1,''fontweight'',''bold'',''call'',chtr);'];


%change number of trials callback
chtr='set(txtHndl,''string'',s0),set(txtHndl2,''string'',s0);';
chtr=[chtr 'set(txtHndl3,''string'',s0);'];
chtr=[chtr 'delete(change),delete(res),delete(res1),delete(res2),delete(tr1),'];
chtr=[chtr 'delete(pltsub);'];
chtr=[chtr 'pltsub=subplot(''position'',[.05 .3 .60 .60]);'];
chtr=[chtr 'plot(xdata,ydata,''-m'',''linewidth'',2);'];
chtr=[chtr 'axis(newax);axis(axis),hold on;'];
chtr=[chtr 'plot([a b],[dval dval],''k-'');'];
chtr=[chtr 'plot([b b],[0 dval],''k-'');'];
chtr=[chtr 'set(txtHndl,''string'',mess9),set(txtHndl2,''string'',mess10);'];
chtr=[chtr 'trbox=uicontrol(''Style'',''edit'',''units'',''normal'','];
chtr=[chtr '''fontsize'',12,''position'',[.55 .2 .15 .05],'];
chtr=[chtr '''back'',bkgray1,''fontweight'',''bold'',''call'',donothing);'];
chtr=[chtr 'trenter=uicontrol(''Style'',''push'',''units'',''normal'','];
chtr=[chtr '''fontsize'',12,''position'',[.55 .1 .15 .05],'];
chtr=[chtr '''string'',''ACCEPT Trials'','];
chtr=[chtr '''back'',bkgray1,''fontweight'',''bold'',''call'',gottr);'];

%++++++++++++++++++++++++++++++++++
%SUBPLOTS are used for regions of the screen which are not uicontrols
%++++++++++++++++++++++++++++++++++

%THE GUI STARTS HERE
hfig=figure('units','normal','position',[0 0 1 1],'color',bkgr);
axis('off')
%screen title
headsub=subplot('position',[0 .95 .5 .05]);axis('off');
text(0,0,header,'color','blue','fontsize',24,...
    'fontweight','bold','erasemode','none')

%vanity
vansub=subplot('position',[.9 .01 .05 .03]);axis('off');
text(0,0,'by D.R.Hill','color','black','fontsize',10,'fontweight','bold',...
     'fontangle','oblique','erasemode','none')  
  
%creating messagearea for data displays 
datasub=subplot('position',[.7 .5 .25 .45]);axis('off');  
  
%
%START PUSH BUTTONS
butfrm1=uicontrol('style','frame','units','normal',...
                 'position',[.89 .04 .1 .24],'backgroundcolor','y');
%undoh = uicontrol('style','push','units','normal','pos',[.9 .29 .08 .06], ...
%        'string','UNDO','fontweight','bold','call',undoit);
helph = uicontrol('style','push','units','normal','pos',[.9 .21 .08 .06], ...
        'string','HELP','fontweight','bold','call',helps);
endh = uicontrol('style','push','units','normal','pos',[.9 .05 .08 .06], ...
        'string','QUIT','fontweight','bold','call',done);
rstarth = uicontrol('style','push','units','normal','pos',[.9 .13 .08 .06], ...
        'string','RESTART','fontweight','bold','call',...
        'close(gcf),montecarlo');
     
%enter function
%
funcinfrm=uicontrol('style','frame','units','normal',...
                 'position',[.54 .24 .12 .08],'backgroundcolor','r');
funcin = uicontrol('style','push','units','normal','pos',[.55 .25 .1 .06], ...
        'string','Enter f(x)','fontsize',12,'fontweight','bold','call',getfunc);

     
     
%message area
%
txtPos1=[.1 .2 .4 .05];
txtPos2=[.1 .14 .4 .05];
txtPos3=[.1 .08 .4 .05];

    txtHndl=uicontrol( ...
	'Style','text', ...
        'Units','normalized', ...
        'Max',10, ...
        'BackgroundColor',[1 1 1], ...
        'Position',txtPos1,'fontweight','bold','fontsize',12, ...
        'String',s0,'foregroundcolor','magenta');
   txtHndl2=uicontrol( ...
	'Style','text', ...
        'Units','normalized', ...
        'Max',10, ...
        'BackgroundColor',[1 1 1], ...
        'Position',txtPos2,'fontweight','bold','fontsize',12, ...
        'String',s0,'foregroundcolor','magenta');
   txtHndl3=uicontrol( ...
	'Style','text', ...
        'Units','normalized', ...
        'Max',10, ...
        'BackgroundColor',[1 1 1], ...
        'Position',txtPos3,'fontweight','bold','fontsize',12, ...
        'String',s0,'foregroundcolor','magenta');
