1
$\begingroup$

I was looking for a function for numerical optimization(in nonlinear least square sense) in mathematica and have some simple questions.

Here is the thing: At the beginning, I found the MATLAB function called 'lsqcurvefit' and this seemed to be the one I was looking for. Here is the link: http://www.mathworks.com/help/optim/ug/lsqcurvefit.html

After that, I found the same(?)(Hopefully) function in the Mathematica. This is the example code in the Wolfram (I tried to put the link but I couldn't because I didn't have enough reputation points to put more than 2 links, I'm sorry):

    In[1]:= NonlinearRegress[{{2, -0.99}, {6, -0.7}, {10, 0.58}, {14, 
    0.41}, {18, -0.66}}, Sin[a + b*x^2], {a, b}, x]
    Out[1]= BestFitParameters -> {a -> 0.776944, b -> 1.00375}

But my situation is that, I have more than one variable in the equation. I hope this link might be the one in this case: http://reference.wolfram.com/language/NonlinearRegression/tutorial/NonlinearRegression.html

In[1]:= data = {{1.0, 1.0, .126}, {2.0, 1.0, .219}, {1.0, 2.0, .076}, {2.0, 
2.0, .126}, {.1, .0, .186}};
In[2]:= NonlinearRegress[data, (theta1 theta3 x1)/(
1 + theta1 x1 + theta2 x2), {theta1, theta2, theta3}, {x1, x2}]
Out[2]= BestFitParameters -> {theta1 -> 3.13151, theta2 -> 15.1594, 
theta3 -> 0.780063}

Now I have two questions.

1.In the last link where the case that has the equation with three parameters to solve and two variables in the equation (when comparing with the first MATLAB link), does the first vector of the input data (1.0,2.0,1.0,2.0,.1) correspond to the x1? and the second (1.0,1.0,2.0,2.0,.0) and the third vector (.126,.219,.076,.126,.186) indicates x2 and ydata, respectively?

2.To make this sure, I tried to run the same thing in the first link using the Mathematica, but kept failing to solve. Here is my code:

    In[1]:= data1 = {{0.9, 455.2}, { 1.5, 428.6}, { 13.8, 124.1}, { 19.8, 
67.3}, { 24.1, 43.2}, { 28.2, 28.1}, { 35.2, 13.1}, { 
60.3, -0.4}, { 74.6, -1.3}, { 81.3, -1.5}};
   In[2]:= NonlinearRegress[data1, 
theta1*e^((theta2)*(x1)), {theta1, theta2}, {x1}]

    FindFit::nrlnum: The function value {-455.2+1. e^0.9,-428.6+1. e^1.5,-124.1+1. e^13.8,-67.3+1. e^19.8,-43.2+1. e^24.1,-28.1+1. e^28.2,-13.1+1. e^35.2,0.4 +1. e^60.3,1.3 +1. e^74.6,1.5 +1. e^81.3} is not a list of real numbers with dimensions {10} at {theta1,theta2} = {1.,1.}.
    NonlinearRegress::fitfail: The fitting algorithm failed.

Can anyone help me out what I did wrong or what I understood completely wrong?

Appreciated.

$\endgroup$
  • 3
    $\begingroup$ What version of Mathematica are you using? From the documentation in version 10.4.1 one finds the following about NonlinearRegress: As of Version 7.0, NonlinearRegress has been superseded by NonlinearModelFit and is part of the built-in Wolfram Language kernel. $\endgroup$ – JimB Dec 17 '16 at 5:30
  • $\begingroup$ After changing e to E and including Needs["NonlinearRegression"]` , your last bit of code runs on MMA 11 without error. To avoid that mistake, I usually use the Exp function instead. $\endgroup$ – LouisB Dec 17 '16 at 10:55
4
$\begingroup$

The answer to your first question is Yes. If you have a version greater or equal to 7.0, then the following should work and address your second question:

data = {{1.0, 1.0, .126}, {2.0, 1.0, .219}, {1.0, 2.0, .076}, {2.0, 2.0, .126}, {.1, .0, .186}};
nlm = NonlinearModelFit[data, (theta1 theta3 x1)/(1 + theta1 x1 + theta2 x2),
   {theta1, theta2, theta3}, {x1, x2}];
nlm["BestFitParameters"]
(* {theta1 -> 3.131505242020007,theta2 -> 15.159362112360794,theta3 -> 0.7800626110719642} *)

But note that the example is just something short to get things to work. No one in their right mind would/should attempt to estimate 4 parameters (theta1, theta2, theta3, and the residual error variance) with just 5 sample points.

$\endgroup$
  • 2
    $\begingroup$ For me, it is a little depressing that people have to be told that last sentence. (+1, of course.) $\endgroup$ – J. M. will be back soon Dec 17 '16 at 6:50

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.