Project

Discretization basics   Numerical derivative vs. exact derivative

JORGE LOZANO

Introduction to CFD using Matlab and OpenFOAM

Discretization basics

Numerical derivative vs. exact derivative

Objective

Write a program that compares the first, second and fourth order approximations of the first derivative against the analytical or exact derivative.

  1. The function that you will compute if f(x) = sin(x)/x^3
  2. You will compute the first derivative at x = pi/3

2a. You will compute the derivative both numerically and analytically. Make sure you compute 1st, 2nd and 4th order derivatives.

  1. Define an error parameter as the absolute difference in numerical and exact derivative at x = pi/3
  2. Plot a bar graph that shows the error
  3. You need to have appropriate labels and legends on your plot
  4. Make sure you upload your file

Formula for the fourth order approximation of the first derivative

Fig.1 Fourth order approximation formula

Theory

A finite difference is a mathematical expression of the form f (x + b) − f (x + a). If a finite difference is divided by b − a, one gets a difference quotient. The approximation of derivatives by finite differences plays a central role in finite difference methods for the numerical solution of differential equations, especially boundary value problems.

Certain recurrence relations can be written as difference equations by replacing iteration notation with finite differences.

Today, the term "finite difference" is often taken as synonymous with finite difference approximations of derivatives, especially in the context of numerical methods.

For functions with more variables, the partial derivatives can be approximated by grouping together all the same variables and applying the univariate approximation for that group. For example, if F(x, y) is our function, then some partial derivative approximations are

Finite differences can be considered in more than one variable. They are analogous to partial derivatives in several variables.

Some partial derivative approximations are:

Procedure

Three files having each one of the order approximations are performed separately by user created functions, called:

  • first_order_approximations(x,dx)
  • second_order_approximations(x,dx)
  • fourth_order_approximations(x,dx)

In which each one of them calculates the analytical derivative, from the equation given, and the respective differencing, having as known a forward and central differencing for the first two, and a central but wider term differencing for the fourth approximation. In each one of them an output parameter is obtained, representing the error difference according to the approximation and the analytical derivative.

In the numerical discretization file, which contains the general code that if being run give values for x as pi/3 and dx, using linspace to create 2000 values between pi/1 and pi/40000. These values represent the accuracy in between the calculations it takes more time but results as a better solution. After the given values, the three approximations files are called to run and obtain an order error by the out variable from each one of them. At the end a figure is created with a legend from the three approximations’ errors having in the x axis the dx values and maintaining all plots visible, and saving every output error result due to the for cycle used along thee i variable making every error approximation result as a 1 x 2000 matrix.

As follows, all four codes are shown below:

numerical_discretization_using_functions.m

clear all
close all
clc

x=pi/3;
dx=linspace(pi/1,pi/40000,2000);

for i= 1:length(dx)

  first_order_error(i)=first_order_approximations(x,dx(i));
  second_order_error(i)=second_order_approximations(x,dx(i));
  fourth_order_error(i)=fourth_order_approximations(x,dx(i));

end

figure(1)
loglog(dx,first_order_error,'b')
hold on
loglog(dx,second_order_error,'r')
hold on
loglog(dx,fourth_order_error,'g')
hold on

legend('first_order_error','second_order_error','fourth_order_error')

first_order_approximations.m

function out= first_order_approximations(x,dx)
  
analytical_derivative=(x^3*(cos(x))- sin(x)*3*x^2)/(x^6);

forward_differencing=((sin(x+dx)/(x+dx)^3) - (sin(x)/((x)^3)))/dx;

out=abs(forward_differencing-analytical_derivative);

endfunction

second_order_approximations.m

function out= second_order_approximations(x,dx)


analytical_derivative=(x^3*(cos(x))- sin(x)*3*x^2)/(x^6);

central_differencing=((sin(x+dx)/(x+dx)^3) - (sin(x-dx)/((x-dx)^3)))/(2*dx)

out=abs(central_differencing-analytical_derivative)



endfunction

fourth_order_approximations.m

function out= fourth_order_approximations(x,dx)


analytical_derivative=(x^3*(cos(x))- sin(x)*3*x^2)/(x^6)

fourth_differencing=(((sin(x-2*dx))/((x-2*dx)^3)) - 8*(((sin(x-dx))/((x-dx)^3)))-((sin(x+2*dx))/((x+2*dx)^3)) + 8*(((sin(x+dx))/((x+dx)^3))))/(12*dx)

out=abs(fourth_differencing-analytical_derivative)



endfunction

Results

The following three figures shows the lasts elements calculated for the errors, first order error, second order error and fourth order error respectively:

Fig. 2 Last five error quantities calculated compared to the analytical derivative.

As seen in Fig. 2 the errors obtained at last for the fourth order approximation has more accuracy compared to the other two, although it takes more time to perform the calculations due to the simplicity of the equation it’s able to do so.

The plot, Fig.3 shows the error quantity between the original analytical derivative of the equation given, and the three (first, second, and fourth order approximations), obtaining good response when applying the fourth derivative. The plot is shown in a logarithmic perspective due to the greater values obtained for both axis values.

In Fig.4 appears a close up of the same plot showing the last and important values obtained by the three approximations, being able to see the differences easily and to decide whether or not to use any of the values depending on the time give, processing speed, and accuracy to expect form a calculation.

Fig.3 PLOT; dx variation vs, error approximation from the analytical result.

Fig.4 Zoom in from error approximation plot.

Conclusion

The function: f(x) = sin(x)/x^3, was the original function to solve and compare the error approximation.

A program with four files representing the comparison between the first second and fourth approximation of the first derivative and the analytical derivative is performed as expected, managing the x and dx values to modify the speed of computing, values in between the range to calculate and plotting a logarithmic graph to see the variation and difference in each one of the methods performed. Seeing a greater convergence along the analytical value in the fourth order approximation as expected and showing four maximum values as spikes and two for the second order approximation error due to the characteristics of the function.

References

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.