Finding minimum values for two-unknown equation!!!

Hello,
'a' 'b' 'c' 'd' are known in the equation. We are trying to find values for 'x' and 'y' that minimizes 'F'
Is it possible to insert this function directly in to Matlab? If yes, how can we minimize?
Thanks

 Accepted Answer

This works:
a = 3;
b = 5;
c = 7;
d = 13;
% % x = xy(1), y = xy(2)
F = @(xy) hypot(xy(1)-a, xy(2)-b) + hypot(xy(1)-c, xy(2)-d)
[xy, fval] = fminsearch(F,[1 1])

4 Comments

Thanks for the answer:) It works well. Actually , we have a more complex problem here.
How can we find x and y that minimize this expression?
It looks like fairly straightforward coding. You can use the hypot function to evaluate the square roots. The others are just as you wrote them, substituting xy(1) for x and xy(2) for y.
You can combine the terms that have 4330 and 2500 in them, so one term becomes:
(10144 - 9786).*hypot(xy(1)-4330,xy(2)-2500)
similarly for:
2*((xy(1)-4330).^2 + (xy(2)-2500).^2)
to make things easier.
It’s rather tedious typing but not difficult programming. You can either do it in a function file or as an ‘anonymous function’ as I did. (See the documentation for Anonymous Functions for details.)
Thank you so much. I have one more question.I want to generalize the code.
What I am trying to do is to insert these equations into summation like this:
I know BS and r matrices. x and y still unknown.
I have this error message in Matlab. How can I fix it?
You have two errors that I see that will cause your loop to fail:
  1. Don’t define F as F(ti,:). Just define it as F and then call it as F in fminsearch. Also, F(ti,:) or F should not be on the right side of the equation. You are correct in putting it in the loop, since apparently you are defining different constants ( r, BS ) in it. Test F outside the loop on one set of parameters before you put it in the loop, then with fminsearch. That way, you know it works, what it returns, and that it works with fminsearch.
  2. In your fminsearch output in the loop, xy has 2 elements ( x and y ), so return it as either [xy(ti,:),fval] or [xy(:,ti),fval] depending on whether it returns xy at each iteration as a row or column vector. Also, I suggest you store fval as well, so you know the function minimum at each iteration. This may be of interest to you later.

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 21 May 2014

Edited:

on 23 May 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!