Project

Discretization basics - Comparing the first, second and fourth order approximations of the first derivative against the analytical derivative

Aim:

To discretized a function and to compare the first,second and fourth order approximation of the first derivative of the function against its analytical value.

Objective:

  • To discretize the function and find the first, second and fourth order approximation.
  • To calculate the analytical value of the function.
  • To find the errors in three different approximation values.
  • To plot a bar graph and analyse the errors of three approximation values.

Discretization:

Discretization is a process of transferring the function or equation into discrete points. This discretization process helps to convert the partial differential equations into algebraic function. Discretization plays a pivotal role in the computational fluid dynamics domain. There are various methods of discretization,

  • Finite element method.
  • Finite difference method.
  • Finite volume method.

The exact discretization may not be found due to heavy matrix exponential and other factors. This can be prevented by finding the approximation of the function and find its error from the exact or analytical value of the function. Based on the consideration of number of discretized points, the order of approximation varies.

Code:

clear all
close all
clc

% Initializing x value and the function
x=pi/3;
fn = sin(x)/x^3;

%Analytical solution
analytical = (x^3*cos(x) - 3*x^2*sin(x))/x^6

%dx value
dx = pi/30;

%first order approximation with error
first_order = ((sin(x+dx)/(x+dx)^3) - (sin(x)/x^3))/dx;
error1 = abs(first_order-analytical)

%Second order approximation with error
second_order = ((sin(x+dx)/(x+dx)^3) - (sin(x-dx)/(x-dx)^3))/(2*dx);
error2 = abs(second_order-analytical)

% (f(j-2)-8*f(j-1)+8*f(j+1)-f(j+2))/12dx
% split the equation into several terms
a = (sin(x-dx-dx))/(x-dx-dx)^3;
b =  8*((sin(x-dx))/(x-dx)^3);
c =  8*((sin(x+dx))/(x+dx)^3);
d = (sin(x+dx+dx))/(x+dx+dx)^3;

%fourth order approximation with error
fourth_order = (a-b+c-d)/(12*dx);
error4 = abs(fourth_order-analytical)

%plotting the errors
y = [error1,error2,error4]
label = {'error1','error2','error4'};
bar(y,'facecolor','b')
set(gca,'xticklabel',label)
title('Bar graph representation of errors');

Code explanation:

  • Initially, the x value is assigned as pi/3.
  • The function is assigned. Here function = sin(x)/x^3 was assigned.
  • The function is solved analytically.
  • The first order approximation value is obtained by solving the ((sin(x+dx)/(x+dx)^3) - (sin(x)/x^3))/dx discretized equation after assigning the dx value.
  • The error value is obtained from the formula error = approximation_value - analytical value.
  • Similarly, the second order approximation value and its error are obtained by solving ((sin(x+dx)/(x+dx)^3) - (sin(x-dx)/(x-dx)^3))/(2*dx) discretized equation.
  • The fourth order approximation and its error are obtained by solving the following discretized equation,
  • Finally, the error values of various approximation is plotted in a bar graph representation and the results are analysed.

Output plot:

Conclusion:

The error in fourth order approximation of the first derivative is less when compared with the other two order approximation values. The error value decresed in the order of error4<error2<error1. Thus, higher the order of approximation, less will be the error obtained in discretization.

Reference:

https://en.wikipedia.org/wiki/Discretization

https://in.mathworks.com/help/matlab/ref/bar.html

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.