Project

Numerical Discretization comparing the 1st, 2nd and 4th order approximations and also finding the effect of error on dx using MATLAB

OBJECTIVE:

To write a program that compares the first, second and fourth order approximations of the first derivative against the analytical or exact derivative using MATLAB.

THEORY:

Numerical Discretization:

Discretization is the process of transferring continuous functions, models, variables, and equations into discrete counterparts.

For a differentiable function `f(x,y)` and small values of `dx and dy`,we have the following derivatives.

1st order approximation:

2nd order approximation:

4th order approximation:

PROGRAM:

clear all
close all
clc

%Numerical derivative vs exact derivative
%Analytical Function, f(x) = sin(x)/x^3;

%first derivative x
x = pi/3;

%Numerical derivative dx
dx = pi/40;

%Analytical Derivative
%f'(x) = (x^3*(cos(x))-sin(x)*3*x^2)/x^6;
analytical_derivative = (x^3*(cos(x))-sin(x)*3*x^2)/x^6;


%Computing Derivatives
%1st order derivatives
Forward_differencing = ((sin(x+dx)/(x+dx)^3) - (sin(x)/(x)^3))/dx;

%2nd order derivatives
Central_differencing_2nd_order = ((sin(x+dx)/(x+dx)^3) - (sin(x-dx)/(x-dx)^3))/(2*dx);

%4th order derivatives
Central_differencing_4th_order = ((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);

%Error Parameter
error_1st_order = abs(Forward_differencing - analytical_derivative);
error_2nd_order = abs(Central_differencing_2nd_order - analytical_derivative);
error_4th_order = abs(Central_differencing_4th_order - analytical_derivative);

%Plotting
derivatives = categorical({'1st order','2nd order','4th order'});
error = [error_1st_order,error_2nd_order,error_4th_order];

bar(derivatives,error,0.4,'b')
xlabel('Approximation')
ylabel('Error')
title('Order of derivatives against error')
legend('Error')

EXPLANATION:

  • clear all, close all & clc commands are used in order to gain a new start without the use of previous values.
  • The first derivative of x(given) and Numerical derivative of dx(assumption) is assigned.
  • Now with the given function, analytical derivative is differentiated and assingned to the variable.
  • With the help of the 1st, 2nd and 4th order derivatives equation know, derivation is done.
  • To find the error parameter, abs command is used to determine them irrespective of the sign.
  • Bar graph is been plotted for Order of derivatives against error using bar command.

RESULTS:

PLOT:

CONCLUSION:

From the results we can conclude that,

  • Higher the order goes, lesser the magnitude error becomes.
  • Lower order yields higher errors.

------------------------------------------------------------------------------------------------------------

Effect of dx on error

OBJECTIVE:

To write a program that compares the first, second and fourth order approximations of the first derivative for a range of dx and finding its effect on the error using MATLAB.

PROGRAM:

Here the program is splitted into four parts by the command function, to make the program neat.

First order approximation:

function error_1st_order = First_order_approximation(x,dx)

%Numerical Discretization
%Analytical Function, f(x) = sin(x)/x^3;
%Analytical Derivative, f'(x) = (x^3*(cos(x))-sin(x)*3*x^2)/x^6;

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

%First_order_approximation
Forward_differencing = ((sin(x+dx)/(x+dx)^3) - (sin(x)/(x)^3))/dx;

%Error
error_1st_order = abs(Forward_differencing - analytical_derivative);

end

Second order approximation:

function error_2nd_order = Second_order_approximation(x,dx)

%Numerical Discretization
%Analytical Function, f(x) = sin(x)/x^3;
%Analytical Derivative, f'(x) = (x^3*(cos(x))-sin(x)*3*x^2)/x^6;

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

%First_order_approximation
Central_differencing_2nd_order = ((sin(x+dx)/(x+dx)^3) - (sin(x-dx)/(x-dx)^3))/(2*dx);

%Error
error_2nd_order = abs(Central_differencing_2nd_order - analytical_derivative);

end

Fourth order approximation:

function error_4th_order = Fourth_order_approximation(x,dx)

%Numerical Discretization
%Analytical Function, f(x) = sin(x)/x^3;
%Analytical Derivative, f'(x) = (x^3*(cos(x))-sin(x)*3*x^2)/x^6;

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

%First_order_approximation
Central_differencing_4th_order = ((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);

%Error
error_4th_order = abs(Central_differencing_4th_order - analytical_derivative);

end

Explanation:

  • function command is determined here for error_1st_order, error_2nd_order, error_4th_order respectively as three functions with inputs x and dx.
  • Now with the given function, analytical derivative is differentiated and assingned to the variable accordingly for higher orders.
  • The effect of dx for the respective errors is found using the error formula.
  • In order to determine the error irrespective of the sign, abs command is used.
  • Program is now ended.

Main Program :

Effect of dx on error

clear all
close all
clc

%Effect of dx on error
%first derivative x
x = pi/3;

%Numerical derivative dx
dx = linspace(pi/3,pi/3000,20);

for i = 1:length(dx)
    
    First_order_error(i) = First_order_approximation(x,dx(i));
    Second_order_error(i) = Second_order_approximation(x,dx(i));
    Fourth_order_error(i) = Fourth_order_approximation(x,dx(i));
    
end

%Plotting
loglog(dx,First_order_error,'r');
hold on
loglog(dx,Second_order_error,'g');
hold on
loglog(dx,Fourth_order_error,'b');

xlabel('dx')
ylabel('Error')
legend('First order','Second order','Fourth order')
title('Effect of dx on error')
    

Explanation:

  • clear all, close all & clc commands are used in order to gain a new start without the use of previous values.
  • First derivative x is given as pi/3 which is assigned to it.
  • Now the dx value is assumed to have 20 values in between pi/3 to pi/3000 by using linspace command.
  • To go through all the 20 values of dx in a loop for finding the error, for loop command is used.
  • The functions (First_order_approximation, Second_order_approximation, Fourth_order_approximation) which coded before are now used in the for loop with i as the repeating array.
  • for loop is now ended.
  • The known data are now plotted using loglog command in a single plot which is done by hold on command.
  • The plot is now labelled and the result is generated.

RESULTS:

Effect of dx on error plot

CONCLUSION:

From the above results we can conclude that,

  • As the value of dx increases the error also increases.
  • Although initially the values of error for Fourth order is quite high, but as it declines the value of step size, it gives a lesser error.
  • Slope of the Fourth order drops exponentiatly meaning the error dropping rate is higher than other orders.
  • Hence Fourth order approximation is the most accurate one.
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.