comparison of absolute error between 4th order scheme for second derivative
close all
clear all
clc
n_x =10
x=pi/3;
dx = linspace(pi/4,pi/40,n_x)
for i=1:length(dx)
skewed_left_error(i) = skewed_left(x,dx(i))
skewed_right_error(i) = skewed_right(x,dx(i))
central_error(i)= central_(x,dx(i));
end
figure(1)
loglog(dx,skewed_left_error,\'color\',\'b\',\'marker\',\'H\')
hold on
loglog(dx,skewed_right_error,\'color\',\'r\',\'marker\',\'H\');
hold on
loglog(dx,central_error,\'color\',\'g\',\'marker\',\'H\');
xlabel(\'dx\')
ylabel(\'absolute error\')
title(\'error of different scheme vs. dx(step size)\')
legend(\'skewed left error\',\'skewed right error\',\'central error\',\'northwest\')
%solving by using function skewed_right
function out = skewed_right(x,dx)
%function f(x) = exp(x)*cos(x)
syms a b c d e h
%for skewed_left no. point (n) = (order of derivative + order of accurancy)
%n =4 + 2 =6
% solving equation with taylor table method
equation_1 = a + b + c + d + e + h ==0;
equation_2 = 0 + b +(2*c) + (3*d) + (4*e) + (5*h) ==0;
equation_3 = 0 + b +(4*c) + (9*d) + (16*e) + (25*h) == 2 ;
equation_4 = 0 + b +(8*c) + (27*d) + (64*e) + (125*h) ==0 ;
equation_5 = 0 + b +(16*c) + (81*d) + (256*e) + (625*h) ==0;
equation_6 = 0 + b +(32*c) + (243*d) + (1024*e) + (3125*h) ==0;
equation = [equation_1 equation_2 equation_3 equation_4 equation_5 equation_6];
s= solve(equation)
a=s.a
b=s.b
c=s.c
d=s.d
e=s.e
h=s.h
f=@(x)exp(x)*cos(x);
x=pi/3;
%dx = pi/4000
%analytical_function f(x)=exp(x)*cos(x)
analytical_derivative = (-2)*exp(x)*sin(x)
skewed_right_numerically = ((a*f(x))+(b*f(x+dx)) + (c*f(x+2*dx)) +(d*f(x+3*dx)) +( e*f(x+4*dx)) +( h*f(x+5*dx)) )/(dx^2)
out = abs(skewed_right_numerically-analytical_derivative)
end
%solving through skewed_left
function out = skewed_left(x,dx)
syms a b c d e h
%for skewed_left no. point (n) = (order of derivative + order of accurancy)
%n =4 + 2 =6 thus we have 6 equation for 6 unknown
% solving equation with taylor table method
equation_1 = a + b + c + d + e + h==0;
equation_2 = 0 - b -(2*c) - (3*d) - (4*e) - (5*h) ==0
equation_3 = 0 + b +(4*c) + (9*d) + (16*e) + (25*h) == 2 ;
equation_4 = 0 - b - (8*c) - (27*d) - (64*e) - (125*h) ==0
equation_5 = 0 + b +(16*c) + (81*d) + (256*e) + (625*h) ==0
equation_6 = 0 - b - (32*c) - (243*d) - (1024*e) - (3125*h) ==0
equation = [equation_1 equation_2 equation_3 equation_4 equation_5 equation_6];
s= solve (equation)
a=s.a
b=s.b
c=s.c
d=s.d
e=s.e
h=s.h
%using annonymous function
f=@(x)exp(x)*cos(x);
x=pi/3;
%analytical_function f(x)=exp(x)*cos(x)
analytical_derivative = (-2)*exp(x)*sin(x)
skewed_left_numerically = ((a*f(x))+(b*f(x-dx)) + (c*f(x-2*dx)) +(d*f(x-3*dx)) +( e*f(x-4*dx)) +( h*f(x-5*dx)) )/(dx^2)
out = abs(skewed_left_numerically-analytical_derivative)
end
%using function central
function out =central_(x,dx)
f=@(x)exp(x)*cos(x);
%analytical_function f(x)=exp(x)*cos(x)
x=pi/3;
analytical_derivative = -2*sin(x)*exp(x)
syms a b c d e
%for skewed_left no. point (n) = (order of derivative + order of accurancy - 1)
%n =4 + 2 -1 = 5 thus we have 5 equation for 5 unknown
% solving equation with taylor table method
equation_1 = a + b + c + d + e == 0;
equation_2 = -2*a - b + 0 + d + 2*e == 0;
equation_3 = 2*a + b/2 + 0 + d/2 + 2*e == 1;
equation_4 = -8*a/6 - b/6 + 0 + d/6 + 8*e/6 == 0;
equation_5 = 16*a/24 + b/24 + 0 + d/24 + 16*e/24 == 0;
equation = [equation_1 equation_2 equation_3 equation_4 equation_5 ];
s= solve (equation)
a=s.a
b=s.b
c=s.c
d=s.d
e=s.e
central_numerically = ((a*f(x-2*dx))+(b*f(x-dx)) + (c*f(x)) +(d*f(x+dx)) +( e*f(x+2*dx)) )/(dx^2)
out = abs(central_numerically-analytical_derivative)
end
above plot shows error of different scheme to step size ,here we can say that as grid sace is decresed , the absolute error reduces for skewed right side however for central scheme error is very low where dx is vely low in compared to other skewed left and skewed right scheme