
function [p,alt,er]=unipoly(f,df,a,b)
%function computes linear best unform approximation polynomial 
%for a strictly convex function f
%INPUT 
% f,df- function handles to f and df
% a,b - interval end
%OUTPUT
%p=cx+d - linear polynomial coeff as a vector [c,d]
%alternans as 3 entries vector
%er - ||p-f||_{\infty}
%EXAMPLE
%a=-1;b=3;f=@(x) x.*x +cos(x); df=@(x)  2*x-sin(x);[p,alt,er]=unipoly(f,df,a,b)
%s=linspace(a,b);fs=f(s);ps=polyval(p,s);plot(s,fs,s,ps,alt,f(alt),"+");
%[f(alt)-polyval(p,alt),norm(fs-ps,"inf")]

fb=f(b);
c=(fb-f(a))/(b-a);
g=@(x) df(x)-c;
alt=[a,0,b];
alt(2)=fzero(g,[a,b]);
d=(fb+f(alt(2)) -c*(alt(2)+b))*0.5;
p=[c,d];
er=fb-c*b-d;
end



