MATLAB: Linear Programming, optimal location for “main” Station on a Line

linearlinearprogramminglocationon a lineoptimal locationoptimizationstation

Hello there,
I'am having trouble solving an Exercise regarding a linear programming optimization.
fmincon(@dist, x0, [], [], [], [], lb, ub, @d_constr)
First I want to generate a Random set of numbers of "little" stations and their distribution on a Line:
j = randi([1 1000]);
n = randi([2 20]);
m = randi([j],n,1);
Then I need to find the location for the "Main" Station,
which needs to bet at the point on the Line where it has the minimum distance to all other Stations.
the function for the sum is this:
function d = dist(x)
L = x(1);
d = @(m) sum(m - L);
end
Now a function for the constrains is needed:
As the only one is L>0 && L<j ; where j is the total lenght of the line.
function [g,h] = d_constr(x)
L = x(1);
g(1) = L < j;
g(2) = L > 0;
h = [];
end
Well it doesn't work and Iam blank.
This is the Error I am getting all the Time:
Error using fmincon (line 700)
FMINCON requires all values returned by functions to be of data type double.
Error in distance (line 69)
fmincon(@dist, x0, [], [], [], [], x1, x2, @d_constr)
Thank you for your time <3

Best Answer

which needs to bet at the point on the Line where it has the minimum distance to all other Stations.
If you mean the minimum least squares distance, then this can be found with fmincon as follows
Linitial = mean(m);
Loptimal = fmincon(@(L) sum(m-L).^2, Linitial, [],[],[],[],0,j)
However, you should expect this to converge in zero iterations because the initial guess here mean(m) is the known analytical solution to this problem.
If you mean the minimum absolute distance sum(abs(m-L)), then you shouldn't be using fmincon at all. You should be using linprog to solve the linear program,
This makes a little more sense, since you said this was supposed to be a linear programming problem.