Week 3 - Taylor table method and Matlab code
OBJECTIVE: to calculate the tailor series method for the skewed right approximation, skewed left approximation, and central differencing
writing the program to calculate the errors in the right-skewed scheme, left-skewed scheme, central differencing scheme
EQUATIONS AND THEORY:
before going to calculation part we have to know for fourth order approximation how much nodes we have to take for the central and skewed differencing.
for central differencing:
1. The order of accuracy P = Number of nodes in the stencil N - order of derivative Q + 1 for symmetric/central schemes
number of nodes in the sensil =The order of accuracy P + order of derivative Q -1
number of nodes in the sensil = 4+2-1
number of nodes in the sensil=5 for central order approximation,
this means we can consider five nodes for central order scheme.
2. The order of accuracy P = Number of nodes in the stencil N - order of derivative Q for symmetric/central schemes
number of nodes in the sensil =The order of accuracy P + order of derivative Q
number of nodes in the sensil = 4+2
number of nodes in the sensil=6 for right skewed and left skewed approximations
this means we can consider six nodes for both right skewed and left skewed scheme.
- central differencing:
the formula to calculate central differencing
`(d^2f)/dx^2= (af_(i-2)+ bf_(i-1)+cf_(i)+df_(i+1)+ ef_(i+2))/(Delta*x^2)`
| `f(i)` | `Deltax f'(i)` | `(Deltax^2) f''(i)` | `(Deltax^3) f'''(i)` | `(Deltax^4) f''''(i)` | `(Deltax^5) f'''''(i)` | `(Deltax^6) f''''''(i)` | |
| af(i) | a | -2a | 4a/2 | -8a/6 | 16a/24 | -32a/120 | 64a/720 |
| bf(i+1) | b | -b | b/2 | -b/6 | b/24 | -b/120 | b/720 |
| cf(i+2) | c | 0 | 0 | 0 | 0 | 0 | 0 |
| df(i+3) | d | d | d/2 | d/6 | d/24 | d/120 | d/720 |
| ef(i+4) | e | 2e | 4e/2 | 8e/6 | 16e/24 | 32e/120 | 64e/720 |
| 0 | 0 | 1 | 0 | 0 | ? | ? |
a = 0.0833, b= 1.333, c=-2.500, d=1.333, e= -0.0833
equation:
a+b+c+d+e=0 ..........(1)
-2a-b+0+d+2e= 0 ..........(2)
2a+b/2+0+d/2+2e=1 ..........(3)
(-8a/6)+(-b/60+0+d/6+8e/6 ..........(4)
16a/24+b/24+0+d/24+16e/24=0 ..........(5)
a = 0.0833, b= 1.333, c=-2.500, d=1.333, e= -0.0833
by solving the given equation we get the value for a, b ,c, d, e,
central differencing : in the central differencing we take the information from the both side of the of the node so we get the accuracy is also good but when compared two left and right skewed diffrencing the accuracy is less.
- skewed right differencing
the formula to calculate the skewed left side differencing
`(d^2f)/dx^2= (af_(i)+ bf_(i+1)+cf_(i+2)+df_(i+3)+ ef_(i+4)+gf_(i+5))/(Delta*x^2)`
| `f(i)` | `Deltax f'(i)` | `(Deltax^2) f''(i)` | `(Deltax^3) f'''(i)` | `(Deltax^4) f''''(i)` | `(Deltax^5) f'''''(i)` | `(Deltax^6) f''''''(i)` | |
| af(i) | a | 0 | 0 | 0 | 0 | 0 | 0 |
| bf(i+1) | b | b | b/2 | b/6 | b/24 | b/120 | b/720 |
| cf(i+2) | c | 2c | 4c/2 | 8e/6 | 16c/24 | 32c/120 | 64c/720 |
| df(i+3) | d | 3d | 9d/2 | 27d/6 | 81d/24 | 243d/120 | 729d/720 |
| ef(i+4) | e | 4e | 16e/2 | 64e/6 | 256e/24 | 1024e/120 | 4096e/720 |
| ef(i+5) | f | 5g | 25g/2 | 125g/6 | 625g/24 | 3125g/120 | 15625g/720 |
| 0 | 0 | 1 | 0 | 0 | ? | ? |
equations:
a+b+c+d+e+g=0 ..........(1)
0+b+2c+3d+4e+5g=0 ..........(2)
0+b/2+4c/2+9d/2+16e/2+25g/2=1 .......(3)
0+b/6+8c/6+27d/6+64e/6+125g/24= 0 ......(4)
0+b/24+10c/24+81d/24+256e/24+625g/120=......(5)
0+b/120+32c/120+243d/120+1024e/120+3125g/120=0....(6)
by solving the given equation we get the value for a, b ,c, d, e,g
a = 3.7500 , b=-12.833, c=17.833, d=-13.00, e=5.0888, g=0.833
right skewed differencing: in this method we are taking the inform ation from the right side of the node so thw order od accuracy is nearly more compared to the central differencing.
- skewed left-hand approximation:
the formula to calculate the skewed left-hand differencing:
`(d^2f)/dx^2= (af_(i)+ bf_(i-1)+cf_(i-2)+df_(i-3)+ ef_(i-4)+gf_(i-5))/(Delta*x^2)`
| `f(i)` | `Deltax f'(i)` | `(Deltax^2) f''(i)` | `(Deltax^3) f'''(i)` | `(Deltax^4) f''''(i)` | `(Deltax^5) f'''''(i)` | `(Deltax^6) f''''''(i)` | |
| af(i) | a | 0 | 0 | 0 | 0 | 0 | 0 |
| bf(i+1) | b | b | b/2 | b/6 | b/24 | b/120 | b/720 |
| cf(i+2) | c | 2c | 4c/2 | 8e/6 | 16c/24 | 32c/120 | 64c/720 |
| df(i+3) | d | 3d | 9d/2 | 27d/6 | 81d/24 | 243d/120 | 729d/720 |
| ef(i+4) | e | 4e | 16e/2 | 64e/6 | 256e/24 | 1024e/120 | 4096e/720 |
| ef(i+5) | g | 5g | 25g/2 | 125g/6 | 625g/24 | 3125g/120 | 15625g/720 |
| 0 | 0 | 1 | 0 | 0 | ? | ? |
equations:
a+b+c+d+e+g=0 ..........(1)
0-b-2c-3d-4e-5g=0 ..........(2)
0+b/2+4c/2+9d/2+16e/2+25g/2=1 .......(3)
0-b/6-8c/6-27d/6-64e/6-125g/24= 0 ......(4)
0+b/24+10c/24+81d/24+256e/24+625g/120=......(5)
0-b/120-32c/120-243d/120-1024e/120-3125g/120=0....(6)
by solving the given equation we get the value for a, b ,c, d, e,g
a = 3.7500 , b=-12.833, c=17.833, d=-13.00, e=5.0888, g=-0.833
left skewed differencing: in this method we are taking the inform ation from the right side of the node so thw order od accuracy is nearly more compared to the central differencing.
PROGRAM:
CODE:
%defining the inputs
clear all
close all
clc
%defining the mesh size of the domain
dx = linspace( pi/5, pi/400, 20);
%defining the value of x between points
x = pi/30;
for i = 1:length(dx)
central_differencing_error(i) = central (x,dx(i))
end
for i = 1:length(dx)
right_skewed_error(i) = right ( x, dx(i))
end
for i = 1:length(dx)
left_skewed_error(i) = left ( x, dx(i))
end
figure(1)
loglog(dx,central_differencing_error,'r')
hold on
loglog(dx,right_skewed_error,'b')
hold on
loglog(dx,left_skewed_error,'g')
xlabel('log dx')
ylabel('log error')
legend('right skewed error','central skewed error','left diffrencing error')
title('comparison of central differencing right and left skewed diffrencing')
FUNCTIONAL CODE:
function right_skewed_error = right ( x, dx)
%defining the function
f = @(x) exp(x)*cos(x);
%ANALYTICAL DERIVATIVE
analytical_derivative = -2*sin(x)*exp(x);
%DEFINING MATRIX A
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];
%DEFINING MATRIX B
B = [ 0; 0; 1; 0; 0; 0];
%CALCULATING THE VALUE OF THE MATRIX X THAT CONTAINS ALL CO-EFFICIENTS
C=A\B;
%ASSINING THE VALUE TO EACH CO-EFFICIENTS
a= C(1)
b= C(2)
c= C(3)
d= C(4)
e= C(5)
g= C(6)
% central differencing
numerical_derivative =(a*f(x)+(b*f(x+(dx)))+(c*f(x+(2*dx)))+(d*f(x+(3*dx)))+(e*f(x+(4*dx)))+(g*f(x+(5*dx))))/ dx^2;
right_skewed_error = abs(numerical_derivative -analytical_derivative);
end
FUNCTTIONAL CODE:
function left_skewed_error = left ( x, dx)
%defining the function
f = @(x) exp(x)*cos(x);
%ANALYTICAL DERIVATIVE
analytical_derivative= -2*sin(x)*exp(x) ;
%DEFINING MATRIX A
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];
%DEFINING MATRIX B
B = [0; 0; 1; 0; 0; 0];
%CALCULATING THE VALUE OF THE MATRIX X THAT CONTAINS ALL CO-EFFICIENTS
C= A\B;
%ASSINING THE VALUE TO EACH CO-EFFICIENTS
a= C(1)
b= C(2)
c= C(3)
d= C(4)
e= C(5)
g= C(6)
% central differencing
numerical_derivative =(a*f(x)+(b*f(x-(dx)))+(c*f(x-(2*dx)))+(d*f(x-(3*dx)))+(e*f(x-(4*dx)))+(g*f(x-(5*dx))))/ dx^2;
left_skewed_error = abs(numerical_derivative -analytical_derivative);
end
FUNCTIONAL CODE:
function central_differencing_error = central ( x, dx)
%defining the function
f = @(x) exp(x)*cos(x);
%ANALYTICAL DERIVATIVE
analytical_derivative= -2*sin(x)*exp(x) ;
%DEFINING MATRIX A
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];
%DEFINING MATRIX B
B = [ 0; 0; 1; 0; 0];
%CALCULATING THE VALUE OF THE MATRIX X THAT CONTAINS ALL CO-EFFICIENTS
C= inv(A)*B;
%ASSINING THE VALUE TO EACH CO-EFFICIENTS
a= C(1)
b= C(2)
c= C(3)
d= C(4)
e= C(5)
% central differencing
central_differencing = (a*f(x-(2*dx))+b*f(x-(dx))+c*f(x)+d*f(x+(dx))+e*f(x+(2*dx)))/dx^2;
central_differencing_error = abs(central_differencing -analytical_derivative);
end
results;
this figure compares the central differencing error, left skewed differencing error, right skewed diffrencing error.
1. why skewed scheme is useful? what a skewed scheme camn do but cd cant do?
- skewed scheme is useful because in some complex system the numerical stencil wont be symmetric points is available and at this point only skewed scheme can help. And the advantage of using skewed system is seen near the boundaries where there is no access to further points over the boundary i,e Asymmetric.
-
The order of accuracy P = Number of nodes in the stencil N - order of derivative Q + 1 for symmetric/central schemes
number of nodes in the sensil =The order of accuracy P + order of derivative Q -1
number of nodes in the stensil = 4+2-1
number of nodes in the sensil=5 for central order approximation,
this means we can consider five nodes for the central order scheme.
- in this central order differencing the data we are taking to solve and get the solution is from the 5 nodes as the data we are considering is more the error becomes less. the numerical stencil for central differencing is smaller then skewed differencing.
-
The order of accuracy P = Number of nodes in the stencil N - order of derivative Q for symmetric/central schemes
number of nodes in the stencil =The order of accuracy P + order of derivative Q
number of nodes in the stencil = 4+2
number of nodes in the stencil=6 for right-skewed and left skewed approximations
this means we can consider six nodes for both right-skewed and left skewed scheme.
- in this method the order of accuracy is same but according to the formula mentioned above we are taking the number of nodes is equal to 6 . so the finding the solution using the 6 nodes is so the error is less and accuracy is more so instead of using the central differencing we can use the skewed schemes.