Discretization for a Range of dx
The objective of this challenge is to calculate the error for a range of dx using function codes. Thereby calculating the errors of First, second and Fourth-Order approximation and to look at its behaviour over the range of dx.
Here a first-order derivative is used.
FUNCTION TO SOLVE:
Here we are solving for f(x)=sin(x)x3f(x)=sin(x)x3
Its Analytical Derivative is x3⋅(cos(x))−sin(x)⋅3⋅x2x6x3⋅(cos(x))-sin(x)⋅3⋅x2x6;
First-order approximation:
This method uses a forward differencing technique by which the values are taken from the current node and the next node.
Now, why do we call it First-order approximation is that in Taylor series we remove the terms of the of second-order and so on in the R.H.S.The reason for the removal of second order is such that on L.H.S we don't have a second-order term. This is called truncation error and thus it becomes an approximation method and not an exact method. The order of delta x is 1 in the first term that we remove thus it is called as First-order approximation.
f(x+a)=f(x)+f'(x)△x+f''(x)△x22!+f''(x)△x33!....f(x+a)=f(x)+f′(x)△x+f′′(x)△x22!+f′′(x)△x33!....
Second-order approximation:
This method uses a central differencing technique by which the values are taken from the next node and the previous node.
When we add forward and backwards differencing we get Second-order approximation for the first-order derivative.
Now, why do we call it Second-order approximation is that in Taylor series we remove the terms of the of third-order and so on in the R.H.S. The order of delta x is 2 in the first term that we remove thus it is called as Second-order approximation.
Fourth-order approximation:
Now, why do we call it Fourth-order approximation is that in Taylor series we remove the terms of the of Fifth-order and so on in the R.H.S.The order of delta x is 4 in the first term that we remove thus it is called as Fourth-order approximation.
FUNCTION CODE FOR FIRST ORDER APPROXIMATION:
function out = first_order_approximation(x,dx)
analytical_derivative = (x^3*(cos(x)) - sin(x)*3*x^2)/x^6;
forward_differencing = ((sin(x+dx)/(x+dx)^3) - (sin(x)/(x)^3))/dx;
out = abs(forward_differencing - analytical_derivative);
end
FUNCTION CODE FOR SECOND ORDER APPROXIMATION:
function out = second_order_approximation(x,dx)
analytical_derivative = (x^3*(cos(x)) - (sin(x)*3*x^2))/x^6;
central_differencing = (sin(x+dx)/(x+dx)^3 - sin(x-dx)/(x-dx)^3)/(2*dx);
out = abs(central_differencing - analytical_derivative);
end
FUNCTION CODE FOR FOURTH ORDER APPROXIMATION:
function out = fourth_order_approximation(x,dx)
analytical_derivative = (x^3*(cos(x)) - (sin(x)*3*x^2))/x^6
fourth_orderapprox = ((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);
out = abs(fourth_orderapprox - analytical_derivative);
end
MAIN CODE FOR CALCULATING ORDER OF APPROXIMATIONS:
clear all
close all
clc
x = pi/3;
dx = linspace(pi/40,pi/4000,20);
for i = 1:length(dx)
first_order_error(i) = first_order_approximation(x,dx(i));
second_order_error(i) = second_order_approximation(x,dx(i));
fourth_order_error(i) = fourth_order_approximation(x,dx(i));
end
figure(1)
loglog(dx,first_order_error, 'g')
hold on
loglog(dx,second_order_error, 'y')
hold on
loglog(dx,fourth_order_error, 'b')
xlabel('dx')
ylabel('error')
legend('first order error', 'Second order error' , 'Fourth order error')
title('Comparison Of Errors in first second and fourth order approximations')
OBTAINED RESULT:
CONCLUSIONS:
- The errors of Fourth-order approximation Scheme is lower than the other methods and First-order approximation is higher than the rest.
- In all three subsequent scheme, we can observe that when the value of dx increases the value of error in all scheme increases.
- The first order errors seem to have the highest error. When flow physics becomes complex usage of fourth-order approximation might get complex.