-1
$\begingroup$

If I have the following data (x value is composition and y value is temperature):

data = {{0, 54.61`}, {100, 57.26243979492134`}, {80,53.839874154239816`}, {50, 54.09456572258326`}, {24, 56.15393883162748`}}

Which plotted like this gives:

ListPlot[List /@ data, Frame -> True, FrameStyle -> 16, Axes -> False,
  GridLines -> Automatic, GridLinesStyle -> Lighter[Gray, .8], 
 FrameTicks -> {Automatic, Automatic}, 
 FrameLabel -> (Style[#, 20, Bold] & /@ {"x", Row[{"T", ""}]}), 
 ImageSize -> Large, LabelStyle -> {Black, Bold, 14}, 
 PlotStyle -> {Red}, PlotRange -> {{-1, 101}, {52, 60}}]

enter image description here

Questions:

  1. How can I fit the data to the following equation?:

EDIT: I just put updated the equation to have two fitting parameters an a "-273.15" in the equation to make it more consistent to fit the data

eq = (x*(57.26 + 273.15) + k*(100 - x)*(54.61 + 273.15))/(x + k*(100 - x)) + q*(x*(100 - x)) - 273.15;

where q and k are fitting parameters and x is the x values of data.

  1. How can I find the best q and K that fits the data?

I am trying the following:

nlm = NonlinearModelFit[data, eq, {q, k}, x]
nlm["BestFitParameters"] (*{q -> 0.0000243353, k -> 6.10434*10^13}*)

Show[Plot[nlm[x], {x, 0, 100}], ListPlot[data], PlotRange -> All]

But it does not fit the data very well. I think is because perhaps I am not forcing the model to take better values of q and k.

$\endgroup$
3
  • 1
    $\begingroup$ Having in mind all that @Daniel Huber has written have a look into Menu/Help/WolframDocumentation/FindFit. $\endgroup$ – Alexei Boulbitch Apr 11 at 17:20
  • 1
    $\begingroup$ Try nlm = NonlinearModelFit[data, eq, q, x]; Show[Plot[nlm[x], {x, 0, 100}], ListPlot[data]] and you'll get an explicit picture why this linear function in q (but quadratic in x) gives an improbable fit. $\endgroup$ – JimB Apr 11 at 17:34
  • $\begingroup$ @Thank JimB. I just updated the equation to have two fitting parameters q and K which will perhaps make the fit better $\endgroup$ – John Apr 11 at 19:52
2
$\begingroup$

I think that the functions you're trying to fit are inappropriate for the data (or at best no theoretical justification for the models is presented) and you only have 5 data points. (There's an old saying about restaurant review: The food was bad and the portions too small.)

But you are right in that you need better starting values to get the "best" (although inappropriate) fit.

data = {{0, 54.61`}, {100, 57.26243979492134`}, {80, 53.839874154239816`}, 
  {50, 54.09456572258326`}, {24, 56.15393883162748`}};

eq = (x*(57.26 + 273.15) + k*(100 - x)*(54.61 + 273.15))/(x + k*(100 - x)) + q*(x*(100 - x)) - 273.15;

nlm = NonlinearModelFit[data, eq, {{q, -0.002}, {k, -0.1}}, x]
nlm["BestFitParameters"]
(* {q -> -0.00166586, k -> -0.13542} *)

Show[Plot[nlm[x], {x, 0, 100}], ListPlot[data]]

data and fit

So one might think that this is just maximum likelihood going wacko but it is not. Suppose one constructs the sum of squares and attempts to find the values of q and k to minimize it:

sumOfSquares = Total[(data[[All, 2]] - (eq /. x -> data[[All, 1]]))^2]

Sum of squares

Minimize[sumOfSquares, {q, k}]
(* {1.06294, {q -> -0.00166586, k -> -0.13542}} *)

This matches perfectly with the maximum likelihood estimates from NonlinearModelFit.

I don't know your subject matter but I find it hard to believe that there would be a theoretical justification for the model form that you present. Maybe using a simple linear regression with a slope and intercept might be a better summary of the 5 sample points.

Here is a picture from a distance:

Show[Plot[nlm[x], {x, 0, 100}, PlotRange -> {All, {0, 100}}], ListPlot[data]]

Data and fit from a distance

$\endgroup$
3
$\begingroup$

Consider your data:

data = Sort@{{0, 54.61`}, {100, 57.26243979492134`}, {80, 
    53.839874154239816`}, {50, 54.09456572258326`}, {24, 
    56.15393883162748`}};
ListLinePlot[data]

enter image description here

Now consider your function:

f = (x*(57.26 + 273.15) + (100 - x)*(54.61 + 273.15))/(x + (100 - 
        x)) + q*(x*(100 - x)) // Simplify

enter image description here

This is a quadratic function, but your data looks like anything but quadratic. Further, your function starts at {0,327.76} for any q, far away from your data.

For short, it does not make much sense to fit this function to the given data. Maybe you should explain where your data and function come from.

Addendum:

Well, the constant term of 327.76 is certainly way off. You could fit the constant term. Then your data look more like a cubic than a quadratic. Here is a reasonable fit with a cubic:

fit = Fit[data, {1, x, x^ 2, x^3}, x]
Plot[fit, {x, 0, 100}, Epilog -> Point[data]]

enter image description here

$\endgroup$
3
  • $\begingroup$ Hi Daniel! Thank you very much. You are right. The fit doesn't fit the data very well. My intention is simply to find a way to get the best possible fit and q that is obtainable with that equation. Is that something possible? $\endgroup$ – John Apr 11 at 19:12
  • 1
    $\begingroup$ Added an addendum to my answer $\endgroup$ – Daniel Huber Apr 11 at 19:49
  • $\begingroup$ Daniel thank you! I also updated the equation with two fitting parameters. I think with two fitting parameters it will fit the data better by using the equation and with no need for trying to make it cubic. $\endgroup$ – John Apr 11 at 19:51

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.