I am trying to implement a very simple NMinimize
code that would search for parameters of a polynomial that minimize (globally) the distance between this polynomial and one given curve. The distance that I need to use is the uniform distance (i.e. $\max\limits_{x\in [0,1]} |f(x)-g(x)|$) , or infinite norm.
I do not want to use the FindFit
command as it has less flexibility and also globality of the found solution is not ensured (especially for more involved problems).
So I have constructed the following code:
v[x_] := ChebyshevT[6, x] (* the given function to be approximated *)
f[x_?NumericQ, a_?NumericQ, b_?NumericQ, c_?NumericQ] :=
a x^2 + b x + c (* funcn whose parameters I want to find *)
abs[a_?NumericQ, b_?NumericQ, c_?NumericQ, x_?NumericQ] :=
Abs[v[x] - f[x, a, b, c]]
maxabs[a_?NumericQ, b_?NumericQ, c_?NumericQ] :=
NMaxValue[abs[a, b, c, y], {y, 0, 1}]
n = NMinimize[{maxabs[a, b, c]}, {a, b, c}, Method -> "NelderMead"]
Plot[{v @ y, f[a, b, c, y] /. n[[2]]}, {y, 0, 1},
PlotStyle -> {{Red}, {Dashed, Blue}}]
However this gives me a fit which is obviously suboptimal, (a straight line, while even the naked eye suggests that a simple parabola would be a way better fit)... Did I do anything wrong with the code?
NMinimize
failed to converge to a solution. Do you not get the same? $\endgroup$f[y,a,b,c]
notf[a,b,c,y]
. $\endgroup$MiniMaxApproximation
. $\endgroup$