0
|
4991 Views
|
2 Replies
|
3 Total Likes
View groups...
Share

# 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:= A={{1,1},{1,2},{1,3}};b={7,7,8};In:= FindMinimum[Norm[A.k-b],{k,ConstantArray[0,Last@Dimensions[A]]}]During evaluation of In:= FindMinimum::nrnum: The function value {12.7279,12.7279,12.7279} is not a real number at {k} = {{0.,0.}}. >>Out= 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:= Norm[A.k - b]Out= Sqrt[Abs[-8 + {{1, 1}, {1, 2}, {1, 3}}.k]^2 + 2 Abs[-7 + {{1, 1}, {1, 2}, {1, 3}}.k]^2]In:= % /. k -> {0., 0.}Out= {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:= 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:= 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= {0.408248, {v1 -> 6.33333, v2 -> 0.5}}In:= N[LeastSquares[A, b]]Out= {6.33333, 0.5}