A Taylor Series Expansion to Approximate a Function near a Point

The Taylor series expansion up to seventh terms for approximation of near point x =1 is,
Taylor series expansion up to seventh terms





Letting starting point xₒ is at 0 and the step size (xi - xₒ) is 1. Now, the value of eˣ at x = 1 is to be calculated by Taylor series expansion on the basis of the starting point and its derivatives at x = 0 with step size 1. A MATLAB program is written to evaluate this value from zero order approximation to sixth order approximation and show the improvement of accuracy and minimization of truncation error as well.
The true value of is 2.718281828459046. The truncation error is the difference between true and approximate values. The following MATLAB program describes each steps of the calculation. 

Taylor Series Implementation by a MATLAB Program

% Taylor Series expansion to approximate exp(x) near point x=1
 
function e = taylorseries(x0,x1,h)
 
e0=exp(x0);              % calculates exp(x) value when x=0
 
derivative1=1;           % all the six derivatives calculated at x=0
derivative2=1;
derivative3=1;
derivative4=1;
derivative5=1;
derivative6=1;
 
% zero order approximation
 
        e=e0;   
        error=exp(x1)-e;      
        disp (e);        % display of exp(x) and error at command prompt          
        disp(error);
        
% first order approximation        
            
        e=e0+(derivative1*h);       
        error=exp(x1)-e;       
        disp (e);       
        disp(error);
             
% second order approximation
 
        e=e0+(derivative1*h)+(derivative2*(h^2/factorial(2)));        
        error=exp(x1)-e;        
        disp (e);        
        disp(error);
        
% third order approximation
                     e=e0+(derivative1*h)+(derivative2*(h^2/factorial(2)))+(derivative3*(h^3/factorial(3)));        
        error=exp(x1)-e;        
        disp (e);        
        disp(error);
        
% fourth order approximation        
                  e=e0+(derivative1*h)+(derivative2*(h^2/factorial(2)))+(derivative3*(h^3/factorial(3)))+(derivative4*(h^4/factorial(4)));       
        error=exp(x1)-e;        
        disp (e);        
        disp(error);
        
% fifth order approximation                      
        e=e0+(derivative1*h)+(derivative2*(h^2/factorial(2)))+(derivative3*(h^3/factorial(3)))+(derivative4*(h^4/factorial(4)))+(derivative5*(h^5/factorial(5)));        
        error=exp(x1)-e;        
        disp (e);        
        disp(error);
        
% sixth order approximation
        e=e0+(derivative1*h)+(derivative2*(h^2/factorial(2)))+(derivative3*(h^3/factorial(3)))+(derivative4*(h^4/factorial(4)))+(derivative5*(h^5/factorial(5)))+(derivative6*(h^6/factorial(6)));        
        error=exp(x1)-e;        
        disp (e);        
        disp(error);
end


Table 1: Outputs from the Above Program to Approximate at x=1

Approximation of an exponential function by Taylor series










The above table tells us as the order of the Taylor series increases the truncation error also decreases. However, after sixth and higher order approximations, improvement of the accuracy is not much significant. So, we can stop the approximation to first seven terms of the Taylor series and the approximate value of ex near point x = 1 is 2.718.

No comments:

Post a Comment