4
$\begingroup$

I want to find a fit function for the following data. Can someone suggest the best-fit function for the following data? The upper part of the data set is difficult.

data = {{0.0270538, 0.92}, {0.0854374, 0.93}, {0.124226, 0.94}, {0.14931, 
  0.95}, {0.16527, 0.96}, {0.175575, 0.97}, {0.182765, 
  0.98}, {0.188613, 0.99}, {0.194274, 1.}, {0.200419, 
  1.01}, {0.207346, 1.02}, {0.215083, 1.03}, {0.223469, 
  1.04}, {0.232232, 1.05}, {0.241039, 1.06}, {0.249549, 
  1.07}, {0.257445, 1.08}, {0.264458, 1.09}, {0.270386, 
  1.1}, {0.275104, 1.11}, {0.278564, 1.12}, {0.280794, 
  1.13}, {0.281891, 1.14}, {0.282012, 1.15}, {0.281358, 
  1.16}, {0.280166, 1.17}, {0.27869, 1.18}, {0.27719, 
  1.19}, {0.275915, 1.2}, {0.275095, 1.21}, {0.274926, 
  1.22}, {0.275567, 1.23}, {0.277128, 1.24}, {0.279669, 
  1.25}, {0.283197, 1.26}, {0.287672, 1.27}, {0.293, 1.28}, {0.299049,
   1.29}, {0.305648, 1.3}, {0.3126, 1.31}, {0.319689, 
  1.32}, {0.326692, 1.33}, {0.333388, 1.34}, {0.339569, 
  1.35}, {0.345052, 1.36}, {0.349685, 1.37}, {0.353355, 
  1.38}, {0.355998, 1.39}, {0.357599, 1.4}, {0.358195, 
  1.41}, {0.357876, 1.42}, {0.356783, 1.43}, {0.3551, 
  1.44}, {0.353051, 1.45}, {0.350887, 1.46}, {0.348878, 
  1.47}, {0.347298, 1.48}, {0.346412, 1.49}, {0.346466, 
  1.5}, {0.347666, 1.51}, {0.350175, 1.52}, {0.35409, 
  1.53}, {0.359445, 1.54}, {0.366196, 1.55}, {0.374222, 
  1.56}, {0.383326, 1.57}, {0.39324, 1.58}, {0.403638, 
  1.59}, {0.414145, 1.6}, {0.424364, 1.61}, {0.43389, 
  1.62}, {0.442339, 1.63}, {0.449372, 1.64}, {0.454715, 
  1.65}, {0.458178, 1.66}, {0.459661, 1.67}, {0.459142, 
  1.68}, {0.456655, 1.69}, {0.452226, 1.7}, {0.44578, 
  1.71}, {0.436996, 1.72}, {0.425101, 1.73}, {0.408591, 
  1.74}, {0.384856, 1.75}, {0.349698, 1.76}};
$\endgroup$
5
  • 1
    $\begingroup$ Fitting a function (a model) to your data is far from trivial task. Firstly, you should probably reverse the $x$ and $y$ (invData = Reverse[data, 2]) so that you get a single-valued function. Secondly, you should explain what do you want to achieve by fitting a function. Do you want to interpolate data? Do you want to extrapolate data? Where does this data come from? Do you know of any (physical, economical ...) model that could describe the data? $\endgroup$
    – Domen
    Commented Apr 8, 2022 at 13:56
  • $\begingroup$ Analytical fit-function is needed, not interpolation or extrapolation. $\endgroup$
    – SciJewel
    Commented Apr 8, 2022 at 14:06
  • $\begingroup$ The main point of fitting data to a model is typically to extract the values of parameters in the model from experimental data. If you don't have a reasonable physical model for your data, and you do not seek to extract further information from it beyond just reproducing the data, then use an interpolation instead. $\endgroup$
    – MarcoB
    Commented Apr 8, 2022 at 14:12
  • $\begingroup$ @SciJewel Are u asking about what should be a good guess for a closed form function that may fit the data instead of interpolation? $\endgroup$
    – lineage
    Commented Apr 8, 2022 at 22:01
  • $\begingroup$ As others have noted, you probably want to predict data[[All,1]] from data[[All,2]] as you don't have a function with a single value for every value of data[[All,1]]. Mathematica's regression functions expect the predictor variables (called independent variables in ancient times) to come first and the response variable (called the dependent variable in those ancient times) to be last. In any event, you should be explicit about which variable is which. $\endgroup$
    – JimB
    Commented Apr 10, 2022 at 18:31

