Page 35 - Algorithm Collections for Digital Signal Processing Applications using MATLAB
P. 35

1. Artificial Intelligence                                        23

           3.3      M-program for Simulated Annealing

           Matlab  program  for  minimizing  the  function  f(x)=  x+10*sin(5*x)+
           7*cos(4*x)+sin(x),where x varies from 0 to 5 .
              ___________________________________________________________

              sagv.m

              x=0:1/300:5
              plot(x, minfcn(x))
              hold
              curval=rand*5;
              curcost=minfcn(curval);
              T=1000;
              for i=1:1:100
                  newval=rand*5;
                 newcost=minfcn(newval);
                 if((newcost-curcost)<=0)
                     curval=newval;
                     curcost=minfcn(curval);
                  else
                     if(exp((curcost-newcost)/T)>rand)
                         curval=newval;
                         curcost=minfcn(curval);
                     end
                 end
              T=T/log(100)
              plot(curval,minfcn(curval),'r*');
              CURRENTVAL{i}=curval;
              M{i}=minfcn(curval);
              pause(0.2)
              end

              ___________________________________________________________

                   minfcn.m

                   function [res]=minfcn(x)
                   res=x+10*sin(5*x)+7*cos(4*x)+sin(x)
   30   31   32   33   34   35   36   37   38   39   40