Project

Deriving the fourth-order approximation for second-order derivative through the central skewed right and skewed left differencing schemes Subsequently evaluating their errors

Title: Deriving the fourth-order approximation for second-order derivative through the central, skewed right and skewed left differencing schemes. Subsequently evaluating their errors

Function: `exp(x)*cos(x)`

Firstly, Central differencing derivation through Taylor table

\"\"

Here You get 5 equations and we have 5 unknowns to find out. So, consider the above case as a matrix `AX=B`where A is the coefficient matrix while X is a column matrix of unknowns and the resulting B is the column matrix of the resulting values

X= inv (A) * B

through this, we get a set of values for X. For further simplification, we need to convert this decimal values into fractional values. For this, I used rats function

Consequently, I arrived with the second-order differential equation through central differencing of `exp(x)*cos(x)`

`((-(exp(x-(2*dx(i)))*sin(x-(2*dx(i)))))+ (16*(exp(x-dx(i))*sin(x-dx(i))))-(30*exp(x)*sin(x))+(16*exp(x+dx(i))*sin(x+dx(i)))-(exp(x-(2*dx(i)))*sin(x-(2*dx(i)))))/(12*((dx(i))^2))`

Skewed Right: No of Stencils here are \'6\' as the nodes is equals to the sum of order of differentiation(2) and order of accuracy required(4)

\"\"

Similarly, I arrived with the second-order differential equation through skewed right differencing of `exp(x)*cos(x)`

`((3.75*(exp(x)*sin(x)))-(12.833*exp(x+dx(i))*sin(x+dx(i)))+(17.833*exp(x+(2*dx(i)))*sin(x+(2*dx(i))))-(13*exp(x+(3*dx(i)))*sin(x+(3*dx(i))))+(5.08*exp(x+(4*dx(i)))*sin(x+(4*dx(i))))-(0.833*exp(x+(5*dx(i)))*sin(x+(5*dx(i)))))/(dx(i))^2`

Skewed Left: No.of Stencils 6 in reverse direction till f(i-5)

\"\"

Similarly, I arrived with the second-order differential equation through central differencing of `exp(x)*cos(x)`

`((3.75*(exp(x)*sin(x)))-(12.833*(exp(x-dx(i))*sin(x-dx(i))))+(17.833*exp(x-(2*dx(i)))*sin(x-(2*dx(i))))-(13*exp(x-(3*dx(i)))*sin(x-(3*dx(i))))+(5.08*exp(x-(4*dx(i)))*sin(x-(4*dx(i))))-(0.833*exp(x-(5*dx(i)))*sin(x-(5*dx(i)))))/((dx(i))^2)`

Then a for loop is used to simulate the data for the above three cases

Input variables: `X=pi/4` while dx has taken from pi/10 to pi/250

Analytical derivative is conducted and stored it

`Analytical_derivative = -2*exp(x)*sin(x);`

Error has been found out through the difference of specified derivative and the analytical error

Plotting of graph

A log-log Graph has been plotted in between dx and error for three scenarios

Results

\"\"

from the first graph, we can conclude that central differencing error is more compared to the other two graphs. While both the other error patterns follow the same trend due to the \"increase in the number of stencils\". Yet the skewed left is more precise over skewed right

Coming to the second graph FDS and BDS though they are in the better pattern as the value gets increased they will surpass the other errors.

clear all
close all
clc

