How to determine the coefficient from the argmin function ?

2 次查看(过去 30 天)
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.

采纳的回答

Bjorn Gustavsson
Bjorn Gustavsson 2021-9-22
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.
HTH
  4 个评论
Lyhour Chhay
Lyhour Chhay 2021-9-24
Thank you very much sir, I really appreciate your kindness. I am apology for late response because I didn't get notification on the e-mail due to it moves to junk mail. However, your provide coding is working very well. Again, Thank you very much sir.
Lyhour Chhay
Lyhour Chhay 2021-9-25
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.

请先登录,再进行评论。

更多回答(2 个)

Walter Roberson
Walter Roberson 2021-9-25
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 = 
symvar(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 = 
60.6375
F = matlabFunction(subs(eqn1, [X,Z], [x,z]), 'vars', {[a1, b1, c1]})
F = function_handle with value:
@(in1)((in1(:,1)+in1(:,3))./in1(:,2)+2.0).^2+((in1(:,1).*2.0+in1(:,3))./in1(:,2)+3.0).^2+((in1(:,1).*5.0+in1(:,3))./in1(:,2)+4.0).^2+((in1(:,1).*3.0-in1(:,3))./in1(:,2)+6.0).^2+((in1(:,1).*5.0-in1(:,3))./in1(:,2)-4.0).^2
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.
  4 个评论
Walter Roberson
Walter Roberson 2021-9-27
I choose 5 because it 5 terms is large enough to have problems finding an exact root if the problem turned out to involve roots of a polynomial. (Though I guess 6 would have been even better for that.) If I choose too small of a number, then there might hypothetically have been exact solutions that could not generalize to large number of terms.
I did not need more than 5 in order to illustrate the pattern. There was no information in the original problem to suggest that the vector lengths were 10... or 20... or 2713503 ... And I needed to choose some number in order to have a pattern that MATLAB could take the derivatives of.
does this number use for find the best coeff. a1,b1,c1
No, 5 is standing in for length(X) and length(Z) and has nothing to do with starting points for searches.
The starting point, abc0, does affect the values that are returned by fminsearch(). fminsearch() is deterministic, and will use the initial value to guess directions to search in. However, whatever locations are probed by fminsearch, the outcome will still be that the first value divided by the second value will be a constant that depends upon the X and Z values, and the third value divided by the second value will be a (different) constant that depends upon the X and Z values. The besta1 and bestc1 formulas show how you would calculate the ratios.
Lyhour Chhay
Lyhour Chhay 2021-9-28
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,

请先登录,再进行评论。


John D'Errico
John D'Errico 2021-9-27
编辑:John D'Errico 2021-9-27
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 个评论
Lyhour Chhay
Lyhour Chhay 2021-9-28
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,

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Number Theory 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by