Minimun of x and y
2 views (last 30 days)
Show older comments
I need help with coding this problem please!
Determine the integers x and y such that
(abs(x-1) + abs(y-1) + abs(x-3) + abs(y-5) + abs(x-8) + abs(y-3)) is a minimum.
Find the minimum.
0 Comments
Answers (2)
Walter Roberson
on 1 Apr 2012
You can break the problem up into cases according to when the abs() makes a difference.
For example, abs(x-1) is different than x-1 if x < 1, so you can break the expression into two cases, one assuming x>=1 and the other assuming x<1. In the situation where x<1 then abs(x-1) would be the same as -(x-1) and abs(x-3) is going to be the same as -(x-3) and likewise abs(x-8) would be the same as -(x-8) so you can combine those three terms into 12-3*x.
Then you would re-assess under the assumption that x>=1 so abs(x-1) is x-1, but case out abs(x-3) being different than x-3 ... leading to a different combination.
When you have all the combinations and boundary conditions set up, then because all the expressions are going to be linear, the minimum and maximum are going to be found at the boundaries, so loop over all the cases testing all the combination of boundary conditions and finding the combination that produces the minimum.
0 Comments
Aaditya Kalsi
on 1 Apr 2012
The easiest way to solve this problem is by using FMINSEARCH.
You want to find the minimum of f(x) where f(x) = (abs(x(1)-1) + abs(x(2)-1) + abs(x(1)-3) + abs(x(2)-5) + abs(x(1)-8) + abs(x(2)-3)).
I have replaced x by 'x(1)' and y by 'x(2)' as 'x' in my expression above is a vector with 2 elements.
Find the X and Y that minimize this by:
f = @(x) (abs(x(1)-1) + abs(x(2)-1) + abs(x(1)-3) + abs(x(2)-5) + abs(x(1)-8) + abs(x(2)-3));
solX = fminsearch(f, rand(2, 1));
solX(1) is the optimized X value and solX(2) is the optimized Y value. However, in this example we cannot force the optimized values to be integers. You may still round them however to find approximate answers.
To solve for iintegers, you will need the Global Optimization Toolbox (R2011b and onwards)
0 Comments
See Also
Categories
Find more on Systems of Nonlinear Equations 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!