To derive central difference skewed right difference and skewed left difference scheme for fourth order approximation of second order derivative by using Taylor table of finite difference method
Aim:-The aim of this project,is to derive central difference,skewed right difference and skewed left difference scheme for fourth order approximation of second order derivative by using, Taylor table of finite difference method.
1.Central difference:-To derive central difference for fourth order approximation of second order derivative by Taylor table method of finite difference method by using equation given below and with help of fig(a):-
Fig(a):-Central difference for fourth order approximation
Second order derivative of fourth order approximation for CDS is:-
Taylor table for Central difference of fourth order approximation:-
Following MATLAB code is written to find value of above coefficients:-
clear all
close all
clc
%Finding coefficient for above table;
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=[0;0;1;0;0];
C=inv(A)*B
Output:-On solving above MATLAB code we get coefficients as follows
a= -0.0833
b=1.3333
c= -2.50
d= -1.3333
e= -0.0833
MATLAB CODE:-In this program,we have compute absolute error which is basically the difference of Central difference and second order analytical derivative and then by using function out we are calling this function in the main program to compare with other schemes.
function out=central_difference(x,dx)
%Function=exp(x)*cos(x);
%Analytical_derivative;
%Second_order_analytical_derivative;
Second_order_analytical_derivative=-2*sin(x)*exp(x);
%Numerical_derivative;
%Fourth_order_approximation;
central_difference=(-0.0833*exp(x-2*dx)*cos(x-2*dx)+1.3333*exp(x-dx)*cos(x-dx)-2.50*exp(x)*cos(x)+1.3333*exp(x+dx)*cos(x+dx)-0.0833*exp(x+2*dx)*cos(x+2*dx))/dx^2;
out=abs(central_difference-Second_order_analytical_derivative);
end
2.Skewed right side difference:-To derive skewed right side difference for fourth order approximation of second order derivative by Taylor table method of finite difference method by using equation given below and with help of fig(b):-
Fig(b):-Skewed right side difference for fourth order approximation
Second order derivative of fourth order approximation for SRS is:-
Taylor table for skewed right side difference of fourth order approximation:-
Following MATLAB code is written to find value of above coefficients:-
clear all
close all
clc
%Finding coefficient for above table;
A=[1 1 1 1 1;0 1 2 3 4;0 0.5 4/2 9/2 16/2;0 1/6 8/6 27/6 64/6;0 1/24 16/24 81/24 256/24];
B=[0;0;1;0;0];
C=inv(A)*B
Output:-On solving above MATLAB code we get coefficients as follows
a= 2.9166
b=-8.6666
c= 9.500
d= -4.666
e= 0.9166
MATLAB CODE:-In this program,we have compute absolute error which is basically the difference of skewed right side difference and second order analytical derivative and then by using function out we are calling this function in the main program to compare with other schemes.
function out=skewed_right_side(x,dx)
%Function=exp(x)*cos(x);
%Analytical_derivative;
%Second_order_analytical_derivative;
second_order_analytical_derivative=-2*sin(x)*exp(x);
%Numerical_derivative;
%Fourth_order_approximation;
skewed_right_side=(2.9166*exp(x)*cos(x)-8.6666*exp(x+dx)*cos(x+dx)+9.500*exp(x+2*dx)*cos(x+2*dx)
-4.666*exp(x+3*dx)*cos(x+3*dx)+0.9166*exp(x+4*dx)*cos(x+4*dx))/(dx^2);
out=abs(skewed_right_side-second_order_analytical_derivative);
end
3.Skewed left side difference:-To derive skewed left side difference for fourth order approximation of second order derivative by Taylor table method of finite difference method by using equation given below and with help of fig(c):-
Fig(c):-Skewed left side difference for fourth order approximation
Second order derivative of fourth order approximation for SLS is:-
Taylor table for skewed right side difference of fourth order approximation:-
Following MATLAB code is written to find value of above coefficients:-
clear all
close all
clc
%Finding coefficient for above table;
A=[1 1 1 1 1;0 -1 -2 -3 -4;0 0.5 4/2 9/2 16/2;0 -1/6 -8/6 -27/6 -64/6;0 1/24 16/24 81/24 256/24];
B=[0;0;1;0;0];
C=inv(A)*B
Output:-On solving above MATLAB code we get coefficients as follows
a= 2.9166
b=-8.6666
c= 9.500
d= -4.666
e= 0.9166
MATLAB CODE:-In this program,we have compute absolute error which is basically the difference of skewed left side difference and second order analytical derivative and then by using function out we are calling this function in the main program to compare with other schemes.
function out=skewed_left_side(x,dx)
%Function=exp(x)*cos(x);
%Analytical_derivative;
%Second_order_analytical_derivative;
second_order_analytical_derivative=-2*sin(x)*exp(x);
%Numerical_derivative;
%Fourth_order_approximation;
skewed_left_side=(2.9166*exp(x)*cos(x)-8.6666*exp(x-dx)*cos(x-dx)+9.500*exp(x-2*dx)*cos(x-2*dx)-4.666*exp(x-3*dx)*cos(x-3*dx)+0.9166*exp(x-4*dx)*cos(x-4*dx))/dx^2;
out=abs(skewed_left_side-second_order_analytical_derivative);
end
MAIN PROGRAM:-In this program we have called all above schemes program to compare each other at the same value of x and range of dx.And to see which scheme gives less error.
clear all
close all
clc
%Input parameter
x=pi/3;
dx=linspace(pi/4,pi/4000,80);
%Function=exp(x)*cos(x);
%Analytical_derivative;
%Second_order_analytical_derivative;
second_order_analytical_derivative=-2*sin(x)*exp(x);
%For loop;
for i=1:length(dx)
central_difference_error(i)=central_difference(x,dx(i));
skewed_right_side_error(i)=skewed_right_side(x,dx(i));
skewed_left_side_error(i)=skewed_left_side(x,dx(i));
end
%Plotting;
figure(1)
loglog(dx,central_difference_error,\'r\')
hold on
loglog(dx,skewed_right_side_error,\'g\')
hold on
loglog(dx,skewed_left_side_error,\'b\')
%Labeling;
xlabel(\'dx\')
ylabel(\'error\')
legend(\'central_difference_error\',\'skewed_right_side_error\',\'skewed_left_side_error\')
Plot:-Comapring absolute error of central diiference scheme,skewed right side difference scheme and skewed left side difference scheme.
Conclusion:- By observing above plot we can conclude following observation which are as follows
- Skewed right gives maximum error or in otherwords we can say that its unstable as compared to other scheme.
- Central diference gives lowest error or in otherwords we can say that its stable as compared to other scheme.
- Skewed left gives error somewhere in between skewed right difference and central diference.
Now,Although central difference gives less error as compared to skewed right and left side difference,but in some cases skewed difference scheme is more useful as compared to central difference.To learn in detail let see an example.
- If we want calculate error at boundary,we cannot apply central diiference sheme since it require value from both end so,in this case we can apply skewed diference scheme
- Also,we can calculate error at boundary by using forward difference scheme or backward difference scheme,but it wont be good decision because FDS and BDS has low order of approximation so it would be have more error.Therefore,skewed difference scheme is prefer for such case.