%solving equations to get constants for  f\"(x) through central differencing = a*f(i-2)+b*f(i-1)+c*f(i)+d*f(i+1)+e*f(i+2)
%Constants for central differencing for fourth order approximation
A=[1 1 1 1 1; -2 -1 0 1 2; 2 1/2 0 1/2 2; -8/6 -1/6 0 1/6 8/6; 16/24 1/24 0 1/24 16/24];
B=[0;0;1;0;0];
Const_1=inv(A)*B;
Coeff_1= rats(Const_1\')
% cant able to generate the func Central_Differencing_Equation = @(x,dx)((Const_1(5))*(f(x+2*dx)))+((Const_1(2))*(f(x-dx)))+((Const_1(3))*(f(x)))+((Const_1(4))*(f(x+dx)))+((Const_1(5))*(f(x+2*dx)))

%solving equations to get constants for  f\"(x) through Skewed right differencing = a*f(i-2)+b*f(i-1)+c*f(i)+d*f(i+1)+e*f(i+2)
%Constants for Skewed right differencing for fourth order approximation
C=[1 1 1 1 1 1; 0 1 2 3 4 5; 0 1/2 4/2 9/2 16/2 25/2;0 1/6 8/6 27/6 64/6 125/6; 0 1/24 16/24 81/24 256/24 625/24;0 1/120 32/120 243/120 1024/120 3125/120 ];
D=[0;0;1;0;0;0];
Const_2=inv(C)*D;
Coeff_2= rats(Const_2\')

%solving equations to get constants for  f\"(x) through Skewed left differencing = a*f(i-2)+b*f(i-1)+c*f(i)+d*f(i+1)+e*f(i+2)
%Constants for Skewed Left differencing for fourth order approximation
E=[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];
F=[0;0;1;0;0;0];
Const_3=inv(E)*F;
Coeff_3= rats(Const_3\')

x=pi/4;
dx = linspace(pi/10,pi/250,10);

%Analytical Derivative of function exp(x)*cos(x) 

Analytical_derivative = -2*exp(x)*sin(x);

for i= 1:length(dx)
  
%first order approximation through forward differencing = (f(x+dx)-f(x))/dx

Central_Differencing_Equation(i)= ((-(exp(x-(2*dx(i)))*sin(x-(2*dx(i)))))+ (16*(exp(x-dx(i))*sin(x-dx(i))))-(30*exp(x)*sin(x))+(16*exp(x+dx(i))*sin(x+dx(i)))-(exp(x-(2*dx(i)))*sin(x-(2*dx(i)))))/(12*((dx(i))^2))

%Error in Central_Differecing

Central_Differecing_error = abs(Central_Differencing_Equation - Analytical_derivative)

%second order approximation through central differencing = (f(x+dx)-f(x))/dx

Skewed_right(i)= ((3.75*(exp(x)*sin(x)))-(12.833*exp(x+dx(i))*sin(x+dx(i)))+(17.833*exp(x+(2*dx(i)))*sin(x+(2*dx(i))))-(13*exp(x+(3*dx(i)))*sin(x+(3*dx(i))))+(5.08*exp(x+(4*dx(i)))*sin(x+(4*dx(i))))-(0.833*exp(x+(5*dx(i)))*sin(x+(5*dx(i)))))/(dx(i))^2

%Error in second order approximation

Skewed_right_error = abs(Skewed_right - Analytical_derivative)

%Fourth order approximation 

Skewed_left(i) = ((3.75*(exp(x)*sin(x)))-(12.833*(exp(x-dx(i))*sin(x-dx(i))))+(17.833*exp(x-(2*dx(i)))*sin(x-(2*dx(i))))-(13*exp(x-(3*dx(i)))*sin(x-(3*dx(i))))+(5.08*exp(x-(4*dx(i)))*sin(x-(4*dx(i))))-(0.833*exp(x-(5*dx(i)))*sin(x-(5*dx(i)))))/((dx(i))^2)

%Error in fourth order approximation

Skewed_left_error = abs(Skewed_left - Analytical_derivative)

%first order approximation through forward differencing = (f(x+2*dx)-2*f(x+dx)+f(x))/dx^2

Forward_differencing(i)= ((exp(x+(2*dx(i)))*cos(x+(2*dx(i)))) - (2*(exp(x+(dx(i)))*cos(x+(dx(i)))))+(exp(x)*cos(x)))/((dx(i))^2)

%Error in first order approximation

Forward_error = abs(Forward_differencing- Analytical_derivative)

%first order approximation through backward differencing = (f(x-2*dx)-2*f(x-dx)+f(x))/dx^2

Backward_differencing(i)= ((exp(x-(2*dx(i)))*cos(x-(2*dx(i)))) - (2*(exp(x-(dx(i)))*cos(x-(dx(i)))))+(exp(x)*cos(x)))/((dx(i))^2)

%Error in this process

Backward_differencing_error = abs(Backward_differencing - Analytical_derivative)


end

%Line diagram plotting at this specific x 

subplot(2,1,1)
plot(dx,Central_Differecing_error,\'color\',\'b\')
hold on
loglog(dx,Skewed_right_error,\'color\',\'y\')
hold on
loglog(dx,Skewed_left_error,\'color\',\'r\')
xlabel(\'log(dx)\')
ylabel(\'log(error)\')
legend(\'Central Differecing error\',\'Skewed right error\',\'Skewed left error\')
title(\'Comparison of central skewed right and skewed left error\')

subplot(2,1,2)
plot(dx,Central_Differecing_error,\'color\',\'b\')
hold on
loglog(dx,Skewed_right_error,\'color\',\'y\')
hold on
loglog(dx,Skewed_left_error,\'color\',\'r\')
hold on
loglog(dx,Forward_error,\'color\',\'g\')
hold on
loglog(dx,Backward_differencing_error,\'color\',\'m\')
xlabel(\'log(dx)\')
ylabel(\'log(error)\')
legend(\'Central Differecing error\',\'Skewed right error\',\'Skewed left error\',\'Forward differencing error\',\'backward differencing error\')
title(\'Comparison of central skewed right and skewed left error\')



Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.