0
$\begingroup$

Can someone please help me. I have a function

f[x_] := 3*(x - Pi/2)*(Sin[4*x])^2 + 2*Cos[3*x]

and I need to find coefficients of function

g[x_]:= c1 + c2*x + c3*x^2 + c4*x^4

so that maximum absolute error of ( Abs[f[x] - g[x]]) is minimized. On the range of 0 <= x <= Pi.

Please help me, I'm desperate.

$\endgroup$
  • $\begingroup$ Look up NonLinearModelFit in the docs. $\endgroup$ – Marius Ladegård Meyer Jun 23 '17 at 10:48
  • 1
    $\begingroup$ That is for least squared - method, I need to find function so the maximum absolute error is minimized $\endgroup$ – Cro Simpson2.0 Jun 23 '17 at 11:11
  • $\begingroup$ FindFit has an option called NormFunction, I think that is what you are after. $\endgroup$ – Marius Ladegård Meyer Jun 23 '17 at 11:29
  • 1
    $\begingroup$ Might want to check the Remez method. $\endgroup$ – Daniel Lichtblau Jun 23 '17 at 20:02
  • 1
    $\begingroup$ @MariusLadegårdMeyer Both FindFit and NonLinearModelFit seem to require one to supply data points. Are you suggesting OP generate data points? $\endgroup$ – jjc385 Jun 24 '17 at 0:02
4
$\begingroup$

Sorry, I was away from my computer and could not write up an answer right away. I think this is what you want:

Generate some data points that FindFit can work with:

data = Table[{x, f[x]}, {x, 0, Pi, Pi/1000}];

Use FindFit with the infinity norm (see the docs of NormFunction for an example):

bestfit = FindFit[N@data, g[x], {c1, c2, c3, c4}, x, NormFunction -> (Norm[#1, Infinity] & )]

{c1 -> 0.880948, c2 -> -8.93938, c3 -> 6.25143, c4 -> -0.385789}

Notice that it is different when least squares is used:

FindFit[N@data, g[x], {c1, c2, c3, c4}, x]

{c1 -> 1.4222, c2 -> -10.3303, c3 -> 6.92248, c4 -> -0.409044}

Store the best-fit expression:

h = g[x] /. bestfit;

Compare:

Plot[{f[x], h}, {x, 0, Pi}]

enter image description here

$\endgroup$
  • $\begingroup$ While it seems somewhat inelegant (and dangerous, if one is not sufficiently careful) to generate data points from an exact function, I don't see another way to do it. +1 $\endgroup$ – jjc385 Jun 24 '17 at 22:32

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.