Week 3.5 - Deriving 4th order approximation of a 2nd order derivative using Taylor Table method
close all
clear all
clc
x=pi/6;
%Analyal function
Analytical_Function=exp(x)*(cos(x)-sin(x));
[dx]=linspace(pi/40,pi/400,10);
for i=1:length(dx)
Fourth_Order_CD_Approximation= -0.083*f(x-2*dx(i))+1.33*f(x-dx(i))-2.5*f(x)+1.33*f(x+dx(i))-0.083*f(x+(2*dx(i)));
Fourth_Order_CD_Error(i)=abs(Fourth_Order_CD_Approximation-Analytical_Function);
Fourth_Order_RS_Approximation= 1.64787*f(x)-4.1173*f(x+dx(i))+3.4652*f(x+2*dx(i))-1.1705*f(x+3*dx(i))+0.1751*f(x+4*dx(i))-0.0005*f(x+5*dx(i));
Fourth_Order_RS_Error(i)=abs(Fourth_Order_RS_Approximation-Analytical_Function);
Fourth_Order_LS_Approximation= 1.64787*f(x)-4.1173*f(x+dx(i))+3.4652*f(x+2*dx(i))-1.1705*f(x+3*dx(i))+0.1751*f(x+4*dx(i))-0.0005*f(x+5*dx(i));
Fourth_Order_LS_Error(i)=abs(Fourth_Order_LS_Approximation-Analytical_Function);
end
figure(1)
plot(dx,Fourth_Order_CD_Error,'*');
hold on
plot(dx,Fourth_Order_RS_Error,'g');
hold on
plot(dx,Fourth_Order_LS_Error,'o');
hold off;
legend('Centre difference Error','Skewed right-sided difference Error','Skewed left-sided difference Error');
xlabel('dx');
ylabel('Error');
title('Error comparison');
The following approximations were derived and the final equations are as follows:
1. Centre difference : -0.083f(i-2)+1.33f(i-1)-2.5f(i)+1.33f(i+1)-0.083f(i+2)
2. Skewed right-sided difference: 1.6478f(i)-4.1173f(i+1)+3.4652f(i+2)-1.705f(i+3)+0.1751f(i+4)-0.0005f(i+5)
3. Skewed left-sided difference : 1.6478f(i)-4.1173f(i+1)+3.4652f(i+2)-1.1705f(i+3)+0.1751f(i+4)-0.0005f(i+5)
Skewed right-sided difference and Skewed left-sided difference are same.
i) The order of accuracy P = Number of nodes in the stencil N - order of derivative Q + 1 for symmetric/central schemes
ie, 5-2+1 = 4
ii) The order of accuracy P = Number of nodes in the stencil N - order of derivative Q for one-sided/skewed schemes
ie, 6-2 =4
In the program, function is used for the basic function calling.
function sub = f(x)
sub = exp(x)*cos(x);
end
analytical function is calculated using the formula Analytical_Function=exp(x)*(cos(x)-sin(x));
dx is taken in between pi/40 and pi/400 as 10 divisions.
using the above mentioned formual approximations are calculated and the difference from analytical function is taken as the error. The comparison of error is ploted.
It was understood that, skewed approximations have low value of error as compared to central differencing.
while calculating the approximations, one can use maximum number of stencil nodes as the last nodes are nearer to zero and accuracy is good. it is not possible in centre differencing