2 views (last 30 days)

Show older comments

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.

HTH

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)

parta1 = simplify(solve(diff(eqn1, a1),a1))

eqn2 = simplify(subs(eqn1, a1, parta1))

partc1 = simplify(solve(diff(eqn2, c1),c1))

eqn3 = simplify(subs(eqn2, c1, partc1))

symvar(eqn3)

bestc1 = partc1

besta1 = simplify(subs(parta1, c1, bestc1))

minval = simplify(subs(eqn1, {a1, c1}, {besta1, bestc1}))

x = randi([-9 9], 1, n)

z = randi([-9 9], 1, n)

vpa(subs(besta1, [X, Z], [x, z]))

vpa(subs(bestc1, [X, Z], [x, z]))

vpa(subs(minval, [X, Z], [x, z]))

F = matlabFunction(subs(eqn1, [X,Z], [x,z]), 'vars', {[a1, b1, c1]})

abc0 = [-1 0 1]

[bestarg, fval] = fminsearch(F, abc0)

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.

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.)

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

Start Hunting!