By the help of Matlab code to calculate the error of the first and second order derivative using the taylor table method and compare the left skewed right skewed and central differencing scheme
from the above figure we can say that drop rate in error value in the central order scheme is highest among the other two right and left skewed order.
From the figure we can conclude that the error value calculated from the different type of derivative we can say that the second order derivative error has higher value compared to the first order derivative error. The forward differencing scheme error value decreases compared to the backward differencing for the given range of dx. The Left skewed , right skewed and central skewed error value is much higher and it is in the top of the graph for the same value of dx. The difference between analytical derivative and second order derivative is much higher with respect to the difference between analytical derivative and first order derivative.
1. FUNCTION TO COMPARE LEFT SKEWED ,RIGHT SKEWED , CENTRAL SKEWED WITH FDS AND BDS
clear all
close all
clc
% Input value of the calculation
x = pi/3
dx = linspace(pi/40,pi/2000,30);
for i = 1:length(dx)
error_central_scheme(i) = central_differencing_order(x,dx(i));
error_right_skewed(i) = right_skewed_order(x,dx(i));
error_left_skewed(i) = left_skewed_order(x,dx(i));
error_forward_output(i) = forward_differencing_order(x,dx(i));
error_backward_output(i) = backward_differencing_order(x,dx(i));
end
figure (7)
plot1 = loglog(dx,error_central_scheme,\'g\');
hold on
plot1 = loglog(dx,error_right_skewed,\'r\');
hold on
plot1 = loglog(dx,error_left_skewed,\'b\');
grid on
xlabel(\'dx\')
ylabel(\'different error\')
legend(\'error central scheme\',\'error right skewed\',\'error left skewed\')
saveas(plot1,\'comparison between the central,right,left order.png\');
figure(8)
plot2 = loglog(dx,error_central_scheme,\'g\');
hold on
plot2 = loglog(dx,error_right_skewed,\'r\');
hold on
plot2 = loglog(dx,error_left_skewed,\'b\');
hold on
plot2 = loglog(dx,error_forward_output,\'marker\',\'d\',\'markerfacecolor\',\'b\');
hold on
plot2 = loglog(dx,error_backward_output,\'marker\',\'o\',\'markerfacecolor\',\'g\');
grid on
xlabel(\'dx\')
ylabel(\'comparing error type\');
legend(\'central scheme\',\'right skewed\',\'left skewed\',\'forward differencing\',\'backward differencing\');
saveas(plot2,\'first and second order derivative.png\')
2. FUNCTION OF THE FORWARD DIFFERENCIN SCHEMEN
function forward_difference_scheme = forward_differencing_order(x,dx)
% Solving the taylor table for the left skewed order
% f\"(i) = (f(x+dx)-f(x))/dx
% function = exp(x).cosx
% analytical second order derivation of function = -2.exp(x).cos(x)
analytical_second_order_derivative = -2*exp(x)*cos(x);
output_forward_difference = (deriv(x+dx) - deriv(x))/dx;
forward_difference_scheme = abs(analytical_second_order_derivative - output_forward_difference)
end
3. FUNCTION TO CALCULATE THE BACKWARD DIFFERENC SCHEME
function backward_difference_scheme = backward_differencing_order(x,dx)
% Solving the taylor table for the left skewed order
% f\"(i) = (f(x)-f(x-dx))/dx
% function = exp(x).cosx
% analytical second order derivation of function = -2.exp(x).cos(x)
analytical_second_order_derivative = -2*exp(x)*cos(x);
output_backward_difference = (deriv(x) - deriv(x-dx))/dx;
backward_difference_scheme = abs(analytical_second_order_derivative - output_backward_difference)
end
4. CALCULATE THE CENTRAL DIFFERENCING SCHEME
function central_scheme = central_differencing_order(x,dx)
% Solving the taylor table for the central differencing
% numerical derivative = -0.0833.f(i-2)+1.3333.f(i+1)-2.5f(i)+1.3333f(i+1)-0.0833f(i-2)
% function = exp(x).cosx
% analytical second order derivation of function = -2.exp(x).cos(x)
analytical_second_order_derivative = -2*exp(x)*cos(x);
numerical_derivative = -0.0833*deriv(x-2*dx)+1.3333*deriv(x-dx)-2.5*deriv(x)+1.333*deriv(x+dx)-0.0833*deriv(x-2*dx);
central_scheme = abs(analytical_second_order_derivative - numerical_derivative)
end
5. CALCULATE THE LEFT SKEWED ORDER IN THE FUNCTION
function left_skewed_scheme = left_skewed_order(x,dx)
% Solving the taylor table for the left skewed order
% f\"(i) = 2.9167f(i-4)-8.6667f(i-3)+9.5f(i-2) -4.6667f(i-1)+0.9167f(i);
% function = exp(x).cosx
% analytical second order derivation of function = -2.exp(x).cos(x)
analytical_second_order_derivative = -2*exp(x)*cos(x);
left_hand_skewed = 2.9167*deriv(x-4*dx)-8.6667*deriv(x-3*dx)+9.5*deriv(x-2*dx)-4.6667*deriv(x-dx)+0.9167*deriv(x);
left_skewed_scheme = abs(analytical_second_order_derivative - left_hand_skewed)
end
6. FUNCTION TO CALCULATE THE RIGHT SKEWED ORDER SCHEME
function right_skewed_scheme = right_skewed_order(x,dx)
% Solving the taylor table for the left skewed order
% f\"(i) = 2.9167.f(i) -8.6667.f(i+1) +9.5f(i+2) -4.6667f(i+3) +0.9167f(i+4)
% function = exp(x).cosx
% analytical second order derivation of function = -2.exp(x).cos(x)
analytical_second_order_derivative = -2*exp(x)*cos(x);
right_hand_skewed = 2.9167*deriv(x)-8.6667*deriv(x+dx)+9.5*deriv(x+2*dx)-4.6667*deriv(x+3*dx)+0.9167*deriv(x+4*dx);
right_skewed_scheme = abs(analytical_second_order_derivative - right_hand_skewed)
end
6. THE GIVEN FUNCTION OF THE PROBLEM IS SAVED IN THE NEW FUNCTION
function taylor_table = deriv(a);
global dx
taylor_table = exp(a)*cos(a);
end
7. CALCULTION OF THE DIFFERENT ORDER WITH THE HELP OF THE TAYLOR TABLE METHOD
% central differencing scheme
% second order derivative Y\" = f(i-2) + f(i-1) + f(i)+ f(i+1) + f(i+2)
% taylor series f(x+h) = f(x) + hf\'(x) + h^2f\"(x)/2! + h^3.f\"\'(x)/3!+...
% f\"(x) = af(i-2)+bf(i-1)+cf(i)+df(i+1)+ef(i+2)
% e.f(i+2) = e.(f(i)+ 2.f\'(i)+ 2^2.f\"(i)/2!+ 2^3.f\"\'(i)/3!+ 2^4.f\"\"(i)/4!)
% d.f(i+1) = d.(f(i) + f\'(i) + f\"(i)/2! + f\"\'(i)/3! + f\"\"(i)/4! +......)
% c.f(i) = c.f(i)
% b.f(i-1) = b.(f(i) - f\'(i) + f\"(i)/2! - f\"\'(i)/3! + f\"\"(i)/4! +......)
% a.f(i-2) = a.(f(i)- 2.f\'(i+ 2^2.f\"(i)/2!- 2^3.f\"\'(i)/3!+ 2^4.f\"\"(i)/4!)
% f\"(x) = f(i)(a+b+c+d+e)+f\'(i)(-2a-b+0+d+2e)+f\"(i)(2a+0.5b+0+0.5d+2e)+...
% Calculate the value of a,b,c,d,e solving the equation using LHS = RHS
[A] = [1,1,1,1,1;
-2,-1,0,1,2;
2,0.5,0,0.5,2;
-4/3,-1/6,0,1/6,4/3;
2/3,1/24,0,1/24,2/3];
% B = [a;b;c;d;e]
[X] = [0;0;1;0;0];
[B] = inv(A)*X;
% numerical derivative of the second order through the CDS
% numerical derivative = -0.0833.f(i-2)+1.3333.f(i+1)-2.5f(i)+1.3333f(i+1)-0.0833f(i-2)
% right handed skewed scheme
% second order derivative Y\" = f(i)+ f(i+1) + f(i+2) + f(i+3) + f(i+4)
% taylor series f(x+h) = f(x) + hf\'(x) + h^2f\"(x)/2! + h^3.f\"\'(x)/3!+...
% f\"(x) = af(i) + bf(i+1) + cf(i+2) + df(i+3) + ef(i+4)
% e.f(i+4) = e.(f(i) + 4.f\'(i) + 4^2.f\"(i)/2! + 4^3.f\"\'(i)/3!+ 4^4.f\"\"(i)/4!
% d.f(i+3) = d.(f(i) + 3.f\'(i) + 3^2.f\"(i)/2! + 3^.3f\"\'(i)/3! + 3^4.f\"\"(i)/4!
% c.f(i+2) = c.(f(i) + 2.f\'(i)+ 2^2.f\"(i)/2!+ 2^3.f\"\'(i)/3!+ 2^4.f\"\"(i)/4!)
% b.f(i+1) = b.(f(i) + f\'(i) + f\"(i)/2! + f\"\'(i)/3! + f\"\"(i)/4! +......)
% a.f(i) = a.f(i)
% f\"(i) = (a+b+c+d+e)f(i)+f\'(i)(0+b+2c+3d+4e)+f\"(i)(0+0.5b+2c+4.5d+8e)+
% f\"\'(i)(0+1/6.b+4/3.c+9/2.d+32/3.e)+f\"\"(i)(0+1/24.b+2/3.c+27/8.d+32/3.e)
% Calculate the value of a,b,c,d,e solving the equation using LHS = RHS for
% the FDS
[P] = [1,1,1,1,1;
0,1,2,3,4;
0,0.5,2,4.5,8;
0,1/6,4/3,9/2,32/3;
0,1/24,2/3,27/8,32/3];
[Q] = [0;0;1;0;0];
[R] = inv(P)*Q;
% Solving the taylor table for the right hand skewed
% f\"(i) = 2.9167.f(i) -8.6667.f(i+1) +9.5f(i+2) -4.6667f(i+3) +0.9167f(i+4)
% left handed skewed scheme
% second order derivative Y\" = f(i)+ f(i-1) + f(i-2) + f(i-3) + f(i-4)
% taylor series f(x+h) = f(x) + hf\'(x) + h^2f\"(x)/2! + h^3.f\"\'(x)/3!+...
% f\"(x) = af(i) + bf(i+1) + cf(i+2) + df(i+3) + ef(i+4)
% a.f(i-4) = a.(f(i) - 4.f\'(i) + 4^2.f\"(i)/2! - 4^3.f\"\'(i)/3!+ 4^4.f\"\"(i)/4!
% b.f(i-3) = b.(f(i) - 3.f\'(i) + 3^2.f\"(i)/2! - 3^.3f\"\'(i)/3! + 3^4.f\"\"(i)/4!
% c.f(i-2) = c.(f(i) - 2.f\'(i)+ 2^2.f\"(i)/2!- 2^3.f\"\'(i)/3!+ 2^4.f\"\"(i)/4!)
% d.f(i-1) = d.(f(i) - f\'(i) + f\"(i)/2! - f\"\'(i)/3! + f\"\"(i)/4! +......)
% e.f(i) = e.f(i)
% f\"(i) = (a+b+c+d+e)f(i)-f\'(i)(4a+3b+2c+d+0)+f\"(i)(0+0.5d+2c+4.5b+8a)-
% f\"\'(i)(0+1/6.d+4/3.c+9/2.b+32/3.a)+f\"\"(i)(0+1/24.d+2/3.c+27/8.b+32/3.a)
% Calculate the value of a,b,c,d,e solving the equation using LHS = RHS for
% the BDS
[U] = [1,1,1,1,1;
-0,-1,-2,-3,-4;
0,0.5,2,4.5,8;
-0,-1/6,-4/3,-9/2,-32/3;
0,1/24,2/3,27/8,32/3];
[V] = [0;0;1;0;0];
[W] = inv(U)*V;
% Solving the taylor table for the left hand skewed
% f\"(i) = 2.9167f(i-4)-8.6667f(i-3)+9.5f(i-2) -4.6667f(i-1)+0.9167f(i);