Project

Compare the first, second and fourth order approximations of the first derivative against the analytical or exact derivative.

Here we are required to compute the First Numerical and Analytical derivatives.

Definition of Analytical Derivative: It can be referred to as the exact derivative i.e. the exact value of the derivative of the function at a given point.

Definition of Numerical Derivative: It can be referred to as the approximated derivative i.e. the approximation of the actual derivative of the given function at a given point according to a different order of approximations such as first, second, fourth and many more using a discretization parameter called as dx. According to the different orders of approximations employed, different accuracies are obtained respectively.

The funcition given is : `f(x) = frac(sin(x))(x^3)`.

We are required to compute the first, second, and fourth-order approximations of the first derivative.

1.) First Order Approximation:

Expanding `u(x+h)` as a Taylor Series Expansion :

`u(x+h) = u(x) + hu'(x) + h^2frac(u''(x))(2!) + ......`

Neglecting the higher-order terms of the order `h^2` and above we get

`u(x+h) approx u(x) + hu'(x)`

Simplifying this we get:

`u'(x) approx frac( u(x+h) - u(x) ) ( h)`

If we take h as a small element in the x-direction, we get :

`u'(x) approx frac( u(x+Deltax) - u(x) ) ( Deltax)`

2.) Second-Order Approximation:

Expanding `u(x+h)` as a Taylor Series Expansion :

`u(x+h) = u(x) + hu'(x) + h^2frac(u''(x))(2!) + ......`

Expanding `u(x-h)` as a Taylor Series Expansion :

`u(x-h) = u(x) - hu'(x) + h^2frac(u''(x))(2!) + ......`

Subtracting both the terms above we get:

`u(x+h) - u(x-h) = 2hu'(x) + O(h^3)`

Simplifying this we get:

`u'(x) = frac(u(x+h) - u(x-h) )( 2h) + O(h^2)`

3.) Fourth Order Approximation

Expanding `u(x+h)` as a Taylor Series Expansion :

`u(x+h) = u(x) + hu'(x) + h^2frac(u''(x))(2!) + ......`

Expanding `u(x-h)` as a Taylor Series Expansion :

`u(x+h) = u(x) - hu'(x) + h^2frac(u''(x))(2!) + ......`

Expanding `u(x+2h)` as a Taylor Series Expansion :

`u(x+2h) = u(x) + 2hu'(x) + (2h)^2frac(u''(x))(2!) + ......`

Expanding `u(x-2h)` as a Taylor Series Expansion :

`u(x-2h) = u(x) -2 hu'(x) + (2h)^2frac(u''(x))(2!) + ......`

Multiplying the equations by suitable constants and adding them:

`u'(x) = frac(u(x-2h) -8u(x-h)+8u(x+h) - u(x+2h))(12h)`

Switching to MATLAB to compute these derivatives.

Here is the code that is used to compute the first, second, and fourth-order approximations of the first derivative.

clc;            % clears the screen/command window
clear all;      % clears the workspace
close all;      % closes all the figures

% Here I am going to compute the Numerical and Analytical derivatives.
% The function that I am going to be using is f(x) = sin(x)/x^3 .
% The analytical or the exact derivative is given by
% 
%               f'(x) = (x^3*cos(x)-sin(x)*3*x^2)/x^6.


% Defining the discretized range
% The element is small to give higher accuracy results
dx = pi/4000;


% Defining the variable at which the derivatives are to be created
x = pi/3;


%  Defining a variable named Analytical derivative
analytical_derivative = (x^3*cos(x)-sin(x)*3*x^2)/x^6;


% Here I will compute the numerical derivatives according to the order of approximations 
% 
% The order of approximations to be used are first , second and fourth
% order approximations


% The formula for the first order approximation of numerical derivative is:
% 
%               f'(x) = (f(x+dx)-f(x))/(dx)
% 
% Computing the numerical derivative according to first order approximation
num_first_order_approx = ((sin(x+dx)/(x+dx)^3) - (sin(x)/(x)^3))/(dx);


% The formula for the first order approximation of numerical derivative is:
% 
%               f'(x) = (f(x+dx)-f(x-dx))/(2*dx)
% 
% Computing the numerical derivative according to second order approximation
num_second_order_approx = ((sin(x+dx)/(x+dx)^3) - (sin(x-dx)/(x-dx)^3))/(2*dx);


% The formula for the first order approximation of numerical derivative is:
% 
%               f'(x) = (f(x-2dx)-8f(x-dx)+8f(x+dx)-f(x+2dx))/(12*dx)
% 
% Computing the numerical derivative according to fourth order approximation
num_fourth_order_approx = ( (sin(x-2*dx)/(x-2*dx)^3) - 8*(sin(x-dx)/(x-dx)^3)  + 8*(sin(x+dx)/(x+dx)^3) - (sin(x+2*dx)/(x+2*dx)^3) )/(12*dx);

Now to plot the bar graph of the error comparison between first, second, and fourth-order approximations of the first derivative :

% Here I am going to calculate the errors in the approximations at x = pi/3
% I will be using the abs function to find the absolute difference


% The first order error
first_order_error = abs( analytical_derivative - num_first_order_approx );


% The second order error
second_order_error = abs( analytical_derivative - num_second_order_approx );


% The fourth order error
fourth_order_error = abs( analytical_derivative - num_fourth_order_approx );


% Now since the errors are calculated let's plot them in a Bar-Graph
% Define a new parameter to plot the bar graph
errors = [ first_order_error , second_order_error , fourth_order_error ];

% Defining the x-axis variables according to the order of approximation
X_axis = [ 1 2 4 ];


% Now Plotting the bar graph representing the errors
b = bar(X_axis(1),errors(1),'Facecolor','flat');
hold on;
colormap(jet);
c = bar(X_axis(2),errors(2),'Facecolor','flat');
hold on;
colormap(summer);
d = bar(X_axis(3),errors(3),'Facecolor','flat');


% Reshaping the axis according to the plot
xlim([0 4]);
ylim([-0.002  0.003]);


% Labelling the plot and providing suitable title and legend
xlabel(' Order of approximation ');
ylabel(' Errors in the approximations ');
title('Errors in different order approximations of the First Derivative');
legend(  'First order approxiamtion' ,'Second order approximation' ,'Fourth order approximation' );


As the graphs are plotted we can see that the error is the highest for First Order Approximation, then for Second-Order Approximation and the least for Fourth Order Approximation.

CONCLUSION: It can be seen that as the order of approximation increases the accuracy also increases and the error between the numerical and analytical derivative also changes.

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.