function [W, Points] = TSPRand(n,d) %[W, Points] = TSPRand(n,d) %This program will randomly generate TSPs %Inputs: n, a integer greater than 1, and the second optional input d %is a positive number (default value 10). %The program will generate n points randomly distributed in the square %0<=x,y<=d. These points will be the vertices of the complete graph for %the TSP. The first output variable W is an n by n matrix whose entries %give the Euclidean distance between corresponding pairs of points. %The second output variable, Points, is an n by 2 matrix whose rows %give the x- and y-coordinates of the vertices. if nargin<2, d=10; end %set default value of d Points = 10*rand(n,2); W=zeros(n); %initialize price matrix for i=1:n, for j=i+1:n %fill in prices above main diagonal x1=Points(i,1); y1=Points(i,2); x2=Points(j,1); y2=Points(j,2); W(i,j)=sqrt((x2-x1)^2 + (y2-y1)^2); end, end W=W+W';%uses symmetry of W to fill in entries below main diagonal.