Truncation error analysis at different order
Our objective is to calculate 'first derivative' of function f(x) at `x=pi/3` by using approximation method (first order, second order and fourth order approximations) at diffrent values of 'dx' . After calculating derivative we have to calculate truncation error for first, second and fourth order approximation , to analyze change in truncation error with respect to 'dx' value.
Truncation error can be defined as difference between a truncated value and the actual value.
Since we are using Taylor series for calculating derivative and while calculating derivative we are ignoring higher order terms so those higher order term may have very small finite value so it creates small difference in calculation. The error in calculation is "truncation error" for current condition.
Given data:- Function `f(x)=sin(x)/x^3, x=pi/3`
MATLAB code:-
clear all
close all
clc
%analytical function, f(x)= sin(x)/(x^3)
x=pi/3;
%hence, analytical derivative can be calculated as, f'(x)= ((x^3)*cos(x)-3*(x^2)*sin(x))/(x^6)
Analytical_derivative= ((x^3)*cos(x)-3*(x^2)*sin(x))/(x^6);
%range for dx
dx = [pi/25 pi/100 pi/400 pi/1000 pi/5000];
%here, dx = [0.12566 0.031416 0.007854 0.0031416 0.00062832], i.e. dx is in decreasing order
for i=1:length(dx);
%by forward differencing, (f(x+dx)-f(x))/dx
first_order_aprox(i)= ((sin(x+dx(i))/((x+dx(i))^3))-(sin(x)/(x^3)))/dx(i);
error_first_order= abs(Analytical_derivative - first_order_aprox);
%by central differencing, (f(x+dx)-f(x-dx))/2dx
second_order_aprox(i)= ((sin(x+dx(i))/((x+dx(i))^3))-(sin(x-dx(i))/((x-dx(i))^3)))/(2*dx(i));
error_second_order= abs(Analytical_derivative - second_order_aprox);
%For fourth order, (f(x-2dx)-8f(x-dx)+8f(x+dx)-f(x+2dx))/12dx
fourth_order_aprox(i)= ((sin(x-2*dx(i))/((x-2*dx(i))^3))-8*(sin(x-dx(i))/((x-dx(i))^3))+8*(sin(x+dx(i))/((x+dx(i))^3))-(sin(x+2*dx(i))/((x+2*dx(i))^3)))/(12*dx(i));
error_fourth_order= abs(Analytical_derivative - fourth_order_aprox);
endfor
slope_first_order= diff(dx)./diff(error_first_order);
slope_second_order= diff(dx)./diff(error_second_order);
slope_fourth_order= diff(dx)./diff(error_fourth_order);
figure(1);
plot(dx,error_first_order,'color','r');
hold on;
plot(dx,error_second_order,'color','b');
hold on;
plot(dx,error_fourth_order,'color','c');
legend('First order error','Second order error','Fourth order error');
xlabel('dx');
ylabel('Truncation error');
figure(2);
loglog(dx,error_first_order,'color','r');
hold on;
loglog(dx,error_second_order,'color','b');
hold on;
loglog(dx,error_fourth_order,'color','c');
legend('First order error','Second order error','Fourth order error');
xlabel('dx');
ylabel('Truncation error');
In above written program, at first we calculated analytical derivative by formula of `f'(uxxv)=(u'xxv)+(v'xxu)` where for given function, `u=sin(x)" & " v=x^(−3)`
After differentiating function `f(x)` we get `f′(x) = (x^3cosx−3x^2sinx)/x^6`
At given condition, `x=pi/3`, we got `f′(x)=-1.7250`
So to calculate errors we have to find values of approximations by using forward differencing, central differencing methods for first order error and second order error respectively.
Hence as we know for forward differencing method we take next point. So for forward differencing we use `(f(x+dx)-f(x))/(dx)`
For second order, by using central differencing method, i.e. `(f(x+dx)-f(x-dx))/(2dx)`
For fourth order we have formula `(f(x-2dx)-8f(x-dx)+8f(x+dx)-f(x+2dx))/(12dx)`
By solving above equation we get values of first order differencing, second order differencing & fourth order differencing.
To calculate truncation error i.e. difference between exact analytical value and result values from differencing methods.
The difference can be calculated by using simple formula of ( Exact analytical value - Approximation values)
The following table gives us clear understanding for truncation errors of first, second & fourth order approximation at different values of dx.
| Order of approximation |
Truncation error at `dx=pi/25 = 0.12566` |
Truncation error at `dx=pi/100=0.031416` |
Truncation error at `dx=pi/400=0.007854` |
Truncation error at `dx=pi/1000=0.0031416` |
Truncation error at `dx=pi/5000=0.00062832` |
| First order approximation | 0.27078 | 0.075571 | 0.019454 | 0.0078281 | 0.0015706 |
| Second order approximation | 0.051274 | 0.0031398 | 0.00019599 | 3.1357e-05 | 1.2542e-06 |
| Fourth order approximation | 0.0047834 | 1.703e-05 | 6.615e-08 | 1.6929e-09 | 3.0222e-12 |
| Table 1: Truncation error values. | |||||
| Order of approximation | Slope for `dx=pi/25` & `dx=pi/100` | Slope for `dx=pi/100 " & "dx=pi/400` | Slope for `dx=pi/400` & `dx=pi/1000` | Slope for `dx=pi/1000` & `dx=pi/5000` |
| First order approximation | 0.48281 | 0.41987 | 0.40533 | 0.40164 |
| Second order approximation | 1.958 | 8.0038 | 28.623 | 83.491 |
| Fourth order approximation | 19.773 | 1388.9 | 73109 | 1.4872e+06 |
| Table 2: Slope values | ||||
After running above created code we get following plot which shows dx values on x axis and truncation error values on y axis.

For better and clear understanding at very low values of 'dx', we have created 'detailed view at A' in above plot.

The above figure is 'detailed view at A'.

Above plot shows 'dx values' v/s 'truncation error values' on logarithmic scale
By observing results in Tables and above plots its easy to conclude that
- As value of 'dx' decreases, the truncation error is also decreases irrespective of their order of approximation.
- At same value of 'dx', `"Error at first order">"Error at second order">"Error at fourth order"`