Taylor table method and Matlab code
Taylor table method and Matlab code
Derive the following 4th order approximations of the second-order derivative
1. Central difference
2. Skewed right-sided difference
3. Skewed left-sided difference
Once you have done this, write a program in Matlab to evaluate the second-order derivative of the analytical function exp(x)*cos(x) and compare it with the 3 numerical approximations that you have derived.
a. Provide a plot that compares the absolute error between the above-mentioned schemes
b. How do these errors compare with FDS and BDS approximations
c. In your own words, describe why a skewed scheme is useful? What can a skewed scheme do that a CD scheme cannot do?
4. Paste your code using \"INSERT CODE SAMPLE\" option and explain your outputs.
5. Write a detailed explanation for your results, plots or codes without explanation will be awarded zero marks.
Derivation of 4th order approximations of the second-order derivative:
CENTRAL DIFFERENCE SCHEME:
|
TERMS |
f(i) |
Δx f΄(x) |
Δ (x) |
Δ (x) |
Δ (x) |
Δ (x) |
Δ (x) |
|
af(i-2) |
a |
-2a |
4a/2 |
-8a/6 |
-16a/24 |
-32a/120 |
64a/240 |
|
bf(i-1) |
b |
-b |
b/2 |
-b/6 |
b/24 |
-b/120 |
b/240 |
|
cf(i) |
c |
0 |
0 |
0 |
0 |
0 |
0 |
|
df(i+1) |
d |
d |
d/2 |
d/6 |
d/24 |
d/120 |
d/240 |
|
ef(i+2) |
e |
2e |
4e/2 |
8e/6 |
16e/24 |
32e/120 |
64e/240 |
|
Σ (af(i-2)+ bf(i-1)+ cf(i)+ df(i+1)+ ef(i+2)) |
0 |
0 |
1 |
0 |
0 |
0 |
? |
SKEWED RIGHT SIDED SCHEME:
|
TERMS |
f(i) |
Δx f΄(x) |
Δ (x) |
Δ (x) |
Δ (x) |
Δ (x) |
Δ (x) |
|
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 |
8c/6 |
0 |
32c/120 |
64c/720 |
|
df(i+3) |
d |
3d |
9d/2 |
27d/6 |
d/24 |
245d/120 |
729d/740 |
|
ef(i+4) |
e |
4e |
16e/2 |
64e/6 |
16e/24 |
1024e/120 |
4096e/240 |
|
Σ (af(i)+ bf(i+1)+ cf(i+2)+ df(i+3)+ ef(i+4)) |
0 |
0 |
1 |
0 |
0 |
0 |
? |
SKEWED LEFT SIDED SCHEME:
|
TERMS |
f(i) |
Δx f΄(x) |
Δ (x) |
Δ (x) |
Δ (x) |
Δ (x) |
Δ (x) |
|
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 |
-8c/6 |
0 |
-32c/120 |
64c/720 |
|
df(i-3) |
d |
-3d |
9d/2 |
-27d/6 |
d/24 |
-245d/120 |
729d/740 |
|
ef(i-4) |
e |
-4e |
16e/2 |
-64e/6 |
16e/24 |
-1024e/120 |
4096e/240 |
|
Σ (af(i)+ bf(i-1)+ cf(i-2)+ df(i-3)+ ef(i-4)) |
0 |
0 |
1 |
0 |
0 |
0 |
? |
FORMULA FOR FINDING NUMBER OF TERMS :
N = [A+O-1]
N = number of terms
A = order of approximations
O = order of the derivative
N= [4+2-1]
= 5
Following is the program to derive the formula for second-order derivative with fourth-order approximation
clear all
close all
clc
% Taylor_table_coeffecients
% central difference scheme requires information from both sides ( symmetric)
% To obtain the 4th order approximated second-order derivative, we need information from 5 equally distributed neighboring points
[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:0];
%solving matrix A&B to find constants
X = linsolve(A,B);
% skewed right-sided difference scheme requires information on the right side
[C]= [ 1 1 1 1 1;
0 1 2 3 4;
0 1/2 2 9/2 8;
0 1/6 4/3 9/2 32/3;
0 1/24 2/3 27/8 32/3];
[D] = [0;0;1;0;0];
%solving matrix C&D to find constants
Y = linsolve(C,D);
% skewed left-sided difference scheme requires information on the right side
[E]= [ 1 1 1 1 1;
0 -1 -2 -3 -4;
0 1/2 2 9/2 8;
0 -1/6 -4/3 -9/2 -32/3;
0 1/24 2/3 27/8 32/3];
[F] = [0;0;1;0;0];
%solving matrix C&D to find constants
Z = linsolve(E,F);
% sum of coeffecinets should be zero
% check
check_X = X(1)+X(2)+X(3)+X(4)+X(5);
check_central_difference = rats(check_X);
check_Y = Y(1)+Y(2)+Y(3)+Y(4)+Y(5);
check_skewed_right = rats(check_Y);
check_Z = Z(1)+Z(2)+Z(3)+Z(4)+Z(5);
check_skewed_left = rats(check_Z);
COEFFICIENT VALUE FOR THREE SCHEMES
CENTRAL DIFFERENCE
X =
-0.083333
1.333333
-2.500000
1.333333
-0.083333
SKEWED RIGHT SIDED
>> Y
Y =
2.91667
-8.66667
9.50000
-4.66667
0.91667
SKEWED LEFT SIDED
>> Z
Z =
2.91667
-8.66667
9.50000
-4.66667
0.91667
FORWARD AND BACKWARD SCHEMES:
clear all
close all
clc
%Taylor_table_test
% analytical function f\' = exp(x)*cos(x)
%f\'\' = -2*exp(x)*sin(x)
x = pi/3;
dx = linspace(pi/10,pi/400,100);
analytical_function = exp(x)*cos(x);
analytical_derivative = -2*exp(x)*sin(x);
for i = 1:length(dx);
backward_difference(i) = (exp(x)*cos(x)- exp(x-dx(i))*cos(x-dx(i)))/dx(i);
forward_difference(i) = (exp(x+dx(i))*cos(x+dx(i))-exp(x)*cos(x))/dx(i);
backward_error(i) = abs(backward_difference(i)- analytical_derivative);
forward_error(i) = abs(forward_difference(i)- analytical_derivative);
end
%Error comparison between the above two schemes
figure(1)
loglog(dx,backward_error,\'-ks\', \'markerfacecolor\',\'b\',\'markersize\',10,\'linewidth\',2)
hold on
loglog(dx,forward_error,\'-kd\', \'markerfacecolor\',\'g\',\'markersize\',10,\'linewidth\',2)
hold on
legend (\'backward error\', \'forward error\',\'location\',\'northwest\');
xlabel(\'dx\')
ylabel(\'error\')
title ( \'FDS AND BDS ERROR COMPARSION\')
The program which results in a plot that compares the absolute error difference between the above-mentioned schemes:
clear all
close all
clc
%Taylor_table_test
% analytical function f\' = exp(x)*cos(x)
%f\'\' = -2*exp(x)*sin(x)
x = pi/3;
dx = linspace(pi/10,pi/400,100);
analytical_function = exp(x)*cos(x);
analytical_derivative = -2*exp(x)*sin(x);
for i = 1:length(dx);
a(i) = (exp(x-(5*dx(i)))*cos(x-(5*dx(i))));
b(i) = (exp(x-(4*dx(i)))*cos(x-(4*dx(i))));
c(i) = (exp(x-(3*dx(i)))*cos(x-(3*dx(i))));
d(i) = (exp(x-(2*dx(i)))*cos(x-(2*dx(i))));
e(i) = (exp(x-dx(i))*cos(x-dx(i)));
f(i) = exp(x)*cos(x)
g(i) = (exp(x+dx(i))*cos(x+dx(i)));
h(i) = (exp(x+(2*dx(i)))*cos(x+(2*dx(i))));
j(i) = (exp(x+(3*dx(i)))*cos(x+(3*dx(i))));
k(i) = (exp(x+(4*dx(i)))*cos(x+(4*dx(i))));
l(i) = (exp(x+(5*dx(i)))*cos(x+(5*dx(i))));
fourth_order_second_derivative_skewed_right(i) = (45*(f(i))-154*(g(i))+214*(h(i))-156*(j(i))+61*(k(i))-10*(l(i)))/(12*(dx(i)^2));
fourth_order_second_derivative_skewed_left(i) = (45*(f(i))-154*(e(i))+214*(d(i))-156*(c(i))+61*(b(i))-10*(a(i)))/(12*(dx(i)^2));
fourth_order_second_derivative_central_difference(i) = (-1*(d(i))+16*(e(i))-30*(f(i))+16*(g(i))-(h(i)))/(12*(dx(i)^2));
fourth_order_skewed_right_error(i) = abs(fourth_order_second_derivative_skewed_right(i)- analytical_derivative)
fourth_order_skewed_left_error(i) = abs(fourth_order_second_derivative_skewed_left(i)- analytical_derivative)
fourth_order_central_difference_error(i) = abs(fourth_order_second_derivative_central_difference(i)- analytical_derivative)
end
%Error comparsion between the above three schemes
figure(1)
loglog(dx,fourth_order_skewed_right_error, \'-kd\', \'markerfacecolor\',\'r\',\'markersize\',10,\'linewidth\',2)
hold on
loglog(dx,fourth_order_skewed_left_error,\'-ko\', \'markerfacecolor\',\'b\',\'markersize\',10,\'linewidth\',2)
hold on
loglog(dx,fourth_order_central_difference_error,\'-ks\', \'markerfacecolor\',\'g\',\'markersize\',10,\'linewidth\',2)
hold on
legend(\'fourth order skewed right error\',\'fourth order skewed left error\',\'fourth order central difference error\',\'location\',\'northwest\');
xlabel(\'dx\')
ylabel(\'error\')
title( \' Comparision between different schemes of second order derivative of 4th order approximation\')
OUTPUT PLOTS:
RESULTS:
>> fourth_order_skewed_right_error(end)
ans = 0.000000058606
>> fourth_order_skewed_left_error(end)
ans = 0.000000055703
>> fourth_order_central_difference_error(end)
ans = 0.00000000084233
>> backward_error(end)
ans = 3.9120
>> forward_error(end)
ans = 3.8732
FDS and BDS require information on the right and left neighboring points respectively. In FDS and BDS it solves the equation with 1st order approximation with 1st order derivative and the central differencing scheme solve the equation with 2nd order approximation with 1st order derivative both are using same two-point information but CDS provides more accurate results.
Compared to FDS and BDS skewed scheme provides a more accurate result because of getting more available information from many points. It can be used for solving when points available on one side. But the central difference can we use for solving two sides because of its symmetric nature. Higher the order lower the error for the same grid sizes.
In your own words, describe why a skewed scheme is useful? What can a skewed scheme do that a CD scheme cannot do?
The skewed scheme works well when no of points available on one side of the grid. Indeed, for calculating corner node with higher approximations we need skewed scheme at this point for this type no longer central scheme useful. for example, in a pipe flow at the upstream side can be used to calculate the downstream side flow behavior in the skewed scheme, but in the central scheme we need to depend on both sides, not every time information is available for both sides of the ith point.
The graph and output values obtained above for CDS, FDS, CDS, Skewed right scheme, Skewed lift scheme we can clearly understand that CDS contains less error compared to all other schemes
Hence the variation of the error as follows:
CDS
Then it is preferred that using CDS to solve equations will provide accurate results with fewer errors.