Project

Comparison of 1st

  • Objective : To write a program that compares the first, second and fourth order approximations of the first derivative against the analytical or exact derivative and calculates the error as absolute difference of the approximations against exact / analytical derivative .
  • Report :
  1. The function `f(x) = sin(x)/x^3`
  2. The first derivative of the function to be computed is : `f'(x) = (x^3*(cos(x))-sin(x)*3*x^2)/x^6`
  3. The first derivative is to be computed with the value of variable x = pi/3
  • The main code :
clear all 
close all
clc

%analytical funtion = sin(x)/x^3;

% analytical derivative 
% f'(x) = (x^3(cos(x))-sin(x)*3*x^2)/x^6;

x = pi/3;
dx = pi/18;

% first order error function
first_order_error = first_order_approx(x,dx) ;

% second order error function 
second_order_error = second_order_approx(x,dx) ;

% fourth order error function
fourth_order_error = fourth_order_approx(x,dx) ;
 
% naming the x side of the bar graph
x = categorical({'1st.order','2nd.order','4th.order'});

% y side plotting order and terms to plot
y = [ first_order_error ; second_order_error; fourth_order_error ];


bar(x,y)
title('Absolute error from approximations of 1st,2nd & 4th order')
grid on
xlabel('Approximations');
ylabel('Absolute error');

saveas(gcf,'Basics','png')
  1. This is the main program , where the functions of all the three approximations are passed in .
  2. The value of x = pi/3 is determined here and the value of dx is set as any chosen numerical value . Dx is the grid spacing in the approximation equations .
  3. The output of all the functions are passed here as Error terms i.e first_order_error .. etc , as the function calculates the error parameter. (explained ahead).
  4. The bar graph is then plotted .
  5. The x side of the bar graph displays the names in order mentioned with categorical command.
  6. The y side plots the bars in the graph with y scale as the error values computed from the funtions .
  7. Then appropriate xlabel and ylabel for bar graph.
  8. The graph is saved .
  • 1st order approximation function
function output = first_order_approx(x,dx)

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

% forward differencing = (f(x+dx) - f(x))/dx 
% first order approximation 

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

output = abs(first_order_approximation - analytical_derivative);

end
  1. This function first computes the analytical derivative / exact derivative / first derivative of the funtion mentioned above .
  2. Then we apply the First order approximation aka forward differencing formula for first derivative : `(f(x+dx) - f(x))/dx`
  3. The function determined is then substituted in the first order approximation formula as shown in the program above and computed.
  4. This value is close to the analytical derivative depending on how accurate the approximation formula / method is applied .
  5. Then in the output the absolute difference between the approximation result and exact / analytical result is stored .
  6. This output is called in the main function as the Error term . It computes the difference and shows how much the approximate order's error is .
  7. The inputs x and dx are taken from the main program .
  • 2nd order approximation function
function output = second_order_approx(x,dx)

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

% second order approximation 
second_order_approximation = (sin(x+dx)/(x+dx)^3 - sin(x-dx)/(x-dx)^3)/(2*dx);

%error term 
output = abs(second_order_approximation - analytical_derivative);

end
  • 4th order approximation function
function output = fourth_order_approx(x,dx)

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

%fourth order approximation 
fouth_order_approximation = ((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 term
output = abs(fouth_order_approximation - analytical_derivative);

end
  1. The explanation / procedure for 2nd order and 4th order is exactly same as the 1st order approximation .
  2. The formula for each approximation equation is different as shown in each functions .
  3. Formula for the fourth order approximation of the first derivative is :
  • Output :

  • The output bar graph shows the Error on Yscale and Bars on Xscale with the Order of approximations.
  • From this bar graph we can determine that as the order of derivatives of approximation increases the error reduces and close to accurate results are obtained.

THE END

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.