HW1 - 1a function[cost = cost(s s= floor(s if(s<=500 s>=0...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1a. function [cost] = cost(s) s= floor(s); if cost=(400-(s-21).^2).*sin(s.*pi./6); else display( 'S must be between 0 and 500' ) end >> x=0:1:500; plot(x,cost(x),'ro'); title('cost as a function of f'); ylabel('cost'); xlabel('s') 1b. function [snew] = neighbor(s) if x= randi([max(s-25,0),min(s+25,500)]); while x == s %prevents neighborhood function from selection 's' again x= randi([max(s-25,0),min(s+25,500)]); end snew= x; else display( 's not in valid range' ); %'s' must be between 0 and 500 end 1c. function solution= RW(sinitial, maxiter) if solution = zeros(maxiter,5);
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
scurrent= sinitial; sbest= scurrent; COSTbest =cost(scurrent); for i=1:maxiter scurrent= neighbor(scurrent); COSTcurrent= cost(scurrent); if COSTcurrent<COSTbest COSTbest=COSTcurrent; sbest= scurrent; end solution(i,1)= i; solution(i,2)= scurrent; solution(i,3)= sbest; solution(i,4)= COSTcurrent; solution(i,5)= COSTbest; end else display( 's not in valid range' ); end 1d. function solution = RS(sinitial, maxiter) if solution = zeros(maxiter,5); snew= sinitial; sbest= snew; COSTnew= cost(snew); COSTbest= cost(snew); for i=1:maxiter x= randi([0,500]); %prevent S from staying the same while x == snew x= randi([0 500]); end stest= x; COSTtest= cost(stest); if COSTtest<COSTbest COSTbest= COSTtest; COSTnew= COSTtest; snew= stest; sbest= snew; end solution(i,1)= i; solution(i,2)= snew; solution(i,3)= sbest;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/02/2011 for the course ORIE 5430 taught by Professor Shoemaker during the Fall '11 term at Cornell.

Page1 / 6

HW1 - 1a function[cost = cost(s s= floor(s if(s<=500 s>=0...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online