% uniform approximation
n = 4;
D = -1:2^(-4):1;
g = sin(D*pi);
X = D.^((0:n)');
cvx_begin
    variable tau
    variable c(1,n+1)
    -tau <= g - c*X
    g - c*X <= tau
    minimize( tau )
cvx_end
close all
figure
hold on
x = -1:2^(-10):1;
xx = x.^((0:n)');
plot(x,sin(x*pi),'b')
plot(D,g,'bo')
plot(x,c*xx,'r')
plot(D,c*X,'ro')
xlabel('x')
title('Blue = original function, Red = approximation')
grid on
figure
hold on
plot(x,sin(x*pi) - c*xx,'b')
plot(D,g - c*X,'bo')
plot(x,tau*ones(1,length(x)),'r:')
plot(x,-tau*ones(1,length(x)),'r:')
title('Approximation error')
xlabel('x')
grid on
