% Example 4.1.2: Linear interpolation.
% Estimate 
%       exp(0.826) 
% using the function values 
%   exp(0.82)=2.270500, exp(0.83)=2.293319
% and compare with the value of the linear Taylor polynomial
clear all
x_nodes = [0.82 0.83] ;
y_values = [2.270500 2.293319];
x_eval = 0.826 ;
divdif_y = divdif(x_nodes,y_values);
format long
exp(x_eval);
p_eval = interp(x_nodes,divdif_y,x_eval);
z = evaluate_exp_tay(x_eval);
error = exp(x_eval)-p_eval;

fprintf('The error linear interpolation of in exp(%.3f)=%.10f is %.10f\n',x_eval,exp(x_eval),error)

t=0.8:0.01:0.842;
f=exp(t);
LinearInterpolation = interp(x_nodes,divdif_y,t);
Taylor_LinearValues = evaluate_exp_tay(t);
Taylor_QuadraticValues = evaluate_exp_tay(t);
Taylor_CubicValues = evaluate_exp_tay(t);

figure(1)
title('Example 4.1.2')
axis([0.817 0.85 2.2 2.4])
hold all
plot(x_nodes,y_values,'o')
plot(t,f)
plot(x_eval,p_eval,'*')
plot(t,LinearInterpolation,'r','LineWidth',1)
plot(t,Taylor_LinearValues,'b','LineWidth',1)
plot(t,Taylor_QuadraticValues+.003,'g','LineWidth',1)
plot(t,Taylor_CubicValues+.005,'g','LineWidth',1)
legend('data','exact','interpolated evaluation','linear interpolation','linear Taylor','quadratic Taylor + 0.003','cubic Taylor + 0.005')


