3
$\begingroup$

I was trying to fit a powerlaw to some data and noticed some inconsistency in the behaviour of mathematica.

This works

{1, 2, 3, 4, 5} // Fit[#, {1, Log[r]}, Log[r]] & // Exp

(* r *)

whereas this does not

{1, 2, 3, 4, 5} // Fit[#, {1, Log10[r]}, Log10[r]] & // 10^# &

enter image description here

While I understand the logic, it seems it represents some level of inconsistency between the way the variables are handled by Fit?

$\endgroup$
4
  • $\begingroup$ Wow, it's a bit surprising to me the first sample works. $\endgroup$
    – xzczd
    Commented Aug 21, 2022 at 8:06
  • $\begingroup$ @xzczd I agree, but its also unfortunate both do not behave the same way (?) $\endgroup$
    – chris
    Commented Aug 21, 2022 at 8:11
  • 2
    $\begingroup$ To be on the safe side, you should fit relative to a variable not an expression. That the first example works is really a surprise. $\endgroup$ Commented Aug 21, 2022 at 8:14
  • 1
    $\begingroup$ I think the first one only works for the same reason that e.g. a[1] is generally allowed to be considered a "variable" in functions like these—Log[r] just happens to be of the same form. Its "meaning" as a logarithm isn't seen by Fit. (However, Log10[r] immediately evaluates to Log[r]/Log[10], and so is not recognized as a variable.) $\endgroup$
    – thorimur
    Commented Aug 22, 2022 at 20:34

0

Your Answer

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

Browse other questions tagged or ask your own question.