2
$\begingroup$

I am trying to fit some data points to a transformed natural log function and transformed 1/x function. However, the fitted 1/x function fails to adhere nicely to the data points when x is growing larger. Below is my code:

pHBarListChurakov = {10.396067415730338`, 10.497191011235955`, 
   10.547752808988765`, 10.598314606741573`, 10.69943820224719`, 
   10.851123595505618`, 10.901685393258427`, 11.398876404494382`, 
   11.601123595505618`, 11.803370786516854`, 11.997191011235955`, 
   12.19943820224719`, 12.25`, 12.401685393258427`, 
   12.603932584269662`};
rhoOHBarList = 10^(pHBarListChurakov - 11);
calculatedUList = {-0.9000016984445702`, -0.5617336372971362`, \
-0.9009333610974087`, -0.40213804143299425`, -0.05513091822567246`, 
   0.5116416122750036`, 0.2772878238568564`, 2.0185896240329964`, 
   2.721886178225983`, 3.4101441103082335`, 4.350952842429438`, 
   5.258892468165652`, 5.84833335802726`, 6.659730475621987`, 
   6.6045283567870126`};
fitModel = k1/(x(*-k2*)) - k3;
fitCoeff = 
 FindFit[Transpose[{rhoOHBarList, calculatedUList}], {fitModel}, {k1(*,
   k2*), k3}, x]
fittedUFunc[x_] = fitModel /. fitCoeff;
fitModel2 = k1*Log[k3*x] + k2;
fitCoeff2 = 
 FindFit[Transpose[{rhoOHBarList, calculatedUList}], {fitModel2, 
   k3 > 0}, {k1, k2, k3}, x]
fittedUFunc2[x_] = fitModel2 /. fitCoeff2;
oriPlot = 
  ListPlot[Transpose[{rhoOHBarList, calculatedUList}], 
   PlotLabel -> "var vs Bulk OH Conc (mMol/L)"];
fittedPlot = 
  Plot[fittedUFunc[x], {x, 0, 40}, PlotRange -> All, 
   PlotLegends -> {"1/x"}];
fittedPlot2 = 
  Plot[fittedUFunc2[x], {x, 0, 40}, PlotRange -> All, 
   PlotLegends -> {"ln(x)"}, PlotStyle -> Orange];
Show[fittedPlot, fittedPlot2, oriPlot, 
 PlotLabel -> "Calculated & Fitted u vs Bulk OH conc", 
 AxesOrigin -> {0, 0}, PlotRange -> {-20, 7}]

Here is the output: enter image description here I want the transformed 1/x function to satisfy 2 requirements as shown in the picture.

$\endgroup$

1 Answer 1

5
$\begingroup$

A model which fullfills both requirements is fit[x]=(k1 + k2 x + k3 x^2)/(x + k4 x^2)

Asymptotic[(k1 + k2 x + k3 x^2)/(x + k4 x^2), x -> 0] (* k1/x*)
Asymptotic[(k1 + k2 x + k3 x^2)/(x + k4 x^2), x -> Infinity](*k3/k4*)


fit = NonlinearModelFit[Transpose[{rhoOHBarList, calculatedUList}], (
  k1 + k2 x + k3 x^2)/(x + k4 x^2), {k1, k2 , k3, k4}, x, 
  Method -> "NMinimize" ]

    Show[{ListPlot[Transpose[{rhoOHBarList, calculatedUList}]], 
  Plot[fit[x], {x, 0, 40}, PlotRange -> {-10, 10}]}, 
 PlotRange -> {-10, 10}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.