function ex3_3(xleft,xright)
% Example 3.3
% Function to plot the Taylor approximations of the polynomial function
%   with a = 0
%
% Syntax: ex3_3(xleft,xright)
%           xleft,xright = min and max limits of interval
%
% To produce the plots for Example 3.3, call the function with
%  ex3_3(-2,2)

% Check for input error and fix it
if xleft>xright
    dummy = xleft;
    xleft = xright;
    xright = dummy;
end

% Close all figures to avoid superimposing on old figures
close all

% Create an array of uniformly spaced points on the interval
x = linspace(xleft,xright,20);

% Exact Function
fx = -0.1*x.^4 - 0.15*x.^3 - 0.5*x.^2 - 0.25*x + 1.2;  %Array of exact values

% Store intermediate values
h = x-0;
f0 = 1.2*ones(size(x));

% defining the function by Taylor series expansions
f1x = f0 - 0.25*h;                  % 1st order approximation
f2x = f1x - 1*(h.^2)/2;             % 2nd order approximation
f3x = f2x - 0.9*(h.^3)/6;           % 3rd order approximation
f4x = f3x - 2.4*(h.^4)/24;          % 4th order approximation

% Plotting the exact function and polyomials against x
figure(1)
plot(x,fx,'o-k',x,f0,x,f1x,x,f2x,x,f3x,x,f4x);
%axis([xleft,xright,1.5,2.25])
legend({'exact','0th order','1st order','2nd order','3rd order','4th order'},'Location','southwest');
xlabel('x');
ylabel('Approximations to f(x) = -0.1x^4 - 0.15x^3 -0.5x^2 - 0.25x + 1.2');

