How to optimize the parameters in Matlab ?

Hi to all!
I have two vectors: A = [ 10 13 15 7 18]; B = [ a*b 2*b*a 5*a*b^2 4*b*a^2 10*a*b]
'a' and 'b' are symbolic variables.
How can i find the optimal 'a' and 'b' value which the nearest distance between these vectors was obtained ?
Thanks! Avihai

Answers (1)

High school calc, admittedly augmented with the symbolic toolbox to do the hard work.
syms a b
A = [ 10 13 15 7 18];
B = [ a*b 2*b*a 5*a*b^2 4*b*a^2 10*a*b];
You did say distance, so I can only presume you meant Euclidean distance. I suppose you might have meant a different norm for that distance, but the solution is not quite as easy then. Not impossible though.
We can minimize the square of the distance as well as the actual distance. The solution is still a minimizer. But it is easier to work with the square. Square roots get messier.
S = sum((A-B).^2);
Differentiate and solve.
ab = solve(diff(S,a)==0,diff(S,b)==0);
[double(ab.a),double(ab.b)]
ans =
0 + 0i 0 + 0i
-7.7143 + 0i 0 + 0i
0 + 0i -2.88 + 0i
-1.1843 + 0i -0.57097 + 0i
1.1191 + 0i 1.6777 + 0i
-0.15276 + 1.9496i -0.39204 + 1.5898i
-0.15276 - 1.9496i -0.39204 - 1.5898i
1.1215 + 1.993i -0.44892 - 0.5544i
-0.51085 - 0.96577i 1.0168 + 1.7401i
-0.51085 + 0.96577i 1.0168 - 1.7401i
1.1215 - 1.993i -0.44892 + 0.5544i
See that the first 5 roots found were the only real ones. Check the distance for those sets of values. Of course, this presumes that a and b must be real. If they may be complex, then the change is trivial. (Feel free to test it yourself, but I will comment that the 5th root is the smallest solution, even if I included the complex ones.)
for i = 1:5
subs(subs(S,a,vpa(ab.a(i))),b,vpa(ab.b(i)))
end
ans =
867.0
ans =
867.0
ans =
867.0
ans =
739.63984241608439457361480510495
ans =
154.57954730438169628972253023058
So the minimizer is the 5th root we found.
vpa(ab.a(5))
ans =
1.1191001059233511458448608902447
vpa(ab.b(5))
ans =
1.6776979215894517344756792659522
We can plot the surface, or contours of it around the minimizer. Here are the contours.
ezcontour(S,[1 2],[1 2])
grid on
hold on
plot(1.119,1.678,'r+')
WTP?

2 Comments

Thanks John about your detailed answer! But, it seems to be an inferior method to calculate it and why ? The vectors that I gave (e.g. A and B) are an example to my problem. Actually, I have a very complicated symbolic values in vector B and using your suggested method leads to very (!) long run time. May you know how can I apply lsqcurvefit routine to solve this problem ? Thanks again, Avihai
Exactly what is inferior? In fact, it solves your question perfectly! Your question had no reason for me to believe that it would not suffice. The answer I posed has no reliance or need for starting values. See that a general optimizer may converge to a local optimizer rather than the global one. You must deal with convergence issues for an optimizer, set tolerances, etc.
Had you been more descriptive, I might have given a different answer, instead of wasting a fair amount of my own time to make this answer complete.
If you have significant information that you do not pass on in a question, expect to get answers that are less than useful, but answers that indeed do solve your question as posed efficiently, accurately, and correctly.
Yes, you can use lsqcurvefit or lsqnonlin.
help lsqcurvefit
help lsqnonlin
There are examples in the help docs that show how to use those codes.

Sign in to comment.

Asked:

on 27 Dec 2014

Edited:

on 28 Dec 2014

Community Treasure Hunt

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

Start Hunting!