1
$\begingroup$

I'm looking for the formula that best approximates these experimental x-y values: 1

A correct answer should present asymptotic behavior on both the X and Y axes. Y should approach infinity as X approaches 0 and Y should approach 0 as X approaches infinity. My best shots until now have been:

  1. $$-67\ln(x)+600$$ which gives good approximations to the three first values, but lacks the horizontal asymptote, thus giving a negative Y values from a certain X on.

  2. $$\frac{-67\ln(x)+600}{e^{0.005x}}$$ which solves the asymptote problem and gives a reasonably good approximations for the first three values, but approaches 0 too quickly.

I'm no mathematician, so it is possible that there are simple tools to design my desired function I don't know of. I didn't follow a procedure of any kind to obtain the formulas above, but instead eyeballed it on GeoGebra the best I could, starting by considering the function should have asymptotes on both axis and present a somehow logarithmic behavior, quickly approaching infinite for $X<1$ and slowly approaching zero as X approaches infinity.

$\endgroup$
7
  • $\begingroup$ Your second guess is also unbounded as $x$ increases, so it should also give negative Y values from a certain X on. The function $ln(x)$ approaches infinity as $x\rightarrow \infty$, multiplying it by constants won't solve this issue. If you really want your function to approach $0$ at infinity you'll have to try other types of functions. One family of functions with such properties are the $1/x^n$, where $n$ is a real number. $\endgroup$ Aug 3, 2021 at 13:24
  • $\begingroup$ @LucasBaldo I think the OP has forgotten to put a x on the exponent. That would explain the behavior it is described in the question $\endgroup$
    – Prallax
    Aug 3, 2021 at 13:35
  • $\begingroup$ That is correct @Prallax, I'll edit it $\endgroup$ Aug 3, 2021 at 14:16
  • $\begingroup$ Eureqa software achieves curve fitting to various metrics with vast numbers of combinations of classes of functions that you choose. It's no longer free, but perhaps the trial version will be sufficient for this task. $\endgroup$ Aug 3, 2021 at 14:27
  • $\begingroup$ Can you provide more than 4 data points? $\endgroup$ Aug 3, 2021 at 19:59

1 Answer 1

1
$\begingroup$

Disclaimer: I am aware that the policy of the site is not to give complete solutions to homeworks. But this does not look like a homework, and my answer outlines a general procedure that could be used to solve many similar problems. Indeed, it would have been pointless to solve a different problem just for the sake of not giving the full answer, because generalization is straightforward. Furthermore, I did not provide the numerical solution to the problem.


What you are trying to achieve is called a "fit": finding a function that best approximates your data points. There are many tools that can do it for you, but more on that later.

The first thing you need when you want to fit some data points is a model. A model is a function $f(x;a,b,c,...)$, where $x$ is the independent variable and $a$, $b$, $c$... are called parameters. Your goal is to find the values of the parameters such that the function passes near your data points.

In some cases you already know what model to use. It may be given by the physical situation. For example, if the X data represent time and Y data represent the position of a free falling object, the model you want to use is the law of uniform acceleration: $$f(x;a,b,c) = ax^2+bx+c$$ By performing a fit of your data, you will obtain the value of the acceleration $a$, the velocity $b$ and the initial position $c$.

Since you don't mention any given model in your question, I assume that you are not aware of any particular physics law that would describe your data. You only have the requirement of the two asymptotes.

So we need to chose a model that fulfills the requirement, and the model needs to have few parameters. This is really important. You definitely want a model with less parameters than the number of available data points, but the less, the better. The best model is the one that is able to reasonably reproduce your data points with the smallest number of parameters. There are ways to make this statement more rigorous, but I will not cover it here.

A simple model that presents the two required asymptotes is a power law with negative exponent:

$$f(x;a,b) = ax^{-b}$$

Now comes the tool part. There are many powerful tools to perform a fit. Some examples are Matlab, Mathematica, R, and even Excel. I prefer to use python and this is what I will show in this answer.

IMO, the easiest way to begin with python is to use Google Colaboratory. You don't have to download anything and can run python directly from the browser.

