Linear programming, Help!

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.

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

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}]


• 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 – jjc385 Jun 24 '17 at 22:32