0
$\begingroup$

I have data

data = {{5, 580}, {10, 570}, {15, 565}, {20, 562}, {25, 560}};

I have fitting function

A[x_] := a*Sqrt[1 - x/b]

I expect value a = 600, b = 300. Using

 FindFit[data, A[x], {a, b}, x]

I instead error message:

"indFit::nrlnum: "The function value {-580.`+2.` I,-570.`+3.` I,-565.`+3.7416573867739413` I,-562.`+4.358898943540674` I,-560.`+4.898979485566356` I} is not a list of real numbers with dimensions {5} at {a,b} = {1.`,1.`}""

How to use FindIt to get a and b?

$\endgroup$
2
  • 1
    $\begingroup$ FindFit[data,A[x],{{a,600},{b,300}},x] will specify starting points. Just FYI though, I'd probably define A as fitfun[x_,a_,b_]:=... and do FindFit[data, fitfun[x,a,b],{{a,600},{b,300}},x]. Better to avoid capital letters in your defs. to avoid clashing with MMA functions, and prevent your function from messing up if a,b end up with global defs. I'd also look into NonlinearModelFit $\endgroup$
    – N.J.Evans
    Commented Apr 3, 2017 at 15:09
  • 2
    $\begingroup$ The error occurs because FindFit tries values that make the Sqrt complex. Restricting b fixes that : FindFit[data, {A[x], b > 25}, {a, b}, x] $\endgroup$
    – george2079
    Commented Apr 3, 2017 at 15:11

1 Answer 1

1
$\begingroup$

Transforming the data can be helpful, e.g.

dt = {#1, Log@#2} & @@@ data
nlm = NonlinearModelFit[dt, c + Log[b - x]/2, {{c, 10}, {b, 30}}, x]
dtf = {#1, #2^2} & @@@ data
{a0, b0} = {Exp[c] Sqrt[b], b} /. nlm["BestFitParameters"]
lm = LinearModelFit[dtf, {1, x}, x]
{a1, b1} = {Sqrt@#1, -#1/#2} & @@ lm["BestFitParameters"]
Legended[Show[
  Plot[{Exp[nlm[x]], Sqrt[lm[x]]}, {x, 5, 25}, 
   PlotStyle -> {Blue, {Red, Dashed}}], ListPlot[data], 
  PlotRange -> {540, 600}], 
 Placed[LineLegend[{Blue, {Red, Dashed}}, {"Log transform", 
    "Squaring"}], {0.5, 0.8}]]
TableForm[{{a0, b0}, {a1, b1}}, 
 TableHeadings -> {{"Log Transform", "Squaring"}, {"a", "b"}}]

enter image description here

$\endgroup$

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.