0

I am trying to fit the function $A/x³$ to some data points (X,Y). I tried using least squares to fit the function $A x^3$ to the data points (1/X,Y) using least squares. However I do not obtain a good result. I though that maybe the data points need to be weighed somehow?

fit result

Here is the python function I am using right now:

def fit_params(x,y):
    M = np.atleast_2d(1/x[1:]**3).T
    weigth = np.diag(1/y[1:]**2).T
    p, res, rnk, s = lstsq(M, y[1:])
    print(res)
    plt.plot(x, y, 'o', label='data')
    plt.plot(x, p*x**(-3.), label='fit')
    plt.show()
    return p

I obtained an equally bad result when using from scipy.curve_fit but that may be because of bad initial guesses. I also tried np.polyfit and just deleting all other coefficients, but that doesn't make sense, I think. It also led to a bad result.

In case you need the full data and code, you can have a look here.

Would appreciate any query and if needed can provide further clarification.

User1865345
  • 8,202
  • 4
    It doesn't look like your data obeys the $A/x^3$ relation – Firebug Nov 21 '22 at 10:25
  • Your data looks more like $A/x$. – Stephan Kolassa Nov 21 '22 at 11:16
  • 2
    You can let the power automatically be determined by fitting $y ~ \beta_0 + \beta_1 \log x$. – cdalitz Nov 21 '22 at 11:31
  • 2
    None of those suggestions will work, because the response is finite at $x=0.$ You should be exploring the possibilities rather than trying to shoehorn them into some preconceived formula. Could you explain why you want to use a power function for this fit? That might help suggest alternatives. – whuber Nov 21 '22 at 13:46
  • 1
    Taking the logs of both variables would linearize a power function, but that can't work as whuber points out. If you do that with your data you can see that even ignoring the issue with $x=0$, it's still clearly non linear on the log-log scale. The middle 10 values are not so far from linear but even there the slope on the logged data is nowhere near -3. – Glen_b Nov 21 '22 at 16:41
  • You all are totally right, it is not an inverse polynomial of 3 degree @Glen_b. Thank you for pointing it out. I thought it was, because in a physical model we made the assumption that a ceratin magnetic field had a magnetic dipole moment. But of course since this is close to the magnet, we have to look at higher order moments. – TheFibonacciEffect Nov 22 '22 at 12:50
  • Fitting What can I do with this question now? Should I close or delete or edit it? Fitting higher orders seems to have worked. – TheFibonacciEffect Nov 22 '22 at 13:13
  • 1
    What exactly is the distance and how is it measured? – Firebug Nov 23 '22 at 10:13
  • 1
    I agrre with the comment from Firebug about the distance measurement which might be an important parameter. See my answer. – JJacquelin Nov 23 '22 at 15:09
  • 1
    Tukey's three-point method will easily find a suitable transformation. – whuber Nov 23 '22 at 15:17

1 Answers1

2

By inspection one observe a particular behaviour at low values of $x$.

The next figure shows the function $(x^ay)$ for $a=0\;,\:a=1\;,\:a=2\;,\:a=3$.

enter image description here

One could draw intermediate cases with non integer $a$.

Nevertheless this is enough to see that the function is not simple at low $x$. But this is hidden if one choose too high value of $a$. One can understand why high $a$ gives a wrong impression that fitting higher orders seems to work because the "strange" part of the function is hidden. But this part continue to exist and disturbs the fitting calculus.

This answers to the comment: "What can I do with this question now? Should I close or delete or edit it? Fitting higher orders seems to have worked."

I suppose that one have to find a better physical model which accounts for the behaviour at low $x$.

In addition : EMPIRICAL FITTING

Instead of the search for an experimental model one can try various empirical models (purely mathematical). Of course it is not as valuable as a physical model.

If the first point (at $x=0$) is kept out it seems that the power model is not bad. But one have to include in the equation a shift $a$ for $y$ and a shift $b$ for $x$.

enter image description here

The shift on the $y-$axis is small.

The shift on the $x-$axis is very important.

Surprisingly the power of the inverse function is larger than expected : around $5$ instead of $2$ or $3$.

This draw to think that this rather simple model might have few physical signifiance even with a good fitting result. Another hypothesis is that the origin for the distances measurements on $x-$ axis be not correct.

  • 1
    +1 This is a good solution. For an easier, more straightforward method to find it, please see https://stats.stackexchange.com/a/35717/919. – whuber Nov 23 '22 at 15:18