4 Answers 4

8
$\begingroup$

Your data shows a multivalued function. Therefore, you can not simply fit a single valued function. What you can do however, you may interpolate the x and y values separately and define a parametrized function. E.g.:

fun[t_] = {Interpolation[data[[All, 1]]][t], 
  Interpolation[data[[All, 2]]][t]}

enter image description here

With this you can do a parametrized plot:

ParametricPlot[fun[t], {t, 1, Length[data]}, Epilog -> Point[data]]

enter image description here

$\endgroup$
4
  • $\begingroup$ What do mean by multivalued data? This data simply describe a function of one variable. I know the interpolation method. The issue is that I want to know the fit-function and "interpolation" cannot give any information about the fit-function (analytical form) $\endgroup$
    – SciJewel
    Commented Apr 8, 2022 at 14:04
  • 6
    $\begingroup$ But for some x values there are several y values $\endgroup$ Commented Apr 8, 2022 at 14:06
  • $\begingroup$ Can you point out any such? As I all see all Y-values are unique. $\endgroup$
    – SciJewel
    Commented Apr 8, 2022 at 14:10
  • $\begingroup$ Look at x~0.35. This could have y around 1.6 or y around 1.7. A function only has one y for each x. $\endgroup$
    – bill s
    Commented Apr 8, 2022 at 14:13
6
$\begingroup$

Best, reverse data as @Domen proposes and do simple linear fit. Get good result for order of 11.

data = {{0.0270538, 0.92}, {0.0854374, 0.93}, {0.124226, 
    0.94}, {0.14931, 0.95}, {0.16527, 0.96}, {0.175575, 
    0.97}, {0.182765, 0.98}, {0.188613, 0.99}, {0.194274, 
    1.}, {0.200419, 1.01}, {0.207346, 1.02}, {0.215083, 
    1.03}, {0.223469, 1.04}, {0.232232, 1.05}, {0.241039, 
    1.06}, {0.249549, 1.07}, {0.257445, 1.08}, {0.264458, 
    1.09}, {0.270386, 1.1}, {0.275104, 1.11}, {0.278564, 
    1.12}, {0.280794, 1.13}, {0.281891, 1.14}, {0.282012, 
    1.15}, {0.281358, 1.16}, {0.280166, 1.17}, {0.27869, 
    1.18}, {0.27719, 1.19}, {0.275915, 1.2}, {0.275095, 
    1.21}, {0.274926, 1.22}, {0.275567, 1.23}, {0.277128, 
    1.24}, {0.279669, 1.25}, {0.283197, 1.26}, {0.287672, 
    1.27}, {0.293, 1.28}, {0.299049, 1.29}, {0.305648, 1.3}, {0.3126, 
    1.31}, {0.319689, 1.32}, {0.326692, 1.33}, {0.333388, 
    1.34}, {0.339569, 1.35}, {0.345052, 1.36}, {0.349685, 
    1.37}, {0.353355, 1.38}, {0.355998, 1.39}, {0.357599, 
    1.4}, {0.358195, 1.41}, {0.357876, 1.42}, {0.356783, 
    1.43}, {0.3551, 1.44}, {0.353051, 1.45}, {0.350887, 
    1.46}, {0.348878, 1.47}, {0.347298, 1.48}, {0.346412, 
    1.49}, {0.346466, 1.5}, {0.347666, 1.51}, {0.350175, 
    1.52}, {0.35409, 1.53}, {0.359445, 1.54}, {0.366196, 
    1.55}, {0.374222, 1.56}, {0.383326, 1.57}, {0.39324, 
    1.58}, {0.403638, 1.59}, {0.414145, 1.6}, {0.424364, 
    1.61}, {0.43389, 1.62}, {0.442339, 1.63}, {0.449372, 
    1.64}, {0.454715, 1.65}, {0.458178, 1.66}, {0.459661, 
    1.67}, {0.459142, 1.68}, {0.456655, 1.69}, {0.452226, 
    1.7}, {0.44578, 1.71}, {0.436996, 1.72}, {0.425101, 
    1.73}, {0.408591, 1.74}, {0.384856, 1.75}, {0.349698, 1.76}};

