- #1
mralam
- 11
- 0
hey guys,
i want to wirte a function which takes as its arguments two lists of co-ordinates (the first
is a list of customers and the second for possible switch locations), and determines the index of the best switch location to minimise the amount of cable required.
I have done the following:
this code keeps coming back with errors that i am not able figure out how to solve it.
Any advise on how to go about this?
thanks
i want to wirte a function which takes as its arguments two lists of co-ordinates (the first
is a list of customers and the second for possible switch locations), and determines the index of the best switch location to minimise the amount of cable required.
I have done the following:
Code:
%Get the number of customers%
NC = input('Enter the number of costumers there is: ');
customers = [];
%Get each customers co-ordinates and place them into an array%
for i = 1:NC
%could also possibly get input as x y ..
x = input('Enter the x value of costumer %.1d : ', i);
y = input('Enter the y value of costumer %.1d : ', i);
%Array to hold customers%
customers = [customers;x y];
end
%Get the number of switches%
NS = input('Enter the number of switches there is: ');
switches=[];
%Get each switchs co-ordinates and place them into an array%
for i = 1:NS
%could also possibly get input as x y ..
x = input('Enter the x value of switch %.1d : ', i);
y = input('Enter the y value of switch %.1d : ', i);
%Array to hold customers%
switches = [switches;x y];
end
%Create an array with the same size as the number of switches to hold the total cable lengths%
switch_sizes=[];
%do two jobs at once - find the lengths and keep track of the smallest one and the index of the smallest%
smallest_total = 0;
smallest_index = 0;
%For each switch%
for i = 1:NS
%get co-ords of the switch we are looking at%
xs = switches(i,1);
ys = switches(i,2);
%Set total to zero%
total = 0;
%For each customer%
for j = 1:NC
%Calculate the distance between the customer and the switch%
x= customers(j,1);
y= customers(j,2);
distance = sqrt(((x - xs)^2)+((y-ys)^2));
%TODO%
%Add it to the total for this switch%
total = total + distance;
end
if (smallest_total > total)
smallest_total = total;
smallest_index = i;
end
%enter the total for this switch into the total cable lengths array%
switch_sizes = [switch_sizes; total];
end
%Return the co-ordinates of the switch with the lowest value in the array of total cable lengths%
%check syntax%
%not sure whether he wants co-ordinates or the index, returning index as it says in the brief but check%
return smallest_index;
%alternative return switches(smallest_index); %
this code keeps coming back with errors that i am not able figure out how to solve it.
Any advise on how to go about this?
thanks