1
$\begingroup$

I found this fit but I do not understand how to find the maximal absolute error nor do I understand what it is really. I do not see what it is I would compare, would it be f[-2] to lets say p4[2]?

f[x_] = 1/(1 + x^2);
f[-2];
f[-1];
f[0];
f[1];
f[2];
XY = {{-2, 1/5}, {-1, 1/2}, {0, 1}, {1, 1/2}, {2, 1/5}};
p4[x_] = Fit[XY, {1, x, x^2, x^3, x^4, x^5}, x];
$\endgroup$
0
$\begingroup$
f[x_] := 1/(1 + x^2)
fitf[x_] = Fit[{#, f[#]} & /@ Range[-2, 2], x^Range[0, 5], x] // Chop

1.-0.6 x^2 + 0.1 x^4

The absolute error is just the (sometimes) absolute value of the difference between the measured value and the predicted value.

Plot[{f[x], fitf[x], Abs[f[x] - fitf[x]]}, {x, -2, 2}]

enter image description here

Then we can find the maximum of the absolute error and its location with

NMaximize[{Abs[f[x] - fitf[x]], -2. <= x <= 2.}, x]

{0.161803, {x -> -1.61803}}

| improve this answer | |
$\endgroup$
  • $\begingroup$ The equation makes sense now, thanks! But I am now confused what you did for the fit function? if we have 5 points wouldn't that mean we would have a degree 4 polynomial? $\endgroup$ – Sherien Hassan Oct 22 '19 at 21:38
  • $\begingroup$ Oh, no I think my code was incorrect because I had it go up to x^5 when it was supposed to stop at x^4. Thanks again!! $\endgroup$ – Sherien Hassan Oct 22 '19 at 21:45
  • $\begingroup$ @SherienHassan The output of Fit[{#, f[#]} & /@ Range[-2, 2], x^Range[0, 5], x] is 1. - 1.10913*10^-15 x - 0.6 x^2 - 5.23354*10^-16 x^3 + 0.1 x^4 - 8.90048*10^-17 x^5. Chop replaces the coefficients less than 10^-10, which are effectively 0, with 0. Thus the odd powers drop out. $\endgroup$ – That Gravity Guy Oct 22 '19 at 21:48
  • $\begingroup$ Hmm I am still sort of confused, I understand how the chop function works but I thought needed n-1 degree. So given 5 points we would have a degree 4 polynomial? I might be understanding interpolation incorrectly. $\endgroup$ – Sherien Hassan Oct 22 '19 at 21:56
  • $\begingroup$ @SherienHassan, function f[x] is symmetric, so there should not be odd powers in fitting polynomial. $\endgroup$ – Alx Oct 23 '19 at 3:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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