Message Boards Message Boards

GROUPS:

Using Findfit

Posted 6 years ago
15814 Views
|
8 Replies
|
0 Total Likes
|

I am trying to find a,b,c from this equation using an array of x and y,

enter image description here

This is my input in mathematica,

FindFit[{{171.29, 6}, {171.63, 12.1}, {171.86, 24.2}, {172.06, 
       48.3}}, {x == (((sqrt (a^2 + 8 a y) - a) (b - c))/(4 y)) + c}, {a, 
      b, c}, {x, y}]

But I get this message, FindFit::fitc: Number of coordinates (1) is not equal to the number of variables (2). >>

Please advice how to solve for a,b,c

8 Replies
Posted 6 years ago

Look very carefully for each of the very tiny changes made to what you wrote.

In[1]:= sol = FindFit[{{171.29, 6}, {171.63, 12.1}, {171.86, 24.2}, {172.06, 48.3}},
   (((Sqrt [a^2 + 8 a y] - a) (b - c))/(4 y)) + c, {a, b, c}, y, MaxIterations -> 10^4]

Out[1]= {a -> 324223., b -> -8925.88, c -> 8.45726*10^6}

In[2]:= Show[Plot[(((Sqrt [a^2 + 8 a y] - a) (b - c))/(4 y))+c/.sol, {y, 171.29, 172.06}, PlotRange->{0,50}],
   ListPlot[{{171.29, 6}, {171.63, 12.1}, {171.86, 24.2}, {172.06, 48.3}}]]

enter image description here

In[3]:= sol = FindFit[{{171.29, 6}, {171.63, 12.1}, {171.86, 24.2}, {172.06, 48.3}},
   a y^2 + b y + c, {a, b, c}, y, MaxIterations -> 10^4]

Out[3]= {a -> 94.8838, b -> -32524.8, c -> 2.78726*10^6}

In[4]:= Show[Plot[a y^2 + b y + c /. sol, {y, 171.29, 172.06}, PlotRange -> {0, 50}],
   ListPlot[{{171.29, 6}, {171.63, 12.1}, {171.86, 24.2}, {172.06, 48.3}}]]

enter image description here

Hello Bill (and Damodaran),

your last fit-result is quite impressive, but for me it seems to be the case that the parameters a, b, c of the function a y^2 + b y + c are quite different from those of the original function. Therefore, even giving a fit to the data, they are not what was looked for.

The original function was

f0 = (((Sqrt [a^2 + 8 a y] - a) (b - c))/(4 y)) + c

Now the square root often is a source of difficulties and so I prefer to "switch" variables

 In[3]:= f1 = y /. Solve[x == f0, y][[1, 1]]

Out[3]= (a (b^2 - b c - b x + c x))/(2 (c - x)^2)

Here we have the original data, in which I reverse the variables as well

data = {{171.29, 6}, {171.63, 12.1}, {171.86, 24.2}, {172.06, 48.3}};

In[5]:= datax = Reverse /@ data

Out[5]= {{6, 171.29}, {12.1, 171.63}, {24.2, 171.86}, {48.3, 172.06}}

Now I construct a set of (almost polynomial) equations (without any roots!)

In[6]:= eqs = #[[1]] == f1 /. x -> #[[2]] & /@ data

Out[6]= {171.29 == (a (-6 b + b^2 + 6 c - b c))/(2 (-6 + c)^2), 
 171.63 == (a (-12.1 b + b^2 + 12.1 c - b c))/(2 (-12.1 + c)^2), 
 171.86 == (a (-24.2 b + b^2 + 24.2 c - b c))/(2 (-24.2 + c)^2), 
 172.06 == (a (-48.3 b + b^2 + 48.3 c - b c))/(2 (-48.3 + c)^2)}

Here I have 4 equations for 3 unknowns. To get start-Parameters for FindFit I drop in turn one of the equations and solve for a, b, c. This indeed gives 8 solutions:

In[20]:= TT = Table[Solve[Drop[eqs, {j, j}], {a, b, c}], {j, 1, 4}]