ListPlot[data2 = Reverse /@ data, PlotRange -> All]

{min = Min[data2[[All, 1]]], max = Max[data2[[All, 1]]]}

Manipulate[{Plot[
   Evaluate[
    f = LinearModelFit[data2, Table[x^j, {j, 1, jmax}], x] // 
      Normal], {x, min, max}, PlotRange -> {0, 1}, 
   Epilog -> {Red, Point@data2}, PlotStyle -> {Thick, Black}, 
   ImageSize -> 400], f}, {{jmax, 11}, 2, 15, 1}]

enter image description here

Get your plot with ParametricPlot or Inversefunction.

$\endgroup$
2
  • $\begingroup$ :Do you mean InverseFuction[f] ? $\endgroup$
    – SciJewel
    Commented Apr 8, 2022 at 17:10
  • $\begingroup$ Why "2, 15, 1"? $\endgroup$
    – SciJewel
    Commented Apr 10, 2022 at 16:50
6
$\begingroup$

Well, it's always possible to fit a polynomial of high degree if you are just looking to fit the data. Extrapolation would probably be out of the question though, but if you are just looking to find area under the curve or something like that, a polynomial would be sufficient.

polyMod = LinearModelFit[Reverse[data, 2], Table[x^i, {i, 1, 12}], x]
polyMod["BestFit"]

$ 19095.5 - 137063. x + 35576.4 x^2 + 2.26246*10^6 x^3 - 9.56685*10^6 x^4 + 2.05022*10^7 x^5 - 2.7615*10^7 x^6 + 2.50281*10^7 x^7 - 1.55778*10^7 x^8 + 6.59485*10^6 x^9 - 1.81992*10^6 x^{10} + 295831. x^{11} - 21518.7 x^{12} $

input=Transpose[data][[2]];
response=Transpose[data][[1]];
Show[ListPlot[Reverse[data, 2]],Plot[polyMod[x], {x, Min@input, Max@input}, PlotStyle -> Green]]

enter image description here

You could then switch the x and y axis data if you want it viewed in the perspective shown in the question.

