MATLAB Answers

How to determine the coefficient from the argmin function ?

2 views (last 30 days)
Hello everyone! I want to determine the coefficient from the function (equation) as shown in the figure attachement. I have the value X and Z in vector form, How can I determine a,b,c coefficient ? Thank you very much.

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 22 Sep 2021
This is a standard minimization-problem, for that matlab has the fminsearch function (and possibly a couple more in the optimization toolbox), have a look at the help and documentation for that function.
Lyhour Chhay
Lyhour Chhay on 25 Sep 2021
Excuse me sir, I have some question related to guess value of parABC0. When I change the guess value, the coeff a,b,c also change. Therefore, do you have any idea to find the best initial guess value ? thank you very much sir.

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 25 Sep 2021
n = 5;
syms X [1 n] real
syms Z [1 n] real
syms a1 b1 c1
eqn1 = sum((Z + (a1*X + c1)/b1).^2)
eqn1 = 
parta1 = simplify(solve(diff(eqn1, a1),a1))
parta1 = 
eqn2 = simplify(subs(eqn1, a1, parta1))
eqn2 = 
partc1 = simplify(solve(diff(eqn2, c1),c1))
partc1 = 
eqn3 = simplify(subs(eqn2, c1, partc1))
eqn3 = 
ans = 
bestc1 = partc1
bestc1 = 
besta1 = simplify(subs(parta1, c1, bestc1))
besta1 = 
minval = simplify(subs(eqn1, {a1, c1}, {besta1, bestc1}))
minval = 
x = randi([-9 9], 1, n)
x = 1×5
-5 2 1 5 -3
z = randi([-9 9], 1, n)
z = 1×5
4 3 2 4 -6
vpa(subs(besta1, [X, Z], [x, z]))
ans = 
vpa(subs(bestc1, [X, Z], [x, z]))
ans = 
vpa(subs(minval, [X, Z], [x, z]))
ans = 
F = matlabFunction(subs(eqn1, [X,Z], [x,z]), 'vars', {[a1, b1, c1]})
F = function_handle with value:
abc0 = [-1 0 1]
abc0 = 1×3
-1 0 1
[bestarg, fval] = fminsearch(F, abc0)
bestarg = 1×3
-0.0087 0.0215 -0.0300
fval = 60.6375
What does this tell you? It tells you that the best a1 and c1 to use are linear multiples of b1, with the multiples depending upon the exact X and Z values, and that for any given non-zero b1, you will arrive at the same minimum sum-of-squares compared to using the formulas with different non-zero b1.
Lyhour Chhay
Lyhour Chhay on 28 Sep 2021
Dear Bjorn Gustavsson and Walter Roberson,
Thank you very much for your kindly response. Even though, I am not understand clearly, I will think and learn from your comment. Let me take time to study it. Again, thank you very much. Wish you the best.
Best Regards,

Sign in to comment.

John D'Errico
John D'Errico on 27 Sep 2021
Edited: John D'Errico on 27 Sep 2021
What you need to unerstand is there is NO simple solution. There are no unique set of values a1, b1, c1 that solve the problem. Suppose there was? So just imagine we had a solution that was the best possible. Call them a1,b1,c1.
Then we can arbitrarily choose, for ANY non-zero k:
a2 = k*a1
b2 = k*b1
c2 = k*c1
Now you need to see that
(a1*x + b1)/c1 == (a2*x + b2)/c2
since the value of k will factor out. So for ANY non-zero value of k, we can get a new, equally valid solution, if one exists. That means there is no unique optimal set.
What does that tell me? It says that if you use an optimizatin tool, and choose a different set of starting vcalues for a1,b1,c1, then you will find a different solution from the optimizer. What behavior are you seeing when you try to solve it using an optimzer? EXACTLY THAT. The solution is dependent on the starting values.
What is the resolution to this problem? Arbitrarily fix one of those three parameters to be 1. c1 is the most logical. So now your problem reduces to
argmin{(zi - (a1*xi+b1))^2}
over paramneters a1 and b1. This is especially nice, because you already have a simple tool to achieve that, in the form of polyfit.
ab = polyfit(x,z,1);
a1 = ab(1);
b1 = ab(2);
There is no need for any optimization, no starting values needed. Polyfit is blazingly fast. Every time you use it on the same data, you will get the same answer - the unique optimum. This is true as long as you have at least two distinct points. (They cannot be vertically oriented, since then the slope, thus a1 would be undefined.)
  1 Comment
Lyhour Chhay
Lyhour Chhay on 28 Sep 2021
I really appreciate your comment and your response sir. Thank you very much. I will think deeply base on your comment. I am not cleary undertand now. Let me take time to understand this. Again, thank you very much. Wish you the best.
Best Regards,

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by