Week 3 - Taylor table method and Matlab code
AIM/OBJECTIVE :
The main objective of the following program is to find the intensity of error with respect to the actual or the analytical value. In order to communicate this in a precise manner, we take the help of central , skewed left and skewed right differencing methods.
NUMERICAL STENCIL:
It is defined as a boundary or finite points within which the error is calculated.
It has three methods,
Central differencing - The values are symmetric
Skewed Left - The values are towards left
Skewed Right - The values are towards right.
CENTRAL DIFFERENCING:
Number of nodes = p + q - 1
P - Order of derivative (2)
q - Order of Approximation (4)
So the number of nodes for central Differencing = 2+4-1 = 5
To find the coefficients a,b,c,d,e,
The A matrix is,
AC = B
C = [a b c d e f]'
C = inv(A)*B
SKEWED RIGHT SCHEME
Number of = p + q
P - Order of derivative (2)
q - Order of Approximation (4)
So the number of nodes = 2*4 = 6
The A matrix is ,
AC = B
C = [a b c d e f]'
C = inv(A)*B
SKEWED LEFT SCHEME:
Number of = p + q
P - Order of derivative (2)
q - Order of Approximation (4)
So the number of nodes = 2*4 = 6
The A matrix is,
AC = B
C = [a b c d e f]'
C = inv(A)*B
CODE:
Main program:
clear all
close all
clc
n = 200;
x = pi/3;
dx = linspace(pi/3,pi/1000,n);
for i = 1:length(dx)
central_differencing(i) = central(x,dx(i));
skewed_left_differencing(i) = left(x,dx(i));
skewed_right_differencing(i) = right(x,dx(i));
end
figure(1)
loglog(dx,central_differencing,'r');
hold on
loglog(dx,skewed_left_differencing,'b');
hold on
loglog(dx,skewed_right_differencing,'g');
xlabel('dx')
ylabel('error')
legend('central_differencing','skewed_left_differencing','skewed_right_differencing');
Function Codes :
Central Differencing:
function al = central(x,dx);
analytical = -2*exp(x)*sin(x)
f = @(x) exp(x)*cos(x);
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]';
C = inv(A)*B
a = C(1) ;
b = C(2) ;
c = C(3) ;
d = C(4) ;
e = C(5) ;
numerical_central = (a*f(x-2*dx) + b*f(x-dx) + c*f(x) + d*f(x+dx) + e*f(x+2*dx))/(dx^2);
al = abs(analytical - numerical_central);
end
Skewed Right Differencing :
function al = right(x,dx)
analytical = -2*exp(x)*sin(x)
f = @(x) exp(x)*cos(x);
A = [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];
B = [0 0 1 0 0 0]';
C = inv(A)*B
a = C(1) ;
b = C(2) ;
c = C(3) ;
d = C(4) ;
e = C(5) ;
g = C(6) ;
numerical_right = a*f(x*(dx)) + b*f(x+(1*dx)) + c*f(x+(2*dx)) + d*f(x+(3*dx)) + e*f(x+(4*dx)) + g*f(x+(5*dx))/(dx^2);
al = abs(analytical - numerical_right);
end
Skewed left Differencing:
function al = left(x,dx)
analytical = -2*exp(x)*sin(x)
f = @(x) exp(x)*cos(x);
A = [1 1 1 1 1 1
-5 -4 -3 -2 -1 0
25/2 16/2 9/2 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];
B = [0 0 1 0 0 0]';
C = inv(A)*B
a = C(1) ;
b = C(2) ;
c = C(3) ;
d = C(4) ;
e = C(5) ;
g = C(6) ;
numerical_left = a*f(x-(5*dx)) + b*f(x-(4*dx)) + c*f(x-(3*dx)) + d*f(x-(2*dx)) + e*f(x-(1*dx)) + g*f(x)/(dx^2);
al = abs(analytical - numerical_left);
end
Conclusion :
From the above codes , we are trying to plot the error from the three methods appropriately.
When we plot the error vs dx , we come to know that central differencing has the minimal error when compared to other two methods.
To recapitulate, we find the one with symmeric nodes or intervals or points has the minimal error. Hence it is the better menthod to use.