Taylor table method and Matlab code
AIM
The main aim of this challenge is to write an program determine the second order derivative for the given function and compare results with different schemes
OBJECTIVE
The main objective of this challenge is to derive the fourth order approximation for second order function.
FOURTH ORDER APPROXIMATION FOR SECOND ORDER DERIVATIVE
Central Difference Scheme
Now for the central difference scheme the hypothesis equation for the second order function is given by
`frac[del^2u][delx^2]=af(i-2)+bf(i-1)+cf(i)+df(i+1)+ef(i+2)`
In the above equation the values of a,b,c,d,e have to be determined by constructing the taylor's table in order to compute the values as follows
| f(i) | f'(i) | f''(i) | f'''(i) | f''''(i) | f'''''(i) | f''''''(i) | |
| 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 |
After constructing the taylor table summing and equating the coefficients we get a system of linear equations
`[[1,1,1,1,1],[-2,-1,0,1,2],[2,1/2,0,1/2,2],[-4/3,-1/6,0,1/6,4/3],[2/3,1/24,0,1/24,2/3]][[a],[b],[c],[d],[e]]=[(0),(0),(1),(0),(0)]`
Next, we solve the system of linear equation to get the values for a,b,c,d,e. The final equation for central diffrence scheme is given by
`frac{del^2u][delx^2]=-1/12f(i-2)+4/3f(i-1)-5/2f(i)+4/3f(i+1)-1/12f(i+2)]`
Skewed Right Sided Difference
Hypothesis equation for the skewed right sided difference is given by
`frac[del^2u][delx^2]=af(i)+bf(i+1)+cf(i+2)+df(i+3)+ef(i+4)+gf(i+5)`
In the above equation the values of a,b,c,d,e have to be determined by constructing the taylor's table in order to compute the values as follows
| f(i) | f'(i) | f''(i) | f'''(i) | f''''(i) | f'''''(i) | |
| af(i) | a | 0 | 0 | 0 | 0 | 0 |
| bf(i+1) | b | b | b/2 | b/6 | b/24 | b/120 |
| cf(i+2) | c | 2c | 4c/2 | 8c/6 | 16c/24 | 32c/120 |
| df(i+3) | d | 3d | 9d/2 | 27d/6 | 81d/24 | 243d/120 |
| ef(i+4) | e | 4e | 16e/2 | 64e/6 | 256e/24 | 1024e/120 |
| gf(i+5) | g | 5g | 25g/2 | 125g/6 | 625g/24 | 3125g/120 |
After constructing the taylor table summing and equating the coefficients we get a system of linear equations.
`[[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],[0,1/120,32/120,243/120,1024/120]] [[a],[b],[c],[d],[e],[g]]=[[0],[0],[1],[0],[0],[0]]`
To obtain the values of a,b,c,d,e,g we solve the matrix equation using matlab. The final equation for skewed right sided scheme is given by
`frac[del^2u][delx^2}=15/4f(i)-77/6f(i+1)+107/6f(i+2)-13f(i+3)+61/12f(i+4)-5/6f(i+5)`
Skewed Left Sided Difference
Similarly the hypothesis equation for skewed left sided scheme is given by
`frac[del^2u][delx^2]=af(i-5)+bf(i-4)+cf(i-3)+df(i-2)+ef(i-1)+gf(i)`
In the above equation the values of a,b,c,d,e,g have to be determined by constructing the taylor's table in order to compute the values as follows
| f(i) | f'(i) | f''(i) | f'''(i) | f''''(i) | f'''''(i) | |
| af(i-5) | a | -5a | 25a/2 | -125a/6 | 625a/24 | -3125a/120 |
| bf(i-4) | b | -4b | -16b/2 | -64b/6 | 256b/24 | -1024b/120 |
| cf(i-3) | c | -3c | 9c/2 | -27c/6 | 81c/24 | -243c/120 |
| df(i-2) | d | -2d | 4d/2 | -8d/6 | -16d/24 | 32d/120 |
| ef(i-1) | e | -e | e/2 | -e/6 | e/24 | -e/120 |
| gf(i) | 0 | 0 | 0 | 0 | 0 | 0 |
After constructing the taylor table summing and equating the coefficients we get a system of linear equations.
`[[1,1,1,1,1,1],[-5,-4,-3,-2,-1,0],[25/2,16/2,9/2,1,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,104/120,243/120,-32/120,-1/120,0]][[a],[b],[c],[d],[e],[g]]=[[0],[0],[1],[0],[0],[0]]`
For obtaining the values of a,b,c,d,e,g we solve the system of linear equations The final equation for skewed left scheme is
`frac[del^2u][delx^2]=-5/6f(i-5)+61/12f(i-4)-13f(i-3)-107/6f(i-2)-77/6f(i-1)+15/4f(i)`
MATLAB CODE
clear all
close all
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,'color','y','linewidth',2)
hold on
loglog(dx,fourth_order_skewed_left_error,'color','b','linewidth',2)
hold on
loglog(dx,fourth_order_central_difference_error,'color','r','linewidth',2)
hold on
legend('skewed right sided diffrence','skewed left sided diffrence','central difference scheme','location','northwest');
xlabel('dx')
ylabel('error')
title('taylor table')
The code for evaluating the second order derivative `f(x)=e^x xxcos(x)`and comparing it for the 3 numerical approximation has executed. First few lines shows again there is a range of values for dx and the analytical derivative has been computed. Next couple of lines shows the for command and the derivatives are being computed for various points within the numerical stencil boundary and the error for the functions are being defined. The plot to compare the 3 numerical approximations is of logarthemic scale which is used to study the taylor's table method.
RESULTS AND DISCUSSIONS
The plot compares skewed right sided difference, skewed left sided difference and central difference schemes for error versus dx to showcase the importance of the schemes. As we can observe from the plot that the error goes on reducing as value for dx comes down. Central scheme as the least amount of error compared to the other two types of skewed schemes. When we come to central differencing scheme, information is being taken from both left side and right side whereas the skewed schemes take the information either from left or the right region in the numerical stencil.
CONCLUSION
Equations for the 3 numerical approximations has been derived and implemented in the code to do a comparative study on how the error for the various schemes.