Program that compares the first, second and fourth order approximations of the first derivative against the analytical or exact derivative.
The Project aims to show two things
1) error of first order, second order and fourth order with analytical derivative
2) comparison in between different order approximations
1-Step:
Initially, the code started with inputting variables x and dx while x value has been mentioned in the question as pi/3. While dx is taken as pi/40
The graph depicts that first-order absolute error is much higher than that of others at this specific point of x=pi/3 and dx=pi/40. While the order gets increased the error gets reduced.
2-Step:
Secondly the analytical derivative of the function sin(X)/x^3 is solved
3-Step:
The formula for forward differencing first-order approximation is (f(x+dx)-f(x))/dx; this has been solved
then the error has been found out -> abs(first order differential - analytical derivative)
4-step
The formula for central differencing second-order approximation is (f(x+dx)-f(x-dx))/2*dx; this has been solved
further, the error has been found out -> abs(second order differential - analytical derivative)
5-step
The formula for fourth-order approximation is (f(x-2x)+8*f(x+dx)-8*f(x-dx)-f(x+2dx))/12*dx; this has been solved
simultaneously the error has been found out -> abs(fourth order differential - analytical derivative)
then to showcase the errors we used the bar graph to plot the errors and compare them.
Results and Observation: The graph itself shows that the fourth order approximation is very precise that other too
clear all
close all
clc
x=pi/3
dx = pi/40;
%Analytical Derivative of function sin(x)/x^3
Analytical_derivative = (cos(x)/x^3) - ((3*sin(x))/x^4);
%first order approximation through forward differencing = (f(x+dx)-f(x))/dx
First_order_differencial = ((sin(x+dx)/(x+dx)^3) - (sin(x)/x^3))/dx;
%Error in first order approximation
First_order_error = abs(First_order_differencial - Analytical_derivative)
%second order approximation through central differencing = (f(x+dx)-f(x))/dx
second_order_differencial= ((sin(x+dx)/(x+dx)^3) - (sin(x-dx)/(x-dx)^3))/(2*dx);
%Error in second order approximation
Second_order_error = abs(second_order_differencial - Analytical_derivative)
%Fourth order approximation
fourth_order_differencial = ((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 in fourth order approximation
Fourth_order_error = abs(fourth_order_differencial - Analytical_derivative)
%bar graph plotting at this specific x and dx
Errors_Comparison = [First_order_error Second_order_error Fourth_order_error];
bar(Errors_Comparison)
set(gca,'xticklabel', {'First order error', 'Second order error', 'Fourth order error'});
xlabel('approximation order');
ylabel('Error value');
title('errors comparison for different order derivatives');
legend('error values');