Challenge

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

Function code for central differencing

%function code for central diffrencing 

function error_central_diffrencing = fourth_order_central_diffrencing(x,dx)

% analytic function = exp(x).cos(x)

analytic_derivative = -2*exp(x)*sin(x);

central_diffrencing = ((-0.0833333333333333)*(exp(x-(2*dx))*(cos(x-(2*dx))))+((1.3333333333333333)*((exp(x-dx))*(cos(x-dx))))-((2.5000)*((exp(x))*(cos(x))))+((1.33333333333333333)*((exp(x+dx))*(cos(x+dx))))-(0.083333333333333333)*((exp(x+(2*dx)))*(cos(x+(2*dx)))))/(dx^2);

error_central_diffrencing = abs(central_diffrencing-analytic_derivative);
end

Funtion code for right sided skew difference

% function code for skewed right sided difference

function error_right_sided_skew_difference = fourth_order_right_sided_skew_difference(x,dx)

% analytic function = exp(x).cos(x)

analytic_derivative = -2*exp(x)*sin(x);

right_sided_skew_difference = (((3.7500)*((exp(x))*(cos(x))))-((12.833333333333333333333)*((exp(x+dx))*(cos(x+dx))))+((17.8333333333333333333)*((exp(x+(2*dx)))*(cos(x+(2*dx)))))-((13.0000)*((exp(x+(3*dx)))*(cos(x+(3*dx)))))+((5.0833333333333333333333)*((exp(x+(4*dx)))*(cos(x+(4*dx)))))-((0.8333333333333333333333333)*((exp(x+(5*dx)))*(cos(x+(5*dx))))))/(dx^2);

error_right_sided_skew_difference = abs(right_sided_skew_difference - analytic_derivative);
end

Function code for left sided skew diffrence

% function code for skewed left sided difference

function error_left_sided_skew_difference = fourth_order_left_sided_skew_difference(x,dx)

% analytic function = exp(x).cos(x)

analytic_derivative = -2*exp(x)*sin(x);

left_sided_skew_difference = ((((-0.833333333333333333333)*((exp(x-(5*dx)))*(cos(x-(5*dx)))))+((5.08333333333333333333333333)*((exp(x-4*dx)))*(cos(x-(4*dx)))))-((13.0000)*((exp(x-(3*dx)))*(cos(x-(3*dx)))))+((17.8333333333333333333333333)*((exp(x-(2*dx)))*(cos(x-(2*dx)))))-((12.833333333333333333333333333)*((exp(x-dx))*(cos(x-dx))))+((3.7500)*((exp(x))*(cos(x)))))/(dx^2);

error_left_sided_skew_difference = abs(left_sided_skew_difference - analytic_derivative)
end

Main code

% Main code

clear all
close all
clc

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

for i=1:length(dx)
    central_diffrencing(i)= fourth_order_central_diffrencing(x,dx(i));
    right_sided_skew_difference(i)= fourth_order_right_sided_skew_difference(x,dx(i));
    left_sided_skew_difference (i)= fourth_order_left_sided_skew_difference(x,dx(i));
end
% plotting
figure 
loglog(dx,central_diffrencing,'g')
hold on
loglog(dx,right_sided_skew_difference,'b')
hold on
loglog(dx,left_sided_skew_difference,'r')

xlabel('range of dx')
ylabel('error')

legend('central diffrencing','right sided skew difference','left sided skew difference')

Cental diffrencing scheme is a finite diffrence method which optimizes approximation for diffrential operator in central node of the considered patch and provides numerical solution to diffrential equation.

The order of accuracy for central diffrencing is given as : - `P = N-Q+1`

where, P = order of accuracy

N = number of node in stencil

Q = order of derivative

So number of node (N) is equal to 5.

The order of accuracy for Skewed right-sided difference is given as: - `P = N - Q`

where, P = order of accuracy

N = number of node in stencil

Q = order of derivative

So number of node (N) is equal to 6

The order of accuracy for Skewed left-sided difference is given as: - `P = N - Q`

where, P = order of accuracy

N = number of node in stencil

Q = order of derivative

So number of node (N) is equal to 6

The main objective of program in matlab is to evaluate the second order derivative of function `exp(x)*cos(x)` then compare the error and plot the graph which compares the error and numerical scheme.

Graph explanation: -

Graph has been uploaded below in for of screenshot. It can be seen from the graph that central diffrence scheme has least error compared to right sided and left sided difference scheme. The error value descreses rapidly as discretization size decreases upto some extend and then eror starts ibresing . This change of trend in error is due to the round off error.

Why a skewed scheme is useful? What can a skewed scheme do that a CD scheme cannot?

As we have seen in graph central differencing has lowest error compared to skewed method but on the contrary skewed are more useful because we can predict derivative of function at a point by knowing the value of function only on one side left or right . This act as great benefit when we are having information about the function on only one side point in stencil.

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.