6
$\begingroup$

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]^2 + (-0.4990858944 10^-2 - .3007024038*c[1])*
c[2];
eq2 = -.2004682692*c[2]^2 + (-0.2495429472 10^-2 - .1503512019*c[1])*
c[2];
eq3 = -.1503512019*c[2]^2 + (-0.1663619648 10^-2 - .1002341346*c[1])*
c[2];
eq4 = c[0] + c[1] + c[2] - 1;

PS Here is a snap of the calculations on MAPLE enter image description here

$\endgroup$
  • 7
    $\begingroup$ Perhaps LeastSquares is what you are looking for in MMA. $\endgroup$ – MarcoB May 23 '18 at 16:11
  • $\begingroup$ 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. $\endgroup$ – naveed May 23 '18 at 16:20
  • 5
    $\begingroup$ 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. $\endgroup$ – MarcoB May 23 '18 at 17:37
  • 2
    $\begingroup$ 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. $\endgroup$ – Henrik Schumacher May 23 '18 at 20:21
  • 1
    $\begingroup$ Please post the code text so we can test them easily. $\endgroup$ – xzczd May 25 '18 at 5:00
11
$\begingroup$

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

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

$\endgroup$
  • $\begingroup$ Thank you very much. I guess this is what I was looking for. $\endgroup$ – naveed May 27 '18 at 2:30
  • $\begingroup$ @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. $\endgroup$ – xzczd May 29 '18 at 7:39
5
$\begingroup$

Is this not what you want ?

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

Out[6]= {1.94723*10^-27, {c[0] -> 1.01633, c[1] -> -0.0163307, 
  c[2] -> 4.71816*10^-10}}
$\endgroup$
  • $\begingroup$ Thank you. A vote up for you. $\endgroup$ – naveed May 27 '18 at 2:31
  • $\begingroup$ 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. $\endgroup$ – naveed May 30 '18 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.