Out[20]= {{{a -> -1376.5, b -> -363.753, 
   c -> -779.312}, {a -> -1376.48, b -> 398.136, 
   c -> 747.409}}, {{a -> -1376.63, b -> 298.276, 
   c -> 553.491}, {a -> -1376.52, b -> -263.594, 
   c -> -571.99}}, {{a -> -1378.17, b -> 219.027, 
   c -> 402.152}, {a -> -1377.44, b -> -188.469, 
   c -> -413.064}}, {{a -> -1374.94, b -> 155.522, 
   c -> 288.652}, {a -> -1374.89, b -> -136.055, c -> -295.49}}}

Note that the a's are quite similar (and negative! I did not look in the case what happens if we take the negative square-root in the original function), while the b's and c's vary considerably and even change sign.

Now I prepare these results for FindFit

In[9]:= candidates = Flatten[TT, 1] /. Rule -> List

Out[9]= {{{a, -1376.5}, {b, -363.753}, {c, -779.312}}, {{a, \
-1376.48}, {b, 398.136}, {c, 747.409}}, {{a, -1376.63}, {b, 
   298.276}, {c, 
   553.491}}, {{a, -1376.52}, {b, -263.594}, {c, -571.99}}, {{a, \
-1378.17}, {b, 219.027}, {c, 
   402.152}}, {{a, -1377.44}, {b, -188.469}, {c, -413.064}}, {{a, \
-1374.94}, {b, 155.522}, {c, 
   288.652}}, {{a, -1374.89}, {b, -136.055}, {c, -295.49}}}

and then I should look in turn to FindFit for all candidates. Here for example number 3

 In[24]:= sol2 = 
 FindFit[datax, f1, candidates[[3]], x, MaxIterations -> 1000]

Out[24]= {a -> -1376.7, b -> 292.629, c -> 543.659}

which gives the following results for the modified Problem and for the original function.

In[25]:= Plot[f1 /. sol2, {x, 0, 50}, Epilog -> {Red, Point /@ datax}]

Plot[f0 /. sol2, {y, 171.25, 172.25},  Epilog -> {Red, PointSize[.015], Point /@ data},  PlotRange -> {5, 50}]  

enter image description here

enter image description here

Of course we should have a look at all candidates

FindFit[datax, f1, #, x, MaxIterations -> 1000] & /@ candidates

and get

{{a -> -1376.56, b -> -253.241, c -> -548.992}, {a -> -1376.7, 
  b -> 292.629, c -> 543.659}, {a -> -1376.7, b -> 292.629, 
  c -> 543.659}, {a -> -1376.56, b -> -253.241, 
  c -> -548.992}, {a -> -1376.7, b -> 292.629, 
  c -> 543.659}, {a -> -1376.56, b -> -253.241, 
  c -> -548.992}, {a -> -1376.7, b -> 292.629, 
  c -> 543.659}, {a -> -1376.56, b -> -253.241, c -> -548.992}}

and we see a is always negative in the order 1376, b has basically 2 values - 253 and 292 and that is the case with c too: - 548 and 543.

It remains to look for (physical ?) reasons which parameters are appropriate or which will give rise to perhaps negative expressions in the square-root.

Kind regards Hans

Attachments:

Thanks so much Bill. Is there a way to include limits to a, b or c ? Thanks.

Posted 6 years ago

If you read the documentation for FindFit and you click on the triangle next to Scope and you then click on the triangle next to Constraints and Starting Values then you will see an example showing how to place limits on the parameters you are finding.

@Damodaran

Could you give a hint where your equation to be fitted comes from?

HD

Hans, This equation is used for determining self-association of molecules in solution. Basically I get Kd (Dissociation constant). Let me know if you need more info. Damodaran

Hello Bill, I am using Mathematica 11.0 Student Edition and when I copied your suggested solution, it is giving error for the variables given.

What could be causing this problem? This is the exact same input code as given. enter image description here

Posted 2 years ago

Look carefully at the syntax coloring, a, b, and c are blue, y is black. This means that y has an existing definition. With your cursor in that cell, select the menu item Help / Why the Coloring?. Clear the definition of y by evaluating ClearAll[y].

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract