Welcome to the World of Modelling and Simulation

What is Modelling?

This blog is all about system dynamics modelling and simulation applied in the engineering field, especially mechanical, electrical, and ...

Showing posts with label Taylor Series Expansion. Show all posts
Showing posts with label Taylor Series Expansion. Show all posts

Application of the Taylor Series to Determine the Unknown Coefficients of a Polynomial

Let's say, we have a polynomial equation of the following form. 

y = ax^b + 5

Where, a, and b are the unknown coefficients of the polynomials. In this tutorial, we will discuss a method to determine the unknown coefficients using the Taylor series expansion assuming the data points are provided.

From the above polynomial equation, y is the dependent variable, and x is the independent variable. Let's assume that we know ten data points for the polynomial so that the following equations may be written based on the data points: 

y₁ = ax₁^b + 5

y2 = ax2^b + 5

y3 = ax3^b + 5

.
.
.

y10 = ax10^b + 5

This model seems to have a nonlinear dependence on the parameters a and b. Nonlinear regression can be used here to estimate these parameters. Nonlinear regression is based on determining the values of the parameters that minimize the sum of the squares of the residuals and the solution is preceded in an iterative manner. The Gauss-Newton method is one of the algorithms which minimize the sum of the squares of the residuals between nonlinear equation and data. A Taylor series expansion is used to express the nonlinear equation is an approximate linear form. After that, least square method is applied to estimate the parameters, which move towards the minimization of the residuals.

yi = f(xi; a,b,c) + ei;          i = 1, 2, 3, ... ... ... 10

Where, yi is a measured value of the dependent variable,  f(xi; a,b,c) is the nonlinear function of the independent variable, xi, with two unknown parameters a, b, and one given parameter c, which is 5 according to the given correlation. The last term ei is the random error. The model may be represented in a short form without the parameters for convenience as,

yi = f(xi) + ei                               (1)

For the two parameters case, a Taylor series expansion is written for the first two terms as,

Taylor series expansion for first two terms





Here, j is the initial guess, j+1 is the prediction, ∆a = aj+1 - aj and ∆b = bj+1 - bjEquation (2) is substituted to equation (1) which yields,

Taylor series expansion for first two terms




In matrix form, equation (3) may be written as,

Taylor series in matrix form




Where, Zj is the matrix of the partial derivatives of the function at the initial guess j.

Matrix of the partial derivatives


















The vector {D} contains the difference between the measurements and function values at 10 points.

Contains the difference between the measurements and function values











And, the vector {A} contains the changes in the parameters’ values as,

Contains the changes in the parameters






Applying least square method to equation (4) results the following normal equation,

Applying the least square method




Equation (5) is implemented to solve for {∆A} to estimate the improved values of the parameters a and b,

aj+1 = aj + ∆a

bj+1 = bj + ∆b

Thus, this procedure is continued until the solution converges.



#TaylorSeries #Polynomial #NonlinearRegression #LeastSquare #Matlab #Blog #Blogger

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.