# Least squares fit to find unknown coefficients

Stuck on thinking problem set and I'm looking for some help from you. What I need to find is coefficients ($A$, $B$ and $C$) in the following equation using least squares fit. ($\ln P=A + B/T + C\ln T$)

Log[P]==A+B/.T+C*Log[T]
T = {600, 650, 700, 750, 800, 850, 900, 950, 1000}
P = {0.124, 0.627, 2.492, 8.187, 23.06, 57.24, 127.9, 261.6, 496.5}
PLog={-2.08747, -0.466809, 0.913086, 2.10255, 3.1381, 4.04725, 4.85125, 5.56682, 6.20758}
TLog={6.39693, 6.47697, 6.55108, 6.62007, 6.68461, 6.74524, 6.80239, 6.85646, 6.90776}


I tried to fit this by $y=ax+b$ form but there is a $b/T$ term and I don't know how to deal with it. PLog and TLog is natural log value for $P$ and $T$ respectively.

How can I find $A$, $B$ and $C$ in this situation using least squares fit? Please give me some hints. Thank you so much!

• Were I in your shoes, I'd take a two-stage approach: use ubpdqn's method to generate initial parameter estimates, and then polish them with Marco's method. – J. M. will be back soon Feb 1 '17 at 8:00

You can use LinearModelFit on your transformed variable:

t = {600, 650, 700, 750, 800, 850, 900, 950, 1000};
p = {0.124, 0.627, 2.492, 8.187, 23.06, 57.24, 127.9, 261.6, 496.5};
d = {1/#1, Log@#1, Log@#2} & @@@ Transpose[{t, p}];
lm = LinearModelFit[d, {1, x, y}, {x, y}]
bf = lm["BestFitParameters"]
f[{a_, b_, c_}, x_] := Exp[{a, b, c}.{1, 1/x, Log[x]}]
Plot[f[bf, x], {x, 600, 1000},
Epilog -> {Red, PointSize[0.02], Point[Transpose[{t, p}]]},
PlotLabel -> f[bf, x], Frame -> True]


• Thank you so much!! it's big help!! – Sean Feb 1 '17 at 4:29
• @Sean I am glad it was helpful. My answer is essentially equivalent to MarcoB's as the problem can be converted from a non linear function in one variable to a linear function of two variables. Good luck :) – ubpdqn Feb 1 '17 at 4:31

Using the definitions of T and P you have, you are trying to fit $\log{P}$ to a non-linear function of $T$. You can transform your data and generate a set of (T,P) pairs using:

Transpose@{T, Log@P}

(* Out: {{600, -2.08747}, {650, -0.466809}, {700, 0.913086}, {750, 2.10255}, {800, 3.1381},
{850, 4.04725}, {900, 4.85125}, {950, 5.56682}, {1000, 6.20758}} *)


You can then use NonlinearModelFit to obtain the best-fit parameters:

data = Transpose@{T, Log@P};
nlm = NonlinearModelFit[data, a + b/t + c Log[t], {a, b, c}, t];
Plot[
nlm[t], {t, 600, 1000},
Epilog -> {PointSize[0.02], Point@data},
AxesLabel -> {"T", "Log[P]"}
]


The parameter values, together with error estimates and other statistical goodies, can be extracted from the FittedModel object nlm:

nlm["ParameterTable"]


• Thank you so much. You are so genius – Sean Feb 1 '17 at 4:24
• @Sean You are welcome – MarcoB Feb 1 '17 at 4:25
• Can I ask question? You used T and P pair as matrix and how do you know T will be input to t? and where is P in the nlm equation?? – Sean Feb 1 '17 at 4:32
• data is set T and LogP and does it automatically goes into t and Y-axis respectively? – Sean Feb 1 '17 at 4:35
• @Sean take a look at the documentation for NonlinearModelFit`: if you feed it a list of pairs of numbers as the data set, it will assume that the first is a value of the independent variable, and the second the corresponding value of the dependent variable, respectively. You then specify a model for the dependence, and you also specify which variable in that model should be considered independent. – MarcoB Feb 1 '17 at 4:36