Week 3 - Taylor table method and Matlab code
objective:
1. To derive 4th order approximations of the second-order derivative- Central difference, Skewed right-sided difference and Skewed left-sided difference.
2. Providing proof for skewed schemes are fourth-order accurate.
3. To 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 derived.
4. Providing a plot that compares the absolute error between the above-mentioned schemes.
5. describing why a skewed scheme is useful? What can a skewed scheme do that a CD scheme cannot?
1. Derivations:






2. proof for skewed schemes are fourth-order accurate.
1.central difference:
The order of accuracy P = Number of nodes in the stencil N - order of derivative Q + 1 for central schemes
for central scheme, N=5, Q=2
P=5-2+1=4
therefor, 4th order of accuracy
2. skewed right side or left side difference:
The order of accuracy P = Number of nodes in the stencil N - order of derivative Q for one-sided/skewed schemes
here, N=6, Q=2
P=N-Q=6-2=4
therefor, skewed right side or left side difference 4th order of accuracy
Code:-
close all
clear all
clc
% considering suitable value for x and dx
x=pi/3;
dx=linspace(pi/20,pi/40000,20);
% defining function terms for incremental changes of values x and dx
% calling functions
for i=1:length(dx)
central_aprox(i)=central_order_accu(x,dx(i));
skewedright_aprox(i)=skewedright_order_accu(x,dx(i));
skewedleft_aprox(i)=skewedleft_order_accu(x,dx(i));
end
% priting values of different methods of 2nd order derivative
disp('central aproximation_error')
central_aprox(i)
disp('skewedright aproximation_error')
skewedright_aprox(i)
disp('skewedleft aproximation_error')
skewedleft_aprox(i)
loglog(dx, central_aprox)
hold on
loglog(dx, skewedright_aprox,'r')
loglog(dx, skewedleft_aprox,'g')
xlabel('dx');
ylabel('error');
title('comparision between central order, skewed rightside order and skewed leftside order approximation')
legend('central order aproximation', 'skewed rightside approximation','skewed leftside approximation')
function error=central_order_accu(x,dx)
f=exp(x)*cos(x); % f(i)
f1=exp(x+dx)*cos(x+dx); % f(i+1) so on.....
f2=exp(x+2*dx)*cos(x+2*dx);
f3=exp(x+3*dx)*cos(x+3*dx);
f4=exp(x+4*dx)*cos(x+4*dx);
f5=exp(x+5*dx)*cos(x+5*dx);
f1n=exp(x-dx)*cos(x-dx); % f(i-1) so on.....
f2n=exp(x-2*dx)*cos(x-2*dx);
f3n=exp(x-3*dx)*cos(x-3*dx);
f4n=exp(x-4*dx)*cos(x-4*dx);
f5n=exp(x-5*dx)*cos(x-5*dx);
analytical_sol=-2*exp(x)*sin(x);
central_ans=((-0.0833*f2n)+(1.3333*f1n)-(2.5*f)+(1.3333*f1)-(0.0833*f2))/(dx)^2;
error=abs(analytical_sol-central_ans);
end
function error=skewedright_order_accu(x,dx)
f=exp(x)*cos(x); % f(i)
f1=exp(x+dx)*cos(x+dx); % f(i+1) so on.....
f2=exp(x+2*dx)*cos(x+2*dx);
f3=exp(x+3*dx)*cos(x+3*dx);
f4=exp(x+4*dx)*cos(x+4*dx);
f5=exp(x+5*dx)*cos(x+5*dx);
f1n=exp(x-dx)*cos(x-dx); % f(i-1) so on.....
f2n=exp(x-2*dx)*cos(x-2*dx);
f3n=exp(x-3*dx)*cos(x-3*dx);
f4n=exp(x-4*dx)*cos(x-4*dx);
f5n=exp(x-5*dx)*cos(x-5*dx);
analytical_sol=-2*exp(x)*sin(x);
skewedright_ans=((3.75*f)-(12.8333*f1)+(17.8333*f2)-(13*f3)+(5.0833*f4)-(0.8333*f5))/(dx)^2;
error=abs(analytical_sol-skewedright_ans);
end
function error=skewedleft_order_accu(x,dx)
f=exp(x)*cos(x); % f(i)
f1=exp(x+dx)*cos(x+dx); % f(i+1) so on.....
f2=exp(x+2*dx)*cos(x+2*dx);
f3=exp(x+3*dx)*cos(x+3*dx);
f4=exp(x+4*dx)*cos(x+4*dx);
f5=exp(x+5*dx)*cos(x+5*dx);
f1n=exp(x-dx)*cos(x-dx); % f(i-1) so on.....
f2n=exp(x-2*dx)*cos(x-2*dx);
f3n=exp(x-3*dx)*cos(x-3*dx);
f4n=exp(x-4*dx)*cos(x-4*dx);
f5n=exp(x-5*dx)*cos(x-5*dx);
analytical_sol=-2*exp(x)*sin(x);
skewedleft_ans=(-(0.8333*f5n)+(05.0833*f4n)-(13*f3n)+(17.8333*f2n)-(12.8333*f1n)+(3.75*f))/(dx)^2;
error=abs(analytical_sol-skewedleft_ans);
end
Plot:-

from the graph we can conclude that central difference methode has least error compare to other two difference methodes.
skewed right sided and skewed leftsided difference method usually used in boundaries where central differencing not possible to apply, as there will not be any side nabouring points(values) which needed for central difference methode.