absolute error comparison of different schemes
4th order approximations of 2nd order derivatives
1) central difference:
2) Skewed right-sided difference:
3)skewed left-sided difference:
code for the absolute error in different schemes
% fourth-order approximations% given function
% f (x) (exp (x) * cos (x));
clc
close all
clear all
x=pi / 3;
dx=linspace (pi / 40, pi / 4000, 20);
analytical_derivative =(-2) * exp (x) * sin (x);
for i= 1:length(dx)
central_differencing(i) =((-2.5*exp(x)*cos(x))+(1.334*exp(x-dx(i))*cos(x-dx(i)))-(0.0833*exp(x-(2*dx(i)))*cos(x-(2*dx(i))))+(1.334*exp(x+dx(i))*cos(x+dx(i)))-(0.0833*exp(x+(2*dx(i)))*cos(x+(2*dx(i)))))/(dx(i)^2);
skewed_left_differencing(i) = ((3.75*exp(x)*cos(x))-(12.833*exp(x-dx(i))*cos(x-dx(i)))+(17.833*exp(x-(2*dx(i)))*cos(x-(2*dx(i))))-(13*exp(x-(3*dx(i)))*cos(x-(3*dx(i))))+(5.0833*exp(x-(4*dx(i)))*cos(x-(4*dx(i))))-(0.833*exp(x-(5*dx(i)))*cos(x-(5*dx(i)))))/(dx(i)^2);
skewed_right_differencing(i) =((3.75*exp(x)*cos(x))-(12.833*exp(x+dx(i))*cos(x+dx(i)))+(17.833*exp(x+(2*dx(i)))*cos(x+(2*dx(i))))-(13*exp(x+(3*dx(i)))*cos(x+(3*dx(i))))+(5.0833*exp(x+(4*dx(i)))*cos(x+(4*dx(i))))-(0.833*exp(x+(5*dx(i)))*cos(x+(5*dx(i)))))/(dx(i)^2);
Forward_difference(i) =((exp(x+dx(i))*cos(x+dx(i)))-(exp(x)*cos(x)))/dx(i);
backward_difference(i)=((exp(x)*cos(x))-(exp(x-dx(i))*cos(x-dx(i))))/dx(i);
end
% error calculation
central_error = abs (analytical_derivative - central_differencing);
skewed_left_error = abs (analytical_derivative-skewed_left_differencing);
skewed_right_error = abs (analytical_derivative-skewed_right_differencing);
forward_error = abs (analytical_derivative - Forward_difference);
backward_error = abs (analytical_derivative-backward_difference);
loglog (dx, central_error, \' b\', \'linewidth\', 2)
grid on
hold on
loglog (dx, skewed_left_error, \'r\', \'linewidth\', 2)
hold on
loglog (dx, skewed_right_error, \'*g \', \'linewidth\', 2)
hold on
loglog (dx, forward_error, \'-.bls \', \'linewidth\', 2)
hold on
loglog (dx,backward_error, \'-c \', \'linewidth\', 2)
legend(\'central error \',\'skewed left error\', \'skewed right error\',\'forward error\', \'backword error\')
xlabel (\'dx values\')
ylabel (\'Error values\')
the above plot shows the absolute error difference between the central, skewed right and skewed left schemes. it is clear that central difference shows less error comparatively.
This graph shows the comparison of the above schemes with forward and backward errors. initially the error of the forward and backward are small comparatively. but as the skewed one takes the information from most of the points compared to forward or backward so the error decreases significantly. the graph explains it clearly.
skewed schemes work very well when there are no points available on the grids. i.e., if there are only nodes available on the left or right of the grid in these conditions we can use skewed schemes and the results obtained are also very accurate. if we want to compute a higher-order derivative at one of the extreme ends where central difference fails these schemes come in to play.