Discretization of simple function using different orders of approximation
Given data :
- analytical function: `sin(x)/x^3`
- We've to compute the first derivative at `x= pi/3` for `dx = pi/30`
We need to calculate different orders of differentials and get the absolute value of the error. The smaller the error term, the closer it is to the analytical solution. These values obtained are the discrete value for the given function that can be used for many experimental studies.
Program Explanation:
First, the analytical derivative of a function is been calculated. Then first, second and fourth-order approximations were calculated. Errors for the respective order were calculated.
Error is the absolute difference between the order approximation term and the analytical derivative term.
The formula for first-order approximation:
Forward differencing = `(f(x+a)-f(x))/ dx`
The formula for second-order approximation:
Central differencing = `(f(x+a)-f(x-a))/ (2*dx)`
Formula for fourth order approximation:
differencing = `(f(x-2*dx) - (8*f(x-dx)) + (8* f(x+dx)) - f(x+2*dx))/(12*dx)`
Bar graph shows us the pictorial representstion of the values we derived. It can be clearly seen that, as the order of approximation increases, the margin between the analytical solution and derivation solution also decreases. That is, we are getting close to true, desired solution.
These values are clearly ploted with proper labels and titles attached on each bar for clear understanding.
Program:
function [error_first_order, error_second_order, error_fourth_order] = order_approximation(x,dx);
% inputs for program:
x = pi/3;
dx = pi/30; % dx is step size for which it takes next value of error.
% analytical function f(x) = sin(x)/x^3
% analytical term's derivative f'(x):
analytical_derivative = ((x^3*(cos(x)))- (sin(x)*(3*(x^2))))/(x^6);
% First Order Approximation:
forward_difference = ((sin(x+dx)/(x+dx)^3)-(sin(x)/(x)^3))/dx;
% Secound Order Approximation:
central_difference = ((sin(x+dx)/(x+dx)^3)-(sin(x-dx)/(x-dx)^3))/(2*dx);
% Fourth Order Approximation:
differencing = (((sin(x-2*dx)/(x-2*dx)^3) - 8*(sin(x-dx)/(x-dx)^3)+ 8*(sin(x+dx)/(x+dx)^3)- (sin(x+2*dx)/(x+2*dx)^3)))/(12*dx);
% errors:
error_first_order = abs(forward_difference - analytical_derivative)
error_second_order = abs(central_difference - analytical_derivative)
error_fourth_order = abs(differencing - analytical_derivative)
%Plotting:
error = [error_first_order, error_second_order, error_fourth_order]
% Categorical is going to define array of cateogorical string arrays for
% giving the label
x= categorical({'First order error','Second order error','Fourth order error'});
%Reordercats is going to arrange the strings in the order we want.
% Please go through the documentation for more details.
x= reordercats(x,{'First order error','Second order error','Fourth order error'});
g = bar(x,error,0.2,'b','EdgeColor',[0 .9 .9]);
g.FaceColor = 'flat';
g.CData(1,:) = [0.8 0 0.5];
g.CData(2,:) = [0.2 0.8 0.2];
xlabel('Error Orders')
ylabel('Error values')
title('Graph of Differential Order Errors')
end
Output Result:

Obtained Graph:
