# Fitting data to equation with one fitting parameter

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}}] 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.

• Having in mind all that @Daniel Huber has written have a look into Menu/Help/WolframDocumentation/FindFit. – Alexei Boulbitch Apr 11 at 17:20
• 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. – JimB Apr 11 at 17:34
• @Thank JimB. I just updated the equation to have two fitting parameters q and K which will perhaps make the fit better – John Apr 11 at 19:52

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]] 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] 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 = Sort@{{0, 54.61}, {100, 57.26243979492134}, {80,
53.839874154239816}, {50, 54.09456572258326}, {24,
56.15393883162748}};
ListLinePlot[data] f = (x*(57.26 + 273.15) + (100 - x)*(54.61 + 273.15))/(x + (100 -
x)) + q*(x*(100 - x)) // Simplify 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.

fit = Fit[data, {1, x, x^ 2, x^3}, x] • 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? – John Apr 11 at 19:12