MATLAB: How to find the coefficients for an equation having the experimental data

coefficients

I'm trying to write a code that uses observed data to generate the coefficients of a function.
Here's my data:
tc = [10,12,14,16,18,20,22,24,26,28,30,32,34,36,38]; % Temperature (Celsius)
tk = tc + 273.15; % Temperature (Kelvin)
pbar = [6.149,6.585,7.045,7.529,8.035,8.57,9.134,9.722,10.34,10.99,11.67,12.37,13.11,13.89,14.7]; % Pressure (bar)
pkpa = pbar * 100; % Pressure (kPa)
And here is my equation:
A = ln(pkpa) + B/tk;
How do I find A and B that best fit the data?

Best Answer

Rearrange as lnP = A - B/T --> -B/T + A which has form of mx+b for x=1/T, m=-B, b=A.
>> b=polyfit(1./tk,log(pkpa),1); % fit the form
>> yhat=polyval(b,1./tk); % evaluate in Matlab form
>> plot(tk,pkpa,'*-') % plot raw data
>> hold all
>> plot(tk,exp(yhat),'r-') % add fit
>> B=-b(1); % convert to you nomenclature
>> A=b(2);
>> polyval([-B A],1/tk(1))==yhat(1) % compare results to show same
ans =
1
>>
NB: Remember must take exp to convert to actual units of P since fitted ln(P)