# Using FindMinimum to find LeastSquares

Posted 10 years ago
4633 Views
|
2 Replies
|
3 Total Likes
|
 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
2 Replies
Sort By:
Posted 10 years ago
 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 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}