Once you are in Colab, the first thing you need to do is importing the necessary libraries:

import numpy as np                    # math functions and arrays
import matplotlib.pyplot as plt       # plotting functions
from scipy.optimize import curve_fit  # fit function

Then you can define the arrays of data points

X = np.array([1,10,100,2e6])
Y = np.array([600,400,240,4])

And the model function

def f(x,a,b):
    return a*x**b

Now you can finally call the fitting function and let python do its magic

p1,_ = curve_fit(f,X,Y)

$\texttt{p1}$ now contains the best values of $a$ and $b$ found by python.

You can plot the result, in this case a logarithmic scale may lead to a better visualization.

x = np.logspace(0,7)
plt.plot(x,f(x,*p1))
plt.scatter(X,Y)
plt.xscale("log")
plt.yscale("log")
plt.grid()

result of the fit

What to do next

If you were able to follow the steps until here, congratulations, you have done a fit. But this is only the beginning. You may have noticed that the first three points are perfectly aligned. They are truly following a power law then.

The last point is a bit off, tough. This may be because the chosen model was not adequate to describe the data, or the lower position of the last point may be due to experimental error. The only way to know is to estimate the uncertainties of your measures and run a statistical test on your fit, such as a Chi-squared test (easier to implement, but not always reliable) or a Bayesian test (more difficult, but usually preferred)

You can also give the uncertainties to the function curve_fit, to obtain a better result. To learn more about the many ways to use the function curve_fit visit the scipy documentation


A formula for linear models

A linear model is a model that depends linearly on the parameters, like $$f(x;\alpha,\beta) = \alpha + \beta x$$

A fit with this model can be found by using the formula for the simple linear regression.

Let the measurements of the independent variable be $X = (x_1,x_2,..., x_n)$, and the measurements of the dependent variable $Y= (y_1,y_2,..., y_n)$. The averages are defined as:

$$\bar{x} = \frac{1}{n}\sum_{i=1}^n x_i; \ \ \bar{y} = \frac{1}{n}\sum_{i=1}^n y_i$$

The best estimate of the parameters $\alpha$ and $\beta$ will be

$$\hat{\beta} = \frac{\sum_{i=1}^n (x_i-\bar{x})(y_i-\bar{y})}{\sum_{i=1}^n (x_i-\bar{x})^2}$$ $$\hat{\alpha} = \bar{y}-\hat{\beta}\bar{x}$$

The power law is not a linear model, but this formula can be used nonetheless, because the logarithm of the power law is $\log(f(x;a,b)) = \log(ax^{-b}) =\log(a) -b\log(x)$. Thus, if we define $\alpha = \log(a)$, $\beta = -\log(b)$, $x' = \log(x)$ and $f' = \log(f)$, then $f'(x', \alpha, \beta) = \alpha + \beta x'$ is a linear model.

This means that you can take the logarithm of your data X and Y, plug them into the formula to obtain $\hat{\alpha}$ and $\hat{\beta}$, from which you can find $a$ and $b$.

$\endgroup$
4
  • $\begingroup$ First of all, thanks a lot. Just to be clear, it isn't homework, but a thing I'd like to be more or less physically rigorous for a personal worldbuilding project. I'll follow your steps to the best of my capabilities to see if I get it. An answer giving the ecuation would have been great, but actually taking the time to teach me how to do it myself is much better for future reference so, again, thank you! $\endgroup$ Aug 3, 2021 at 15:58
  • $\begingroup$ My python knowledge is zero, how could make the program retrieve the expression that defines the resulting plot? $\endgroup$ Aug 3, 2021 at 16:14
  • $\begingroup$ If you want to retrieve the values of the parameters a and b, just type "print(p1)" without quotes. p1 is an array that contains a and b, in that order $\endgroup$
    – Prallax
    Aug 3, 2021 at 16:26
  • 1
    $\begingroup$ for simple (i.e. linear) models like a power law, there is actually a formula that you could use. But it would not generalize to other models, for which you would still have to use python. I will edit and add this formula $\endgroup$
    – Prallax
    Aug 3, 2021 at 16:30

Your Answer

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

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