Project

Computing & comparing the first, second and fourth order approximations of the first derivative against the analytical or exact derivative for range of discretization points using MATLAB

1. Function for first order error approximation for given function

function error_FDS = first_order(x,dx)

% Writing function for the first order approximation for the main program
% called "discretization_for_range_of_dx.m".

% The function will calculate the absolute error which will be derived by
% substracting it from the exact value of the derivative.
% This function will be inserted into the main program to calculate the
% error for each dx values. 

% First Derivative of function f(x)
% f'(x) = (x^3*cos(x) - 3x^2*sin(x))/x^6
first_derivative = ((x^3)*cos(x) - (3*x^2*sin(x)))/x^6;

% FDS (Forward Differencing Scheme)
% FDS = (f(x+dx) - f(x))/dx

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

% To find absolute difference between numerical & analytical method 
error_FDS = abs(first_derivative - FDS);    

end

2. Second order error approximation for given function

function error_CDS = second_order(x,dx)

% Writing function for the second order approximation for the main program
% called "discretization_for_range_of_dx.m".

% The function will calculate the absolute error which will be derived by
% substracting it from the exact value of the derivative.
% This function will be inserted into the main program to calculate the
% error for each dx values. 


% First Derivative of function f(x)
% f'(x) = (x^3*cos(x) - 3x^2*sin(x))/x^6
first_derivative = ((x^3)*cos(x) - (3*x^2*sin(x)))/x^6;

% CDS (Central Differencing Scheme) 
% 2nd order approximation, CDS = (f(x+dx) - f(x-dx))/2dx

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

% To find absolute difference between numerical & analytical method 
error_CDS = abs(first_derivative - CDS);   

end

3. Fourth order error approximation for given function

function error_CDS4 = fourth_order(x,dx)

% Writing function for the fourth order approximation for the main program
% called "discretization_for_range_of_dx.m".

% The function will calculate the absolute error which will be derived by
% substracting it from the exact value of the derivative.
% This function will be inserted into the main program to calculate the
% error for each dx values. 

% First Derivative of function f(x)
% f'(x) = (x^3*cos(x) - 3x^2*sin(x))/x^6
first_derivative = ((x^3)*cos(x) - (3*x^2*sin(x)))/x^6;

% 4th order approximation as per CDS 
% CDS4 =((f(x-2)-8)*(f(x-1)+8)*(f(x+1)-f(x+2))/12dx

CDS4 = (((sin(x-2*dx))/(x-2*dx)^3) - 8 * ((sin(x-1*dx))/(x-1*dx)^3) +8 * ((sin(x+1*dx))/(x+1*dx)^3) - ((sin(x+2*dx))/(x+2*dx)^3))/(12*dx);

% To find absolute difference between numerical & analytical method 
error_CDS4 = abs(first_derivative - CDS4); 

end

4. Program to calculate & comapre the error for order of approximation with each other & also with respect to dx values

% Writing a program that compares the first, second and fourth order
% approximations of the first derivative of given function and determine
% the error involed into the numerical derivarive against exact or
% analytical method for range of values.

% Function, f(x) = sin(x)/x^3
% Given Computation point, x = pi/3
% Discretization values, dx range = [pi/4000,pi/40,20] 

% 1st, 2nd & 4th order approximation will be derived for the given function
% using different Numerical Approximation Schemes by applying the Taylor
% Table Method.
% There are three different numerical difference schemes are available
% which are FDS (Forward Differencing Scheme), BDS (Backward Differencing
% Scheme) & CDS (Central Differencing Scheme). The FDS & BDS are 1st order
% approximation while the CDS (Central Differencing Scheme) can be used for
% 2nd & 4th order approximation.

% Objective: 
% 1. Computing the derivative of the function through both numerically & 
% analytically for the 1st, 2nd & 4th order approximation.
% 2. Calculating the error, the absolute difference, involved into the
% numerical derivation by subtracting it from the exact derivation at
% x = pi/3. 
% 3. Error needs to be computed for range of discretization values for all
% three orders & plotting them against the discretization values to comapre
% the error involved into them.
% 4. Plotting the bar graph to display the error involved for all order of
% approximation considered with appropriate labels & legends.

% First Derivative of function f(x)
% f'(x) = (x^3*cos(x) - 3x^2*sin(x))/x^6

clear all; close all; clc;

x = pi/3;   % Point to calculate 1st derivative

% Discretization Values
dx = linspace(pi/4000, pi/40, 20);

% Computing the error values for each order with respect to each
% discretization value using the for loop. The function has been defined in
% sepearte .m file for each order

for i = 1:length(dx)
    
    % First Order Error Approximation
    firstorder(i) = first_order(x,dx(i));
    
    % Second Order Error Approximation
    secondorder(i) = second_order(x,dx(i));
    
    % Fourth Order Error Approximation
    fourthorder(i) = fourth_order(x,dx(i));
end

% plotting results of each order's error against the discretization values
% to comapre the results

loglog(dx,firstorder) % for first order approximation
hold on % to add other order approximation against the dx values
loglog(dx,secondorder) % for second order approximation
loglog(dx, fourthorder) % for fourth order approximation
hold off % release the command
legend('First Order Approximation', 'Second Order Approximation', 'Fourth Order Approximation')
title('Comparing Different Order Approximations for Range of Discretization Values')
xlabel('Discretization(dx)')
ylabel('Order Approximation Error')

Results:

The graph shows the all three order of approximation error plotting aginst the dx values. The error difference between all three order is big for smaller dx value but the error difference is getting smaller with higher dx values which indicates that the accuracy of the solution is increases with smaller discretization value.

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.