3
$\begingroup$

I am almost new in Mathematica, and so I have some problems that may appear basic for you. I am given a set of data taken from an experiment. I am supposed to find the curve that best fits them. The data are:

data = {{0.12, 1.51}, {0.12, 1.50}, {0.46, 1.51}, {0.51, 1.52}, {0.79,
     1.52}, {1.07, 1.55}, {1.41, 1.53}, {1.46, 1.51}, {1.73, 
    1.51}, {1.74, 1.50}, {2.02, 1.49}, {2.02, 1.47}, {2.37, 
    1.41}, {2.42, 1.40}, {2.70, 1.34}, {2.97, 1.28}, {2.97, 
    1.29}, {2.98, 1.28}, {2.99, 1.30}, {3.00, 1.28}, {3.31, 
    1.21}, {3.61, 1.17}, {3.62, 1.17}, {3.65, 1.18}, {3.78, 
    1.15}, {3.79, 1.16}, {3.93, 1.15}, {3.94, 1.17}, {3.94, 
    1.17}, {4.18, 1.19}, {4.18, 1.20}, {4.34, 1.29}, {4.48, 
    1.39}, {4.48, 1.38}, {4.60, 1.56}};

Originally, I was looking for a function of the form

$$a - bx - \frac{c - (dx - e)^2}{f - (g x - k)^3}$$

where $a,b,c,d,e,f,g,k$ are constants to be determined, but if you have better ideas for the function form, please inform me. The code I used is:

nlm = NonlinearModelFit[
  data, (a - bx - (c - (dx - e)^2)/(f - (g x - k)^3)), {a, b, c, d, e,
    f, g, k}, x]
Show[Plot[nlm[x], {x , 0, 6}]
 , ListPlot[data, PlotStyle -> {Darker@Green, PointSize[0.03]}]]

But I get a number of errors, like "First argument data in NonlinearModelFit is not a list or a rectangular arrow" I am searching for such a function desperately for some time now, I would deeply appreciate any help I could get....

$\endgroup$
4
  • 1
    $\begingroup$ bx is a symbol, b x or b*x is b times x, fix that (with dx ) and you're on your way... $\endgroup$
    – ciao
    Commented Jun 10, 2015 at 5:30
  • 1
    $\begingroup$ Ditto for d x versus dx. $\endgroup$
    – Verbeia
    Commented Jun 10, 2015 at 5:34
  • 1
    $\begingroup$ Is there a reason you've chosen that particular function? Is it known that the data in this experiment should have such a mathematical form? I ask because a 5th order polynomial fits the data pretty nicely, and with 2 fewer parameters. Of course, any relatively nice looking data set will fit some polynomial... $\endgroup$
    – march
    Commented Jun 10, 2015 at 5:35
  • $\begingroup$ Yes indeed, I am told by my professor that this mathematical formula could be used, but I am not restricted to use this formula only.... $\endgroup$
    – Arbër Ll
    Commented Jun 10, 2015 at 11:45

1 Answer 1

6
$\begingroup$
nlm = NonlinearModelFit[ data, (a - b x - (c - (d x - e)^2)/(f - (g  x - k)^3)), 
                       {a, b, c, d, e, f, g, k}, x, Method -> NMinimize]
Show[Plot[nlm[x], {x, 0, 6}], 
     ListPlot[data, PlotStyle -> {Darker@Green, PointSize[0.03]}]]

Mathematica graphics

$\endgroup$
4
  • $\begingroup$ The graph you found is excellent, but I am not able to generate it in my computer. I get the error: "Value of Method -> NMinimize is not automatic..." , "General::ivar: 0.00012257142857142857 is not a valid variable...." (the latter I got with four different numbers), and General::stop: "Further output of General will be suppressed during this calculation." Is there a problem with my version of Mathematica (7)? $\endgroup$
    – Arbër Ll
    Commented Jun 10, 2015 at 12:30
  • $\begingroup$ @ArbërLl Version 7 is rather old,and I don't have it around anymore. I don't think it supports Method -> NMinimize The other error is possibly solved by restarting Mathematica $\endgroup$ Commented Jun 10, 2015 at 12:38
  • $\begingroup$ First of all, thank you very much for your patience, you already did a great job for me. There is one more question, though. I just got Mathematica 9, and I managed to get the curve you showed, but what about the form of the model, what does this symbol (<<1>>)^2 and (<<1>>)^3 that appears in it mean? ! $\endgroup$
    – Arbër Ll
    Commented Jun 10, 2015 at 22:59
  • $\begingroup$ @ArbërLl Execute nlm["BestFitParameters"]. You'll never be able to program a single line of Mathematica code if you don't read the docs. Try reading the NonlinearModelFit[ ] help page. (BTW the docs aren't friendly at first. Keep trying) $\endgroup$ Commented Jun 11, 2015 at 3:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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