Message Boards Message Boards

0
|
5246 Views
|
2 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Using FindMinimum to find LeastSquares

Posted 10 years ago
I know there is a LeastSquares function, but I am wondering how you'd implement it using FindMinimum? For example:



In[148]:= A={{1,1},{1,2},{1,3}};
b={7,7,8};
In[154]:= FindMinimum[Norm[A.k-b],{k,ConstantArray[0,Last@Dimensions[A]]}]
During evaluation of In[154]:= FindMinimum::nrnum: The function value {12.7279,12.7279,12.7279} is not a real number at {k} = {{0.,0.}}. >>
Out[154]= FindMinimum[Norm[A.k-b],{k,ConstantArray[0,Last[Dimensions[A]]]}]


Thank you
POSTED BY: Abdul Dakkak
2 Replies
Posted 10 years ago
 In[1]:= A = {{1, 1}, {1, 2}, {1, 3}};
 b = {7, 7, 8};
 vars = Table[ToExpression["v" <> ToString[i]], {i, 1, Last[Dimensions[A]]}];
 FindMinimum[Norm[A.vars - b], vars]
 
 During evaluation of In[1]:= FindMinimum::lstol: The line search decreased the step size to within the tolerance
 specified by AccuracyGoal and PrecisionGoal but was unable to find a sufficient decrease in the function. You may
 need more than MachinePrecision digits of working precision to meet these tolerances. >>
 
Out[4]= {0.408248, {v1 -> 6.33333, v2 -> 0.5}}

In[5]:= N[LeastSquares[A, b]]

Out[5]= {6.33333, 0.5}
POSTED BY: Bill Simpson
When FindMinimum evaluates the first argument, k does not yet have a value so because of Plus threading we get
In[4]:= Norm[A.k - b]

Out[4]= Sqrt[Abs[-8 + {{1, 1}, {1, 2}, {1, 3}}.k]^2 + 2 Abs[-7 + {{1, 1}, {1, 2}, {1, 3}}.k]^2]

In[5]:= % /. k -> {0., 0.}

Out[5]= {12.7279, 12.7279, 12.7279}
which has the wrong dimensions. One could do, for example,
f[k_?VectorQ] := Norm[A.k - b];
FindMinimum[f[k], {k, {0, 0}}]
or
SetSystemOptions["EvaluateNumericalFunctionArgument" -> False];
FindMinimum[Norm[A.k - b], {k, {0, 0}}]
POSTED BY: Ilian Gachevski
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract