Linear Programming, optimal location for "main" Station on a Line
Show older comments
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
1 Comment
the function for the sum is ... d=@(m) sum(m-L)
That doesn't really make sense for multiple reasons. First of all, you have written this as a function of m, whereas the unknown variable to be optimized is L. Second, the values m(i)-L can be negative and therefore cannot represent distances. Presumably you mean either the sum of absolute values or a sum of squares.
Accepted Answer
More Answers (0)
Categories
Find more on Linear Programming and Mixed-Integer Linear Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!