COMPARISON OF ERROR VALUE FOR HIGHER ORDER APPROXIMATIONS AND LOWER ORDER APPROXIMATIONS OF DERIVATIVES USING SKEWED AND CENTRAL DIFFERENCE SCHEMES
To solve:
Solve the fourth order approximation of second order derivative for function exp(x) * cos(x) using skewed left handed , right - handed and symmetrical central difference scheme
- These are the values of the coefficients of 6 point scheme(including from f(x)) that are derived using taylors table method.
%Right_sided_diference_variables
A=[1 1 1 1 1 1;0 1 2 3 4 5;0 1/2 2 3^2/2 4^2/2 5^2/2;0 1/6 2^3/6 3^3/6 4^3/6 5^3/6; 0 1/24 2^4/24 3^4/24 4^4/24 5^4/24;0 1/120 2^5/120 3^5/120 4^5/120 5^5/120];
B=[0;0;1;0;0;0];
t=A\B;
%left_sided_difference_variables
C=[1 1 1 1 1 1;5 4 3 2 1 0;25/2 16/2 9/2 4/2 1/2 0;125/6 64/6 27/6 8/6 1/6 0;625/24 256/24 81/24 16/24 1/24 0; 3125/120 1024/120 243/120 32/120 1/120 0];
D=[0;0;1;0;0;0];
C\D;
%central_difference_variables
E=[1 1 1 1 1;-2 -1 0 1 2;2^2/2 1/2 0 1/2 2^2/2;-2^3/6 -1/6 0 1/6 2^3/6;2^4/24 1/24 0 1/24 2^4/24];
F=[0;0;1;0;0];
E\F;
- Now these values are used to write the program of fourth order approximations of second order derivative as below.
- Functions are used to call the approximations in for loop without complication of writing them as a whole program
f(x)=e_x=exp(x)*cos(x) is written to avoid parsing errors and sign errors.
close all
clear all
clc
x=1;
dx=linspace(1,2 ,10);
%function that needs to be derivated
%functions are used to avoid complication of writing the whole syntax in one program
function out =e_x(x)
out=exp(x)*cos(x)
endfunction
%right skewed scheme and error
function right_sided_difference_error = right_difference_approximation(x,dx)
%analytical derivative
analytical_derivative=-2*(exp(x))*sin(x)
%numerical discretization
right_sided_difference=(3.75*(e_x(x))-12.83*(e_x(x+(dx)))+17.83*(e_x(x+(2*dx)))...
-13*(e_x(x+(3*dx)))+5.08*(e_x(x+(4*dx)))-0.833*(e_x(x+(5*dx))))/(dx^2);
right_sided_difference_error=abs(analytical_derivative-right_sided_difference);
end
% left skewed scheme and error
function left_sided_difference_error = left_difference_approximation(x,dx)
analytical_derivative=-2*(exp(x))*sin(x);
left_sided_difference=(-0.833*(e_x(x-(5*dx)))+(5.08*e_x(x-(4*dx)))-13*(e_x(x-(3*dx)))...
+17.8*(e_x(x-(2*dx)))-12.8*(e_x(x-(dx)))+3.78*(e_x(x)))/(dx^2);
left_sided_difference_error=abs(analytical_derivative-left_sided_difference);
end
%central difference scheme and error
function central_difference_error=central_difference_approximation(x,dx)
%analytical derivative
analytical_derivative=-2*(exp(x))*sin(x);
%numerical discretization
central_difference =(-0.083*(e_x(x-(2*dx)))+1.33*(e_x(x-(dx)))-2.5*(e_x(x))...
+1.33*(e_x(x+(dx)))-0.083*(e_x(x+(2*dx))))/(dx^2);
central_difference_error=abs(analytical_derivative-central_difference);
end
for i= 1:length(dx)
central_difference_error(i)=central_difference_approximation(x,dx(i));
right_sided_difference_error(i)=right_difference_approximation(x,dx(i));
left_sided_difference_error(i)=left_difference_approximation(x,dx(i));
end
loglog(dx,central_difference_error,'y')
hold on
loglog(dx,right_sided_difference_error,'k')
hold on
loglog(dx,left_sided_difference_error,'b')
hold off
xlabel('dx')
ylabel('error')
legend('central difference error','right sided difference error','left sided difference error')
- plot appears to be as following :
- Central difference scheme is lowest error value approximation scheme due to its information from both the sides.
- left difference approximation gave lower error value compared to right differnce scheme Comparsion of same function with first order derivative first order approximations and second order derivative fourth order approxiamtions as follows:
Code
clear all
close all
clc
%comparsion of error between the second order derivative fourth order approximations...
% and first order derivative first order approxiamtions
x=1;
dx=linspace(1,2,10);
for i=1:length(dx)
%first order approxiamtions
%analytical derivative
analytical_derivative(i)=-2*exp(x)*sin(x)
%numerical derivative(forward difference)
forward_difference(i)=((exp(x+dx(i))*cos(x+dx(i)))-(exp(x)*cos(x)))/(dx(i))
forward_difference_error(i)=abs(analytical_derivative(i)-forward_difference(i))
%numerical derivative(backward difference)
backward_difference(i)=((exp(x)*cos(x))-(exp(x-dx(i))*cos(x-dx(i))))/(dx(i))
backward_difference_error(i)=abs(analytical_derivative(i)-backward_difference(i))
%second order derivative-fourth order approximations
%numerical derivative(right skewed scheme)
right_sided_difference(i)=((3.75*(exp(x)*cos(x)))-(12.83*((exp(x+dx(i))*cos(x+dx(i)))))...
+(17.83*((exp(x+(2*dx(i)))*cos(x+(2*dx(i))))))-(13*((exp(x+(3*dx(i)))*cos(x+(3*dx(i))))))...
+(5.08*((exp(x+(4*dx(i)))*cos(x+(4*dx(i))))))-(0.83*((exp(x+(5*dx(i)))*cos(x+(5*dx(i)))))))*(1/((dx(i))^2))
right_sided_difference_error(i)=abs(analytical_derivative(i)-right_sided_difference(i))
%numerical derivative(left skewed scheme)
left_sided_difference(i)=((-0.83*(exp(x-(5*dx(i)))*cos(x-(5*dx(i)))))+(5.08*(exp(x-(4*dx(i)))*cos(x-(4*dx(i)))))-(13*(exp(x-(3*dx(i)))*cos(x-(3*dx(i)))))...
+(17.8*(exp(x-(2*dx(i)))*cos(x-(2*dx(i)))))-(12.8*(exp(x-dx(i))*cos(x-dx(i))))+(3.78*(exp(x)*cos(x))))*(1/((dx(i))^2))
left_sided_difference_error(i)=abs(analytical_derivative(i) - left_sided_difference(i));
%numerical derivative(central difference scheme)
central_difference(i)=((-0.083*(exp(x-(2*dx(i)))*cos(x-(2*dx(i)))))+(1.33*(exp(x-dx(i))*cos(x-dx(i))))
-(2.5*(exp(x)*cos(x)))+(1.33*exp(x+dx(i))*cos(x+dx(i)))-(0.083*exp(x+(2*dx(i)))*cos(x+(2*dx(i)))))*(1/((dx(i))^2))
central_difference_error(i)=abs(analytical_derivative(i) - central_difference(i));
end
loglog(dx,forward_difference_error)
hold on
loglog(dx,backward_difference_error)
hold on
loglog(dx,right_sided_difference_error)
hold on
loglog(dx,left_sided_difference_error)
hold on
loglog(dx,central_difference_error)
hold off
xlabel('dx')
ylabel('error')
legend('forward difference error','backward difference error','right sided difference error','left sided difference error','central difference error')
From the above plot the comparison between the error values for first order derivative with first order approximation and second order derivative with fourth order approximation is made below:
- Forward difference error,backward difference error are higher than the left skewed scheme error and central difference error for second order derivative fourth order approximation.
- Right sided scheme is giving higher values which i m unable to obtain a perception and interpretation.
- Skewed schemes are better than CD schemes due to the possibilty of calculation of approximation of any order.Where as CD schemes cant produce some order approximations for same order derivative.
- And also symmetry comes into play for central difference approximations and where as for skewed schemes no symmetry is needed and values can be obtained from anywhere.