x = .6;
n = 100;
hmax = .3;
hmin = - hmax;

fx   =    x *exp(x);
fpx  = (1+x)*exp(x);
fppx = (2+x)*exp(x);

y  = x;
dh = ( hmax - hmin )/n;
for i=1:n
  h     = hmin + i*dh;
  yt    = x + h;
  y(i)  = yt;
  f(i)  = yt*exp(yt);
  f0(i) = fx;
  f1(i) = fx + h*fpx;
  f2(i) = fx + h*fpx + h^2*fppx/2;
  E0(i) = f(i) - f0(i);
  E1(i) = f(i) - f1(i);
  E2(i) = f(i) - f2(i);
end

figure(1)
plot (y,f,y,f0,y,f1,y,f2)
grid
xlabel('x')
ylabel('f')
title('f(x) and approximations up to second order')
print -dps Taylor_approx_na


figure(2)
plot (y,E0,y,E1,y,E2)
grid
xlabel('x')
ylabel('error')
title('Errors of approximations of order zero, one, and two')
print -dps Taylor_error0_na

figure(3)
plot (y,E1,y,E2)
grid
xlabel('x')
ylabel('error')
title('Errors of approximations of order one and two')
print -dps Taylor_error1_na


figure(4)
plot (y,E2)
grid
xlabel('x')
ylabel('error')
title('Error of the approximation of order two')
print -dps Taylor_error2_na



