## Author: M. Turhan ÇOBAN
## Created: 2021-11-07
## monte_carlo_opt

function [BestX BestY] =monte_carlo_opt(fx,xmin,xmax,MAXIMUM)
## MAXIMUM: number of trials
## xmin minimum values of search region
## xmax maximum values of search region
## fx search function
n=length(xmin);
fmax=-realmax('double');
for j=1:n
      dx(j)=(xmax(j)-xmin(j));
endfor
for t=1:MAXIMUM
   ## select a random point vc 
   for j=1:n
      vc(j)=xmin(j)+dx(j)*rand;
   endfor # for j=1:n
   ## calculate the function for vc 
   fc=fx(vc);
   if fc>fmax
     fmax=fc;
     vmax=vc;
   endif # if fc>fmax
endfor # for t=1:MAXIMUM
BestX=vmax;
BestY=fmax;  
end