Challenge

Week 3.5 - Deriving 4th order approximation of a 2nd order derivative using Taylor Table method

ANSWER FOR THE FIRST 3 QUESTIONS ARE WRITTEN AND UPLOADED IN THE PDF ATTACHED TO THIS.

MAIN PROGRAM


clear all
close all
clc

x= pi/3;
dx = linspace(pi/4,pi/4000,40);

for i = 1:length(dx)
central_difference_error(i) = central_differencing(x,dx(i));
skewed_right_side_error(i) = skewed_right_sided(x,dx(i));
skewed_left_side_error(i) = skewed_left_sided(x,dx(i));
end

%plotting

figure(1)
plot(dx,central_difference_error,'linewidth',2)
hold on
plot(dx,skewed_right_side_error,'r','linewidth',2)
plot(dx,skewed_left_side_error,'g','linewidth',2)
xlabel('dx')
ylabel('error')
legend('central difference error','skewed right sided error','skewed left sided error')

figure(2)
loglog(dx,central_difference_error,'linewidth',2)
hold on
loglog(dx,skewed_right_side_error,'r','linewidth',2)
loglog(dx,skewed_left_side_error,'g','linewidth',2)
xlabel('dx')
ylabel('error')
legend('central difference_error','skewed_right_side_error','skewed_left_side_error')

CENTRAL DIFFERENCING

function error_central_differencing = central_differencing(x,dx)
%analytical function f(x) = exp(x)*cos(x);
%f''(x) = -2*exp(x)*sin(x);

analytical_derivative = -2*exp(x)*sin(x);
%values of coefficients calculated from our taylor table;

a = -0.083333;
b=1.33333;
c=-2.50000;
d=1.33333;
e=-0.083333;

%numerical derivative;
%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 difference fourth order approximaton;

central_differencing_fourth_order_approx = ((a*exp(x-2*dx)*cos(x-2*dx))+(b*exp(x-dx)*cos(x-dx))+(c*exp(x)*cos(x))+(d*exp(x+dx)*cos(x+dx))+(e*exp(x+2*dx)*cos(x+2*dx)))/dx^2;

error_central_differencing = abs(central_differencing_fourth_order_approx-analytical_derivative);
end

SKEWED RIGHT SIDED

function error_skewed_right_sided = skewed_right_sided(x,dx)

%analytical_function f(x) = exp(x)*cos(x);
%f''(x) = -2*exp(x)*sin(x);
analytical_derivative = -2*exp(x)*sin(x);

%values of coefficients calculated from our taylor table;

a = 3.750000;
b = -12.8333;
c = 17.83333;
d = -13.0000;
e = 5.083333;
g = -0.833333;

%numerical derivative;
%skewed right sided differencing =
%((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;

skewed_right_sided_fourth_order_approx = ((a*exp(x)*cos(x))+(b*exp(x+dx)*cos(x+dx))+(c*exp(x+2*dx)*cos(x+2*dx))+(d*exp(x+3*dx)*cos(x+3*dx))+(e*exp(x+4*dx)*cos(x+4*dx))+(g*exp(x+5*dx)))/dx^2;
error_skewed_right_sided = abs(skewed_right_sided_fourth_order_approx - analytical_derivative);
end

SKEWED LEFT SIDED

function error_skewed_left_sided = skewed_left_sided(x,dx)

%analytical_function f(x) = exp(x)*cos(x);
%f''(x) = -2*exp(x)*sin(x);
analytical_derivative = -2*exp(x)*sin(x);

%values of coefficients calculated from our taylor table;

a = -0.83333;
b = 5.08333;
c = -13.0000;
d = 17.83333;
e = -12.83333;
g = 3.750000;

%numerical derivative;
%skewed left sided differencing =((a*f(x-5*dx))+(b*f(x-4*dx))+(c*f(x-3*dx))+(d*f(x-2*dx))+(e*f(x-dx))+(g*f(x)))/dx^2;

skewed_left_sided_fourth_order_approx = ((a*exp(x-5*dx)*cos(x-5*dx))+(b*exp(x-4*dx)*cos(x-4*dx))+(c*exp(x-3*dx)*cos(x-3*dx))+(d*exp(x-2*dx)*cos(x-2*dx))+(e*exp(x-dx)*cos(x-dx))+(g*exp(x)))/dx^2;
error_skewed_left_sided = abs(skewed_left_sided_fourth_order_approx - analytical_derivative);
end

The central Differencing scheme can give more perfect solutions with lesser errors. But it is not always recommended for evaluating solutions, for example, if we want to compute a solution for the node at the corner or boundary, then we cannot use a central difference scheme, because, for a central difference scheme, data is being calculated from both the sides. So Skewed difference method can be very useful in this kind of situations

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.