Week 3 - Taylor table method and Matlab code
Taylor Table method and Matlab code
Objective:
- Derive the Fourth order approximation of the second order derivatives using Central difference scheme,Skewed right sided difference schemeSkewed left sided difference scheme
- Prove that they are fourth order accurate
- A program to evaluate the second-order derivative of the analytical function exp(x)*cos(x) and compare it with the 3 numerical approximations.
- Plot the absolute errors of 3 approximations.
- Explain the use of skewed schemes and their benefit over Central difference schemes.
Solution:
- Central Difference Scheme
Number of nodes = p+q-1
Where p=order of approximations
q= order of derivative
Here number of nodes = 4+2-1=5
Therefore the central difference scheme equation is
Now let’s form a Taylor’s table,
The above equation proves that the central difference scheme is fourth order accurate delx^4.
The values of a,b,c,d,e are as derived using the following script:
%Matrix solving for cds
A1=[1 -2 4/2 -8/6 16/24 ;1 -1 1/2 -1/6 1/24 ;1 0 0 0 0 ;1 1 1/2 1/6 1/24 ;1 2 4/2 8/6 16/24];
B1=A1';
C1=[0;0;1;0;0];
X1=B1\C1;
Output:
Substituting the coefficients of the Taylor’s series terms after solving the matrix,
- Skewed right sided difference scheme
Number of nodes = p+q
Where p=order of approximations
q= order of derivative
Here number of nodes = 4+2=6
Now let's form a Taylor’s table.
The above equation is fourth order accurate for skewed right sided difference schemes.
The values of a,b,c,d,e are as derived using the following script:
%Matrix solving for srds
A2=[1 0 0 0 0 0; 1 1 1/2 1/6 1/24 1/120; 1 2 2 8/6 16/24 32/120; 1 3 9/2 27/6 81/24 243/120; 1 4 8 64/6 256/24 1024/120; 1 5 25/2 125/6 625/24 3125/120];
B2=A2';
C2=[0;0;1;0;0;0];
X2=B2\C2;
Output:
- Skewed left sided difference scheme
Number of nodes = p+q
Where p=order of approximations
q= order of derivative
Here number of nodes = 4+2=6
Here,
Now lets construct the Taylor's Table,
The above equation is fourth order accurate for skewed right sided difference schemes.
The values of a,b,c,d,e are as derived using the following script:
%Matrix solving for slds
A3=[1 -5 25/2 -125/6 625/24 -3125/120; 1 -4 8 -64/6 256/24 -1024/120;1 -3 9/2 -27/6 81/24 -243/120; 1 -2 2 -8/6 16/24 -32/120; 1 -1 1/2 -1/6 1/24 -1/120; 1 0 0 0 0 0 ];
B3=A3';
C3=[0;0;1;0;0;0];
X3=inv(B3)*C3;
Output:
Program:
Main Program to plot dx and error across all the 3 schemes:
clear all;
close all;
clc;
x=pi/3;
dx=linspace(pi/4,pi/400,100);
for i=1:length(dx)
error1(i)=cds(x,dx(i));
error2(i)=srds(x,dx(i));
error3(i)=slds(x,dx(i));
end
loglog(dx,error1,'g','Linewidth',3);
hold on
loglog(dx,error2,'b','Linewidth',3);
hold on
loglog(dx,error3,'r','Linewidth',3);
xlabel('dx')
ylabel('Truncation error')
title('Truncation error vs dx')
legend('cds','srds','slds')
Function Program for Central Difference Scheme:
function error1=cds(x,dx)
%analytical function=exp(x)*cos(x);
%analytical_derivative=-2*exp(x)*sin(x);
f2= -2*exp(x)*sin(x);
%Matrix solving for cds
A1=[1 -2 4/2 -8/6 16/24 ;1 -1 1/2 -1/6 1/24 ;1 0 0 0 0 ;1 1 1/2 1/6 1/24 ;1 2 4/2 8/6 16/24];
B1=A1';
C1=[0;0;1;0;0];
X1=B1\C1;
cds= (X1(1)*exp(x-(2*dx))*cos(x-(2*dx))+X1(2)*exp(x-(1*dx))*cos(x-(1*dx))+X1(3)*exp(x)*cos(x)+X1(4)*exp(x+(1*dx))*cos(x+(1*dx))+X1(5)*exp(x+(2*dx))*cos(x+(2*dx)))/(dx)^2;
error1=abs(cds-f2);
end
Function Program for Skewed Right Difference Scheme:
function error2=srds(x,dx)
%analytical function=exp(x)*cos(x);
%analytical_derivative=-2*exp(x)*sin(x);
f2= -2*exp(x)*sin(x);
%Matrix solving for srds
A2=[1 0 0 0 0 0; 1 1 1/2 1/6 1/24 1/120; 1 2 2 8/6 16/24 32/120; 1 3 9/2 27/6 81/24 243/120; 1 4 8 64/6 256/24 1024/120; 1 5 25/2 125/6 625/24 3125/120];
B2=A2';
C2=[0;0;1;0;0;0];
X2=inv(B2)*C2;
srds=(X2(1)*exp(x)*cos(x)+X2(2)*exp(x+1*dx)*cos(x+1*dx)+X2(3)*exp(x+2*dx)*cos(x+2*dx)+X2(4)*exp(x+3*dx)*cos(x+3*dx)+X2(5)*exp(x+4*dx)*cos(x+4*dx)+X2(6)*exp(x+5*dx)*cos(x+5*dx))/(dx)^2;
error2=abs(srds-f2);
end
Function Program for Skewed Left Difference Scheme:
function error3=slds(x,dx)
f2= -2*exp(x)*sin(x);
%Matrix solving for slds
A3=[1 -5 25/2 -125/6 625/24 -3125/120; 1 -4 8 -64/6 256/24 -1024/120;1 -3 9/2 -27/6 81/24 -243/120; 1 -2 2 -8/6 16/24 -32/120; 1 -1 1/2 -1/6 1/24 -1/120; 1 0 0 0 0 0 ];
B3=A3';
C3=[0;0;1;0;0;0];
X3=inv(B3)*C3;
slds=(X3(1)* exp(x-5*dx)*cos(x-5*dx)+X3(2)*exp(x-4*dx)*cos(x-4*dx)+X3(3)*exp(x-3*dx)*cos(x-3*dx) +X3(4) *exp(x-2*dx)*cos(x-2*dx)+X3(5) *exp(x-1*dx)*cos(x-1*dx)+X3(6)*exp(x)*cos(x))/(dx)^2;
error3=abs(slds-f2);
end
Result:
Inference:
- All the schemes are fourth order accurate which is proved in the end of all 3 derivations
- From the above plot, it is evident that the central difference scheme has the least truncation error.The reason for that is it takes information from both the sides.
- Skewed Schemes both right and left had similar amounts of error at the initial points whereas as dx increased, the error of the skewed left sided scheme dropped below the central scheme and raised again significantly. This may be due to the round off error which arises when the dx is increased.
Why Skewed schemes helpful?
Whenever there is no information about the left and right side nodes, skewed schemes come to the rescue. Skewed right sided scheme can be used whenever there is no information about the left nodes and vice versa. This is the advantage skewed schemes have over central difference schemes at the trade off of truncation error.