Taylor table method for deriving fourth order approximation for second derivative and Matlab code for the same
Derivation of fourth order approximations of the second-order derivative using :
1. Central difference
2. Skewed right-sided difference
3. Skewed left-sided difference
The value of co-efficients is obtained using matlab programming.
Everything else is as shown below:
Derivation:
Obtaining the co-efficients :
The image below shows an example of how to find the co-efficients for the second derivative fourth order of central differentiation approximation.
The values of matrix B provides the value of the co-efficients.
Approaching the program :
The function that we are given to solve is f(x) = exp(x) * cos (x)
The given function is of u*v form and the analytical derivative is given by (v(du/dx)+u(dv/dx))
Derivating it twice we get the second derivative of the function
d(exp(x)*cos(x))/dx = exp(x) (–sin(x)) + (cos(x) exp(x))
=> d^2((exp(x)*cos(x))/dx^2 = -exp(x) (cos(x)) + (-sin(x)(x) exp(x)) + exp(x) (–sin(x)) + (cos(x) exp(x))
=> d^2((exp(x)*cos(x))/dx^2 = - 2*exp(x)*cos(X)
The above equation gives us the analytical solution of the equation.
From the derivation we have derived the equtions for skewed right ,left and central difference approximations for the second order derivative.
By using those equations, we will test it out in matlab and plot the error.
Each case Central difference, Skewed right-sided difference and Skewed left-sided difference will be taken in seperate function and the error is calculated in the function itself by using, absolute valuve (analytical second derivative - differencing approximation).
The output of this equation yields the diffrence between actual solution and the solution obtained from differencing approximation method of the second derivative of fourth order.
Error from all three cases are taken and then plotted against their respective dx values in a single log graph.
For this program, we have considered 100 dx values between pi/10 and pi/100.
Program code:
clear all;
close all;
clc
x=pi/3;
%considering 400 diffrent values of dx from pi/10 to pi/2000;
dx = linspace(pi/10, pi/4000,100);
%for loops are used to get the errors for 40 different values of dx
for i = 1:length(dx)
forward_error(i) = SD_forward_fourth_order_approx(x,dx(i));
backward_error(i) = SD_backward_fourth_order_approx(x,dx(i));
central_error(i) = SD_fourth_central_order_approx(x,dx(i));
end
%Comparison between the forward and backward approximations for fourth order;
figure(1)
loglog(dx,forward_error)
hold on
loglog(dx,backward_error,'green')
%x axis containing values of dx
xlabel('dx values');
%y axis is the scale of magnitude of error
ylabel('Error');
legend('Forward differencing approximation','Backward differencing approximation');
figure(2)
loglog(dx,forward_error)
hold on
loglog(dx,backward_error,'green')
hold on
loglog(dx,central_error,'yellow')
%x axis containing values of dx
xlabel('dx values');
%y axis is the scale of magnitude of error
ylabel('Error');
legend('Forward differencing approximation','Backward differencing approximation','Central differencing approximation');
function output = SD_fourth_central_order_approx(x,dx)
%f(x) exp(x) * cos (x);
%analytical derivative is of u*v form given by (v(du/dx)+u(dv/dx));
%d(exp(x)*cos(x))/dx = exp(x) (–sin(x)) + (cos(x) exp(x))
%d^2((exp(x)*cos(x))/dx^2 = -exp(x) (cos(x)) + (-sin(x)(x) exp(x)) + exp(x) (–sin(x)) + (cos(x) exp(x))
%d^2((exp(x)*cos(x))/dx^2 = - 2*exp(x)*cos(X)
analytical_second_derivative = -2*exp(x)*sin(x);
% for numerical derivative let us consider central difference approximation
%Central differencing approximation for second derivative of fourth order differentiation is
%given by
%((-0.8333)*(f(i-2) + 1.333*f(i-1) - 2.5*f(i) + 1.33* f(i+1) - 0.8333*f(i+2))/dx^2;
f_minus_1 = (exp(x-dx))*(cos (x-dx));
f_minus_2 = (exp(x - (2*dx))) * (cos (x- (2*dx)));
f = (exp(x)) * (cos(x));
f_plus_1 = (exp(x + dx)) * (cos(x+dx) );
f_plus_2 = (exp(x + (2*dx))) * (cos (x+ (2*dx)));
difference = ((-(f_minus_2)) + (16*(f_minus_1) - (30*f) + (16*(f_plus_1)) - (f_plus_2)))/(12*dx^2);
output = abs(analytical_second_derivative - difference);
%the output of this function yields the diffrence between actual solution
%to the central difference approximation of 4th order derivative
end
function output = SD_forward_fourth_order_approx(x , dx)
%f(x) exp(x) * cos (x);
%analytical derivative is of u*v form given by (v(du/dx)+u(dv/dx));
%d(exp(x)*cos(x))/dx = exp(x) (–sin(x)) + (cos(x) exp(x))
%d^2((exp(x)*cos(x))/dx^2 = -exp(x) (cos(x)) + (-sin(x)(x) exp(x)) + exp(x) (–sin(x)) + (cos(x) exp(x))
%d^2((exp(x)*cos(x))/dx^2 = - 2*exp(x)*cos(X)
analytical_second_derivative = -(2*exp(x)*sin(x));
% for numerical derivative let us consider forward difference approximation
%Fourth order Forward differencing approximation for second order differentiation is
%given by ((3.75*f(i)) - (12.8333*f(i+1))+(17.8333*f(i+2))-(13*f(i+3)) +(5.08333*f(i+4))-(0.833*f(i+5)))/dx^2
f = (exp(x)) * (cos(x));
f_plus_1 = (exp(x + dx)) * (cos(x+dx) );
f_plus_2 = (exp(x + (2*dx))) * (cos (x+ (2*dx)));
f_plus_3 = (exp(x + (3*dx))) * (cos (x+ (3*dx)));
f_plus_4 = (exp(x + (4*dx))) * (cos (x+ (4*dx)));
f_plus_5 = (exp(x + (5*dx))) * (cos (x+ (5*dx)));
forward_difference = ((45*f) -(154*f_plus_1) + (214*f_plus_2) - (156*f_plus_3) + (61*f_plus_4) - (10*f_plus_5)) /(12*dx^2);
output = abs(analytical_second_derivative-forward_difference);
%the output of this function yields the diffrence between actual solution
%to the skewed forward difference approximation of 4th order approximation of
%seccond order derivative
end
function output = SD_backward_fourth_order_approx(x , dx)
%f(x) exp(x) * cos (x);
%analytical derivative is of u*v form given by (v(du/dx)+u(dv/dx));
%d(exp(x)*cos(x))/dx = exp(x) (–sin(x)) + (cos(x) exp(x))
%d^2((exp(x)*cos(x))/dx^2 = -exp(x) (cos(x)) + (-sin(x)(x) exp(x)) + exp(x) (–sin(x)) + (cos(x) exp(x))
%d^2((exp(x)*cos(x))/dx^2 = - 2*exp(x)*cos(X)
analytical_second_derivative = -(2*exp(x)*sin(x));
% for numerical derivative let us consider forward difference approximation
%fourth order backward differencing approximation for second order differentiation is
%given by (0.091046*f(i-5))-((0.330888*f(i-4))-(0.058691*f(i-3))+(2.250941*f(i-2))-(3.457487*f(i-1))
%+(1.50579*f(i)))/dx^2
f = (exp(x)) * (cos(x));
f_minus_1 = (exp(x - dx)) * (cos(x-dx) );
f_minus_2 = (exp(x - (2*dx))) * (cos (x- (2*dx)));
f_minus_3 = (exp(x - (3*dx))) * (cos (x- (3*dx)));
f_minus_4 = (exp(x - (4*dx))) * (cos (x- (4*dx)));
f_minus_5 = (exp(x - (5*dx))) * (cos (x- (5*dx)));
f = (exp(x)) * (cos(x));
backward_difference = ((45*f) -(154*f_minus_1) + (214*f_minus_2) - (156*f_minus_3) + (61*f_minus_4) - (10*f_minus_5)) /(12*dx^2);
output = abs(backward_difference - analytical_second_derivative);
%the output of this function yields the diffrence between actual solution
%to the skewed backward difference approximation of 4th order approximation for
%seccond order derivative
end
Plots and their analysis:
The plot below depicts the diffrence in the erro between skewed right side (forward) differencing approximation and skewed left side (backward) differencing approximation for different values of dx.
It can be observed that the error decreases with both cases as the value of dx decreases and both the curves converge until they come close at one point, virtually meaning for some value of dx both right sided and left sided approximation yields same solution, but after that point they again start diverging from one another as the value of dx continues to decresae.
The plot below depicts the diffrence in the erro between skewed right side (forward) differencing approximation , skewed left side (backward) differencing approximation and central differencing approximations for different values of dx.
The above plot shows the plot between the amplitude of error for a range of dx values. The graph is plotted on the logarithmic graph in order to get the complete view. It is observed that the error of central differencing is less compared to skewed left and right differencing approximation for same corresponding values of dx.
It can be concluded that the higher the order of derivative lesser will be the error and taking this case, central order approximation seems like the better fit as it gives les errors.
But the skewed left and right system comes in use when we have the direction of quantity. We can obtain accurate solution using skewed left or right system depending on the flow of direction compared to centraldifferencing approximation.