# Counterpart of Maple's LSSolve in Mathematica

I am a great fan of Mathematica. Unfortunately, due to some specific tasks I had to move to Maple. I think it's my lack of info that is not letting me do those tasks. That is why I am asking some command replacements from you guys. I code some mesh free methods like Galerkin and spectral methods. They use a trial solution which contains some constants. After some treatment the problem gets converted to an over-determined system of algebraic equations. Maple has a command LSSolve to determine a best possible solution to that over-determined system. Can you please tell me its alternative in Mathematica. I really need it to bring all my coding back to Mathematica. my equations.

eq1 = -.3007024038*c^2 + (-0.4990858944 10^-2 - .3007024038*c)*
c;
eq2 = -.2004682692*c^2 + (-0.2495429472 10^-2 - .1503512019*c)*
c;
eq3 = -.1503512019*c^2 + (-0.1663619648 10^-2 - .1002341346*c)*
c;
eq4 = c + c + c - 1;


PS Here is a snap of the calculations on MAPLE • Perhaps LeastSquares is what you are looking for in MMA. – MarcoB May 23 '18 at 16:11
• thank you for you reply but i am afraid its not LeastSquares I am after. According to the documentation it solve linear systems. While I mostly encounter with nonlinear equations. As far as LSSolve is concerned it minimizes the Squared residual Error. I have tried to do it myslef in Mathematics but in vain. – naveed May 23 '18 at 16:20
• Then we need more information. Gives us an example of a (small!) problem you solve with LSSolve, together with the output you obtain from Maple. I do not use Maple, so I am only going with the information included in its documentation for LSSolve. If you don't provide more details, then only people intimately familiar with both systems will be able to answer your question. Alternatively, you could always use NMinimize etc to explicitly minimize a sum of squares. – MarcoB May 23 '18 at 17:37
• In priciple, that's a task for NonlinearModelFit. I also recall to have read <s>somewhere</s> here that FindMinimum switches automatically to the Levenberg-Marquardt mathod (a minor modification of Gauss-Newton method) if it detects that the objective function is a sum of squares... So give it a try with either method. – Henrik Schumacher May 23 '18 at 20:21
• Please post the code text so we can test them easily. – xzczd May 25 '18 at 5:00

According to the document of LSSolve:

The LSSolve command solves a least-squares (LS) problem, which involves computing the minimum of a real-valued objective function having the form $$\frac{1}{2}(f_1(x)^2+f_2(x^2)+…+f_q(x)^2)$$ where $x$ is a vector of problem.

So its analog in Mathematica seems to be FindMinimum with a specific function to be minimized. We should be able to obtain (almost) the same result with:

Clear@lSSolve
lSSolve[obj_List, constr___, x_, opt : OptionsPattern[FindMinimum]] :=
FindMinimum[{1/2 obj^2 // Total, constr}, x, opt]
lSSolve[obj_, rest__] := lSSolve[{obj}, rest]


Let's test with the examples given in the document and your question:

# Case 1

Maple:

LSSolve([x-2, x-6, x-9]);
(*     [12.3333333333333321, [x = 5.66666666666667]]*)


Mathematica:

lSSolve[{x - 2, x - 6, x - 9}, x]
(* {12.3333, {x -> 5.66667}} *)


# Case 2

Maple:

LSSolve([x^3-2, x^2-6, x^2-9], initialpoint = {x = 1});
(*           [27.5839512531713, [x = 1.75156454919679]]*)


Mathematica:

lSSolve[{x^3 - 2, x^2 - 6, x^2 - 9}, {x, 1}]
(* {27.584, {x -> 1.75156}} *)


# Case 3

Maple:

LSSolve([x-1, y-1, z-1], {x <= 0, 6*x+3*y <= 1}, initialpoint = {x = -1, y = 1});
(*  [0.711111111111111138, [x = -0.0666666666666667, y = 0.466666666666667, z = 1.]]*)


Mathematica:

lSSolve[{x - 1, y - 1, z - 1}, {x <= 0, 6 x + 3 y <= 1}, {{x, -1}, {y, 1}, z}]
(* {0.711111, {x -> -0.0666676, y -> 0.466668, z -> 1.}} *)


# Case 4

Maple:

LSSolve([x-1], {(x+1)^2 <= 0});
(*        [1.99998465585440166, [x = -0.999992327912486]]*)


Mathematica:

lSSolve[x - 1, (x + 1)^2 <= 0, x]
(* {2., {x -> -1.}} *)


# Case 5: Example in your question

eq1 = -0.3007024038 c^2 + (-(0.4990858944/10^2) - 0.3007024038 c) c;
eq2 = -0.2004682692 c^2 + (-(0.2495429472/10^2) - 0.1503512019 c) c;
eq3 = -0.1503512019 c^2 + (-(0.1663619648/10^2) - 0.1002341346 c) c;
eq4 = c + c + c - 1;
lSSolve[{eq1, eq2, eq3, eq4}, c /@ Range[0, 2]]
(* {1.58921*10^-33, {c -> 1.0166, c -> -0.0165973, c -> 7.45058*10^-9}} *)


Slightly different, but according to objective function value, result of Mathematica is better.

• Thank you very much. I guess this is what I was looking for. – naveed May 27 '18 at 2:30
• @naveed There's a bug in my previous implementation, now it's fixed and result of case 4 is also (almost) the same as that of Maple. – xzczd May 29 '18 at 7:39

Is this not what you want ?

In:= NMinimize[eq1^2 + eq2^2 + eq3^2 + eq4^2, {c, c, c}]

Out= {1.94723*10^-27, {c -> 1.01633, c -> -0.0163307,
c -> 4.71816*10^-10}}

• Thank you. A vote up for you. – naveed May 27 '18 at 2:31
• I have got better results (In the implementation of my complete code) by combining the both techniques. I used Lotus's Nminimize with xzczd definition of LSSolve and got the solutions that agree very well with the exact solutions of actual solutions. I was confused in selecting the answer but then i thought xzczd answer in more general. I am thankful to all of you for helping me out. – naveed May 30 '18 at 17:53