Project

Deriving the 4th order approximation of a second order derivative and comparing results obtained using FDF BDF CDF

In general , If we want a \'q\'th order approximation of a \'p\'th order derivative than there are

p+q terms in the equation if we use one sided formula &

p+q-1 terms in the equation if we use a central difference problem.

Therefore second order derivative at a point for a Skewed right sided difference is given in general as :

In this case p=2 and q=4. So n=6 for one sided forumla and n=5 for central difference formula. Therefore , the general equations for the 3 approximations are as follows:

We find out the values for a, b, c, d, e, f by expanding f(i+1),f(i-1),etc .. terms by using taylor series expansion and than compare both sides to obtain the coefficients .

Upon performing the above step for all the three equations using the taylor\'s table method the following values for a,b,c,d,e,f have been obtained. Now the equations are

Here is the code to find out the absolute error between the analytical derivative and the numerical derivative for various values of dx

First we define a function titled \'f.m\' which takes in the desired value of x and finds out f(x)at the given point.

function [a] = f(y)

a=exp(y)*cos(y)

end

Here is the code of the main program:

clear all
close all
clc
x=pi/3
dx=linspace(0.1,0.4,30)
%f(x)=exp(x)*cos(x)
%coefficients for right side=[3.7500 -12.8333 17.8333 -13.0000 5.0833
%-0.8333]



analytical_derivative = [-2*exp(x)*sin(x)]
%loop for skewed right hand---1
for i=1:length(dx)
    
    t1 = 3.7500*f(x)
    t2=-12.8333*f(x+dx(i))
    t3=17.8333*f(x+(2*dx(i)))
    t4=-13.0000*f(x+(3*dx(i)))
    t5=5.0833*f(x+(4*dx(i)))
    t6=-0.8333*f(x+(5*dx(i)))
    numerical_derivative1(i) = (t1+t2+t3+t4+t5+t6)/((dx(i))^2)
    error1(i)=abs(analytical_derivative-numerical_derivative1(i))
end
%loop for skewed left hand---2
for i=1:length(dx)
    
    t1 = 3.7500*f(x)
    t2=-12.8333*f(x-dx(i))
    t3=17.8333*f(x-(2*dx(i)))
    t4=-13.0000*f(x-(3*dx(i)))
    t5=5.0833*f(x-(4*dx(i)))
    t6=-0.8333*f(x-(5*dx(i)))
    numerical_derivative2(i) = (t1+t2+t3+t4+t5+t6)/((dx(i))^2)
    error2(i)=abs(analytical_derivative-numerical_derivative2(i))
end
%loop for central difference
for i=1:length(dx)
    
    t1 = -2.5*f(x)
    t2= 1.3333*f(x-dx(i))
    t3= -0.0833*f(x-(2*dx(i)))
    t4= 1.3333*f(x+(dx(i)))
    t5= -0.0833*f(x+(2*dx(i)))
 
    numerical_derivative3(i) = (t1+t2+t3+t4+t5)/((dx(i))^2)
    error3(i)=abs(analytical_derivative-numerical_derivative3(i))
end


semilogy(dx,error1,\'-r\',\'LineWidth\',2)
hold on
semilogy(dx,error2,\'--b\',\'LineWidth\',2) 
semilogy(dx,error3,\':k\',\'LineWidth\',2)
hold off
legend(\'Right Side Skewed\',\'Left Side Skewed\',\'Central Difference\',\'location\',\'SouthEast\')
title(\'effect of dx on the absolute error\')
xlabel(\'Value of dx\')
ylabel(\'absolute error(log scale)\')
grid on
saveas(gca,\'effect of dx for different schemes2.jpeg\')

Here is a semi log plot where the logarithm is applied to the error and x axis contains dx values

Answer to second part of assignment:

1)A central difference scheme is more accurate but cannot be applied at the boundaries. But a one sided i.e forward or backward differencing scheme can be applied anywhere within the domain.

Conclusion:

1)As we can see that with an increase in dx value there is an increase in the absolute error with all the three schemes

2) For a given value of dx Right handed Skewed scheme has a greater error than Left or central schemes

google drive m file:

https://drive.google.com/open?id=1w6ZGcCpQ8jkK0XDqG9ge-C7ev1fInLQ6

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.