Click here to Skip to main content
15,891,734 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
function [xval] = simulated_annealing()
tic
for i= 0:3
    x(i+1)=floor(random('unif',-10,10));
end

xval=x(1)^2+x(2)^2+x(3)^2+x(4)^2;

disp (x);
t = 1 ;
bool =true;
while bool
    
   t=t*.9;
   disp (t);
  temp=true;
  count=0;
   while (temp && count<11) 
       count=count+1;
      %random selection from neighbors
      newx(1)= floor(random('unif',x(1)-1,x(1)+1));
      newx(2)= floor(random('unif',x(2)-1,x(2)+1));
      newx(3)=floor( random('unif',x(3)-1,x(3)+1));
      newx(4)= floor(random('unif',x(4)-1,x(4)+1));
      %evaluate new x value
      newxval=x(1)^2+x(2)^2+x(3)^2+x(4)^2;
      
      disp(x);
      disp(newx);
      delta = newxval- xval ;
      if delta<0
       x=newx;
       temp=false;
      else 
        p =exp(-delta/t);
         if p > 0.3
            x=newx;
            temp=false;
     
         end
      end
   
   end
   
   if count>11
       bool=false;
   end
   
   
end

toc
disp (toc);
Posted
Updated 20-Oct-10 8:22am
v4

1 solution

think you want to use randi instead of random:
http://www.mathworks.com/help/techdoc/ref/randi.html[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900