Show[ListPlot[data], ListLinePlot[Transpose[{polyMod[#] & /@ input, input}], PlotStyle -> Green]]

enter image description here

You could always go crazy and fit any sort of strange function to try to get a good fit.

$ -13.8445 + 7.70684 ArcTan[ 4.27598 + 0.173275 Sqrt[ 0.479055 + X1 (2.85861 + X1 Tan[2. X1])]]^2 + (0.00824638 Sin[ 7.38906 (X1)^2])/(2./(X1) - 1. ArcTan[X1]) - 0.0287731 Tanh[1. Tanh[1. Sin[58.7434 - 1. Cos[(5.27447 + X1)^2]]]] $

Show[Plot[-13.8445+7.70684 ArcTan[4.27598+0.173275 Sqrt[0.479055+X1 (2.85861+X1 Tan[2. X1])]]^2+(0.00824638 Sin[7.38906 (X1)^2])/(2./(X1)-1. ArcTan[X1])-0.0287731 Tanh[1. Tanh[1. Sin[58.7434-1. Cos[(5.27447+X1)^2]]]],{X1,Min@input,Max@input},PlotStyle->Green],ListPlot[Reverse[data,2]]]

enter image description here

Show[ListLinePlot[Transpose[{-13.8445+7.70684 ArcTan[4.27598+0.173275 Sqrt[0.479055+X1 (2.85861+X1 Tan[2. X1])]]^2+(0.00824638 Sin[7.38906 (X1)^2])/(2./(X1)-1. ArcTan[X1])-0.0287731 Tanh[1. Tanh[1. Sin[58.7434-1. Cos[(5.27447+X1)^2]]]]/.(X1->input),input}],PlotStyle->Green],ListPlot[data]]

enter image description here

Without context regarding the potential use of the model it is difficult, if not impossible, to know what type of model is desired.

$\endgroup$
7
  • $\begingroup$ The trig function is irrelevant. The polynomial is fine, but what are your "inputs"? I mainly need the function in analytical form. How to get it from "Transpose[{polyMod[#] & /@ input, input}]"? $\endgroup$
    – SciJewel
    Commented Apr 8, 2022 at 17:58
  • $\begingroup$ The definition of input is now added to the post $\endgroup$
    – Nate
    Commented Apr 8, 2022 at 18:00
  • $\begingroup$ If I cannot the fit function in the analytical form at the end, then it's of no use. This method is the same as the "Interpolation" which is rather simple. $\endgroup$
    – SciJewel
    Commented Apr 8, 2022 at 18:40
  • 1
    $\begingroup$ Well if the second value is the independent variable then the analytics form for that polynomial would be the equation shown in the post. For your data is the first value or second value the independent variable? Alternatively, which one would you define as the input to the function you are looking for? $\endgroup$
    – Nate
    Commented Apr 8, 2022 at 18:44
  • 1
    $\begingroup$ @SciJewel Is it possible that there are more than one independent variables driving this system that are not reported in that data? To have values overlapping vertically would seem to imply that there is another variable driving this system other than just the one value shown in the data. $\endgroup$
    – Nate
    Commented Apr 8, 2022 at 19:45
3
$\begingroup$

Given that the plot of the data shows values that overlap vertically it would seem that there is likely another driving variable that would allow for this to occur. Without more data this could simply be treated as noise. If we treat those fluctuations as noise we obviously won't have a function that closely fits every point, but it might capture the general trends in the data. An example of this is shown here.

Show[ListPlot[data],Plot[0.436094+2.1746 (0.484306+1. Sqrt[2.71828^(1/((x)^2 (-3.14159+13.7321 (x)^3)))])^2,{x,0,0.5},PlotStyle->Green]]

enter image description here

If there is an additional variable that we don't have it could solve all the problems. For example say we create this new data set (the second variable is the new one):

altData={{0.0270538,0.135686,0.92},{0.0854374,0.0838094,0.93},{0.124226,0.0504443,0.94},{0.14931,0.0300261,0.95},{0.16527,0.0182274,0.96},{0.175575,0.0117711,0.97},{0.182765,0.00825762,0.98},{0.188613,0.00601191,0.99},{0.194274,0.00394287,1.},{0.200419,0.00141658,1.01},{0.207346,-0.00184847,1.02},{0.215083,-0.00587874,1.03},{0.223469,-0.0105221,1.04},{0.232232,-0.0155217,1.05},{0.241039,-0.0205628,1.06},{0.249549,-0.0253233,1.07},{0.257445,-0.0295038,1.08},{0.264458,-0.0328501,1.09},{0.270386,-0.0351714,1.1},{0.275104,-0.0363495,1.11},{0.278564,-0.0363393,1.12},{0.280794,-0.035167,1.13},{0.281891,-0.0329244,1.14},{0.282012,-0.0297597,1.15},{0.281358,-0.0258629,1.16},{0.280166,-0.0214578,1.17},{0.27869,-0.0167844,1.18},{0.27719,-0.0120884,1.19},{0.275915,-0.0076049,1.2},{0.275095,-0.00355126,1.21},{0.274926,-0.000112627,1.22},{0.275567,0.00256079,1.23},{0.277128,0.00436507,1.24},{0.279669,0.00524353,1.25},{0.283197,0.00518956,1.26},{0.287672,0.00424095,1.27},{0.293,0.00248649,1.28},{0.299049,0.0000509021,1.29},{0.305648,-0.00290428,1.3},{0.3126,-0.00619295,1.31},{0.319689,-0.00961104,1.32},{0.326692,-0.0129479,1.33},{0.333388,-0.0159947,1.34},{0.339569,-0.018555,1.35},{0.345052,-0.0204559,1.36},{0.349685,-0.0215538,1.37},{0.353355,-0.0217419,1.38},{0.355998,-0.0209598,1.39},{0.357599,-0.0191933,1.4},{0.358195,-0.0164774,1.41},{0.357876,-0.012897,1.42},{0.356783,-0.00858548,1.43},{0.3551,-0.00371655,1.44},{0.353051,0.00149815,1.45},{0.350887,0.00682148,1.46},{0.348878,0.0119984,1.47},{0.347298,0.01677,1.48},{0.346412,0.020886,1.49},{0.346466,0.024114,1.5},{0.347666,0.0262593,1.51},{0.350175,0.027168,1.52},{0.35409,0.0267484,1.53},{0.359445,0.0249685,1.54},{0.366196,0.0218697,1.55},{0.374222,0.0175664,1.56},{0.383326,0.0122447,1.57},{0.39324,0.00615778,1.58},{0.403638,-0.000386368,1.59},{0.414145,-0.00703349,1.6},{0.424364,-0.0134085,1.61},{0.43389,-0.0191289,1.62},{0.442339,-0.0238318,1.63},{0.449372,-0.027197,1.64},{0.454715,-0.0289656,1.65},{0.458178,-0.0289582,1.66},{0.459661,-0.0270802,1.67},{0.459142,-0.0233109,1.68},{0.456655,-0.0176824,1.69},{0.452226,-0.0102193,1.7},{0.44578,-0.000850736,1.71},{0.436996,0.0107266,1.72},{0.425101,0.0252429,1.73},{0.408591,0.0441191,1.74},{0.384856,0.0698209,1.75},{0.349698,0.106314,1.76}}

This new data is made up so this new model isn't meaningful, but I'll highlight the example here anyway.

newFunc[{X1_,X2_}]:=21.0078-0.114172 (X1-1. X2+(-13.4291+X1+X2)^2)

Show[ListLinePlot[Transpose[{Transpose[data][[1]],newFunc[#]&/@Transpose[Transpose[altData][[;;2]]]}],PlotStyle->Green],ListPlot[data]]

enter image description here

$\endgroup$
10
  • $\begingroup$ How do you find: 0.436094+2.1746 (0.484306+1. Sqrt[2.71828^(1/((x)^2 (-3.14159+13.7321 (x)^3)))])^2 ? $\endgroup$
    – SciJewel
    Commented Apr 8, 2022 at 23:38
  • $\begingroup$ I use DataModeler (evolved-analytics.com) when trying to find equations to help explain data when I have no known model form. It’s possible that Mathematica’s FindFormula function could give something similar but I find it to be less useful generally and I believe it can only consider single variable inputs whereas there is no limit when I use DataModeler. $\endgroup$
    – Nate
    Commented Apr 9, 2022 at 0:49
  • 1
    $\begingroup$ I see. Can you remove your suggestion "You could always go crazy and fit any sort of strange function to try to get a good fit." and the graph under it. Because it is too much and confusing to others. $\endgroup$
    – SciJewel
    Commented Apr 9, 2022 at 4:41
  • 2
    $\begingroup$ I thought your "You could always go crazy and fit any sort of strange function to try to get a good fit." statement was right on the money. Sorry to see it removed. $\endgroup$
    – JimB
    Commented Apr 9, 2022 at 20:50
  • $\begingroup$ I added it back into the post. It might not necessarily propose a model that should be used to fit the data but I think its inclusion highlights an important point. $\endgroup$
    – Nate
    Commented Apr 9, 2022 at 21:19

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.